Jz-c-12

Source: Internet
Author: User

Sword Point of Offer question 12th: Print 1 to the maximum n number, "large number Problem"

1 //============================================================================2 //Name:jz-c-12.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description: Sword Point of Offer question 12th: Print 1 to the maximum n number, "large number Problem"7 //============================================================================8 9#include <iostream>Ten#include <string.h> One#include <memory>//memory is the header file for the C + + Space Configurator and the new delete definition, which defines the spatial configurator, new Delete, and some functions to invoke the constructor function.  A using namespacestd; -  - voidPrintnumber (Char*Number ); the BOOLIncrement (Char*Number ); - voidPrint1tomaxofndigitsrecursively (Char* Number,intLengthintindex); -  - voidPrint1tomaxofndigits_1 (intN) { +     if(N <=0) -         return; +  A     Char*number =New Char[n +1]; atmemset (number,'0', n); -Number[n] =' /'; -  -      while(!Increment (number)) { - printnumber (number); -     } in  -     Delete[] number; to } + //The string number represents a digit and is incremented by 1 - //returns True if an addition overflow is done; false otherwise the BOOLIncrement (Char*Number ) { *     BOOLIsoverflow =false; $     intNtakeover =0;Panax Notoginseng     intNlength =strlen (number); -  the      for(inti = nlength-1; I >=0; i--) { +         intNSum = Number[i]-'0'+Ntakeover; A         if(i = = nlength-1) thensum++; +  -         if(NSum >=Ten) { $             if(i = =0) $Isoverflow =true; -             Else { -NSum-=Ten; theNtakeover =1; -Number[i] ='0'+NSum;Wuyi             } the}Else { -Number[i] ='0'+NSum; Wu              Break; -         } About     } $  -     returnIsoverflow; - } - //==================== method Two ==================== A voidPrint1tomaxofndigits_2 (intN) { +     if(N <=0) the         return; -  $     Char* Number =New Char[n +1]; theNumber[n] =' /'; the  the      for(inti =0; I <Ten; ++i) { thenumber[0] = i +'0'; -print1tomaxofndigitsrecursively (number, N,0); in     } the  the     Delete[] number; About } the  the voidPrint1tomaxofndigitsrecursively (Char* Number,intLengthintindex) { the     if(Index = = Length-1) { +Printnumber (number);//print, end a loop -         return; the     }Bayi  the      for(inti =0; I <Ten; ++i) { theNumber[index +1] = i +'0';//you can turn an integer into a character -print1tomaxofndigitsrecursively (number, length, index +1); -     } the } the  the //==================== Public Function ==================== the //The string number represents a number that has a number of 0 starts - //print out this number and ignore the beginning of the 0 the voidPrintnumber (Char*Number ) { the     BOOLISBEGINNING0 =true; the     intNlength =strlen (number);94  the      for(inti =0; i < nlength; ++i) { the         if(ISBEGINNING0 && number[i]! ='0') theISBEGINNING0 =false;98  About         if(!ISBEGINNING0) { - //printf ("%c", Number[i]);101cout <<Number[i];102         }103     }104  the //printf ("\ t");106cout <<" ";107 }108 109 //==================== test Code ==================== the voidTest (intN) {111 //printf ("Test for%d begins:\n", n); thecout <<"Test for"<< N <<"begins:"<<Endl;113 print1tomaxofndigits_1 (n); the //print1tomaxofndigits_2 (n); the  the //printf ("Test for%d ends.\n", n);117cout << Endl <<"Test for"<< N <<"ends."<<Endl;118 }119  - intMainintargcChar**argv) {121Test (1);122Test (2);123Test (3);124Test (0); theTest (-1);126     return 0;127}

Jz-c-12

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.