Algorithm title: Print 1 to the maximum n digits

Source: Internet
Author: User

Note : This article is only for learning Exchange, reprint please indicate the source, welcome reprint! today saw the sword refers to the offer on the 12th topic, such as the following:Enter the number n. The number of n bits 10 digits from 1 to the maximum is printed sequentially.

For example, if you enter 3, the,..., 999 is printed.

when I see this topic. The first feeling is to use recursion, why? First we have to start with one of our actual figures, for example 123. The number plus 1 is actually divided into two steps, such as the following: Step 1: The lowest bit plus 1; Step 2: If rounding occurs, the effect of the carry is propagated to a higher level ( this is where recursion is located ). before this algorithm problem is completed. I want to insert a " detail to determine success or failure!" "We write a program, in fact the general idea can be thought of." And often ignore the details of the consideration, such as the question to be considered, such as the following: 1. How to represent an n-digit? ( with character array ) 2. Each plus 1 is in the lowest position; 3. Add 1 if rounding occurs. The carry is propagated to the high-level number (the loop can be used here as well as recursion ); 4. If the highest bit occurs, then overflow, the overflow can be used as a sign of the last number printed; 5. When printing a number, it is only possible to start printing from a number that is not 0 of the highest digit. ( this conforms to the normal representation of the numbers )        recursive implementations such as the following:
 #include <iostream> #include <cstring>using namespace Std;bool Increment ( char *str,int length)//string plus 1, assuming that an overflow occurs, returns True, otherwise returns FALSE{IF (Str==null && length<1)//Assuming an overflow occurred. then returns False{return false;} int sum=str[length-1]-' 0 ' +1;if (sum<10)//Assuming no rounding occurs {Str[length-1]+=1;return true;} Else{if (length-1==0)//If overflow is assumed, returns False{return false;} str[length-1]= ' 0 ';//Take carry, leave the remaining task to the previous length-1 character return Increment (str,length-1);//recursive expression}}void print (char *str)//print character, When printing, remove the previous 0{bool isbegin=false;//identification to be able to start output int i;for (I=0;i<strlen (str); i++) {if (!isbegin && str[i]!= ' 0 ')/ /Assuming that the first non-0 character is found, the identity can start output {isbegin=true;} if (Isbegin) {cout<<str[i];}} Cout<<endl;} void Tomaxn (int n)//prints from 1 to the maximum number of n digits {if (n>=1) {char *str=new char[n+1];memset (str, ' 0 ', n); str[n]= ' + '; while (Increment ( STR,N)//Assuming a smooth growth of {print (str);} delete []str;}} int main () {int N;while (cin>>n) {if (n>=1 && n<=5) {TOMAXN (n);}} return 0;} 
      non-recursive implementations such as the following:
 #include <iostream> #include <cstring>using namespace Std;bool Increment ( char * str)//used to add 1 to the corresponding number of the current string, which returns true to indicate the addition of {int len=strlen (str); int current=str[len-1]-' 0 ' +1;//the digit digit plus 1, Current represents a value of 1 after int i=len-1;//to propagate the chain carry reaction after the single plus 1 while (i>=0)///with I for the present bit {if (CURRENT<10)//If plus 1 does not carry {str[i]=str[i]+ 1;break;} ELSE//assumes that the current bit takes place in the carry {if (i==0)//Assuming that the rounding is the highest bit. The overflow {return false is directly occurred;} ELSE//assumes that the carry is not the highest level, here can guarantee the i!=0, because there is a branch of 0 to deal with {str[i]= ' 0 ';//First the standard zero i=i-1;//start processing the standard of the previous current=str[i]-' 0 ' +1;}} return true;} void print (char *str)//Displays the number {bool Begin=false;int i;int Len=strlen (str); for (i=0;i<len;i++) {if (!begin && str[i]!= ' 0 ') {begin=true;} if (begin)//Assume that the first high number {Cout<<str[i] that is not 0 is found;}} Cout<<endl;} void Tomaxn (int n)//client called the function {char *str=new char[n+1];memset (str, ' 0 ', n);//Note the initial value is in the middle, not the third parameter str[n]= ' n '; Increment (str)) {print (str);} delete []str;} int main () {int N;while (cin>>n) {if (n>=1 && n<=5) {TOMAXN (n);}} return 0;} 
        test results such as the following:       References :"The sword means offer"

Algorithm title: Print 1 to the maximum n digits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.