0 of 1 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 1 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1.(排列数)输入两个正整数n,m(1≤n≤20,1≤m≤n),在 1∼n 中任取 m 个数,按字典序从小到大输出所有这样的排列。例如:
输入:3 2
输出:1 2 1 3 2 1 2 3 3 1 3 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <iostream> #include <cstring> using namespace std; const int SIZE = 25; bool used[SIZE]; int data[SIZE]; int n, m, i, j, k; bool flag; int main() { cin >> n >> m; memset( used, false, sizeof(used) ); for ( i = 1; i <= m; i++ ) { data[i] = i; used[i] = true; } flag = true; while ( flag ) { for ( i = 1; i <= m - 1; i++ ) cout << data[i] << " "; cout << data[m] << endl; flag = ①; for ( i = m; i >= 1; i-- ) { ②; for ( j = data[i] + 1; j <= n; j++ ) if ( !used[j] ) { used[j] = true; data[i] = ③; flag = true; break; } if ( flag ) { for ( k = i + 1; k <= m; k++ ) for ( j = 1; j <= ④; j++ ) if ( !used[j] ) { data[k] = j; used[j] = true; break; } ⑤; } } } } |
1空:
2空:
3空:
4空:
5空:
(2012年真题)