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