12--Prints 1 to the largest n for the number.

Source: Internet
Author: User

/* Title: Print 1 to the largest n for the number. For example: Enter 3, print 1 to 999. The problem of solving ideas, this is a large number of questions. The self-addition operation of large number. But pay attention to 2 points. (1): Judging whether the cross-border, in other words, is to reach the maximum value of the discrimination. The strcmp can be selected, but the complexity is O (n), because only when rounding is possible, and only 99 ... 999 will not cross when rounding up, so I am judged if I is 0. (2): output, note that 0 is removed, 0098, into 98.*/#include <stdio.h> #include <iostream>using namespace std;//determine whether the maximum value, exit output. Only when the highest one implements a carry, is it overflow. BOOL Isoverflow (char *number) {int length = strlen (number)-1;//string The last one is ' \ s ', here is a minus 1 operation. printf ("Length%d\n", length), int ntakeover = 0;//carry flag, determines whether to carry bool Isoverflow = false;//is not the maximum value for (int i = length; I &G t;= 0; i--)//string from left to right, but number is from right to left. {//The first loop is the number that gets the lowest bit, and the future loop is used for carry. int nSum = number[i]-' 0 ' + ntakeover;if (i = = length)//Only the lowest bit does plus 1, which is the first time. Nsum++;if (NSum >= 10)//if greater than or equal to 10, it is necessary to carry {if (i = = 0)//Because here from 1 to length-1, it is already the length of the number, if 0, is out of bounds. {isoverflow = True;break;} Ntakeover = 1;nsum-= 10;number[i] = ' 0 ' + nSum;} else//not greater than 10, complex value, and on exit loop {number[i] = ' 0 ' + Nsum;break;}} Return isoverflow;//returns whether the symbol is out of bounds. }//printing numbers, note that the front of the string is 0 non-printing, such as 098, printing only 98. void Printnumber (char *number) {int length = strlen (number); bool boolSingalprint = false;for (int i = 0; i < length; i++) {if (Boolsingalprint = = False && number[i]! = ' 0 ') Boolsinga Lprint = true;if (Boolsingalprint = = True) cout << number[i];} cout << ' t ';} void formonetonum (int num) {if (num <= 0) {cout << re-enter << Endl;return;} The end of the string is added to, so the length plus 1char *number = new Char[num + 1];memset (number, ' 0 ', num), number[num] = '!isoverflow '; R) {printnumber (number);} Delete[]number;} int main () {cout << Please enter a number length. \ n '; int numbers = 0;cin >> number;formonetonum; return 0;}

  

12--Prints 1 to the largest n for the number.

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.