Question 1515: print the n-digit-9 degree from 1 to the maximum

Source: Internet
Author: User

Description:
Given a number N, print the N digits from 1 to the maximum.
Input:
Each input file contains only one set of test samples.
For each test case, enter a number N (1 <=n <= 5 ).
Output:
For each test case, the number of N digits from 1 to the maximum is printed in sequence.
Sample input:
1
Sample output:
1
2
3
4
5
6
7
8

9

Recommendation index :※

Source: http://ac.jobdu.com/problem.php? PID = 1, 1506

1. This limit on the number of digits of N is easier to AC.

 
# Include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h> using namespace STD; const int Val [6] = {99,999,999,}; int main () {int N, I; scanf ("% d ", & N); for (I = 1; I <= Val [N]; I ++) printf ("% d \ n", I); return 0 ;}

2. However, if the number of digits is not limited, the use of int will overflow, using add_num to simulate the Add 1 action.

# Include <iostream> # include <stdlib. h> # include <stdio. h> # include <string. h> using namespace STD; bool add_num (char * num, int index) {If (index <0) return false; int TMP = num [Index]-'0' + 1; if (TMP <10) {num [Index] = num [Index] + 1; return true;} else {num [Index] = '0'; return add_num (Num, index-1) ;}} void print_num (char * num) {int I; for (I = 0; num [I] = '0' & num [I]! = '\ 0'; I ++); For (int K = I; num [k]! = '\ 0'; k ++) printf ("% C", num [k]); printf ("\ n") ;}int main () {int N, i; scanf ("% d", & N); char * num = new char [n + 1]; for (I = 0; I <n; I ++) {num [I] = '0';} num [I] = '\ 0'; while (add_num (Num, n-1 )! = False) {print_num (Num);} return 0 ;}

3. The sword refers to the offline approach, which also jumps out of the number range and uses the arrangement and combination idea.

 # include 
  
    # include 
   
     # include 
    
      using namespace STD; void print_num (char * num) {int I; for (I = 0; num [I] = '0' & num [I]! = '\ 0'; I ++); If (Num [I]! = '\ 0') {for (int K = I; num [k]! = '\ 0'; k ++) printf ("% C", num [k]); printf ("\ n") ;}} void permutation (char * num, int index, const int Len) {int I; If (Index = Len) print_num (Num); else {for (I = 0; I <10; I ++) {num [Index] = I + '0'; permutation (Num, index + 1, Len) ;}} int main () {int N, I; scanf ("% d", & N); char * num = new char [n + 1]; for (I = 0; I 
     
    
   
  

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.