The rookie series of the C + + Classic Questions (10)

Source: Internet
Author: User

Print 1 to the largest N Number of digits

Title: Enter a number N , sequentially prints out the 1 to the largest N Digit decimal number. For example, enter 3, then print 1, 2, 3, ... .. , 999.

Method One:

This problem is very simple to see, the first to find the Maximum number of n-digit, but a 1 to the maximum value of the cycle is done , If you really put such an answer to the interviewer's words, the consequences are very serious. First, without considering whether the maximum number of gains will overflow, if overflow, the answer is definitely wrong. This code is relatively simple, I will not list it.

Method Two:

Obviously, this problem can be done by recursion, that is, whenever you add one more, I will print it all over the added bit, such as the previous 1-bit, I when I become a double-digit time, from 1-9 to set the 10-digit number, whenever set a value such as 1, In the case of printing all the possible numbers except this 1 bit, Note that each of the top bits here can only be 1-9, but the range of the other bits is 0-9. But the recursion is likely to cause the call stack to overflow, I'll forget it, the largest 8 - byte long Long The integer maximum of the type can reach the decimal position, so if it is done by hand, it is very likely ... anyway, this is a thought, well, not much to say, I put my implementation to the list:

inline void Printonenum (char *pnumbers) {static int count = 0;    printf ("%-6s,", pnumbers);        if (++count = = ten) {cout << Endl;    Count = 0;    }}inline void Printonenum (Long long val) {static int count = 0;    printf ("%-6ld", Val);        if (++count = = ten) {cout << Endl;    Count = 0;        }}void digitrecursive (char *pnumbers, int offset, int bitindex, const int allbits) {if (Bitindex = = allbits) {        Printonenum (pnumbers + offset);    Return        } for (int i = 0; i < ten; ++i) {Pnumbers[bitindex] = i + ' 0 ';    Digitrecursive (pnumbers, offset, Bitindex + 1, allbits);        }}void printeachbitsnumbers (char *pnumbers, int bitindex, const int allbits) {for (int i = 1; i <; ++i) {        Pnumbers[bitindex] = i + ' 0 ';    Digitrecursive (Pnumbers, Bitindex, Bitindex + 1, allbits); }}inline int Getmaxbitoflonglong () {return (int) log (Llong_max);} Long long getmaxvalueofnbits (const int n) {if (n > Getmaxbitoflonglong ()) {return-1;    } Register Long Long max = 1;    for (int i = 0; i < n; ++i) {max *= 10; } return max-1;}    void printallwithoutrecursive (const int n) {Register const long Long max = Getmaxvalueofnbits (n);    For (long long i = 1; I <= max; ++i) {printonenum (i); }}void printallnumbers (const int n) {if (n < 0) {fprintf (stderr, "Input param error, the n must large tha        n 0\n ");    Return        } if (n < Getmaxbitoflonglong ()) {printallwithoutrecursive (n);    Return    } char *pnumbers = new Char[n + 1];    Pnumbers[n] = ' + ';    for (int i = n-1; I >= 0; i.) {Printeachbitsnumbers (pnumbers, I, N); } delete[] pnumbers;}
The above code, I have both implemented, that is, when the number of bits in the input is smaller, do not overflow, use method one, if overflow, use method two.

Of course there are other ways to do it, and I'm not going to list it here.

If there is a mistake welcome to point out, share please identify

Feel good words on the top one, feel good words on the step.

The rookie series of the C + + Classic Questions (10)

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.