Question Link
Question:The number of N must be sorted according to the rule size given by the question to output all possible results.
Ideas:In fact, all the obtained sequences are in the full arrangement of N numbers, so the difficulty is how to output them according to the format given by the question. We can see that it is actually inserted in a known sequence, so we can use backtracking to insert elements. here we can use vector to facilitate element insertion.
# Include <iostream> # include <cstdio> # include <cstring> # include <vector> # include <algorithm> using namespace STD; const int maxn = 10; char STR [maxn]; int N; void space (int cnt) {for (INT I = 0; I <CNT; I ++) printf ("");} void deal (INT cur, vector <char> & S) {If (cur = N) {space (n); printf ("writeln ("); printf ("% C", s [0]); For (INT I = 1; I <n; I ++) printf (", % C ", s [I]); printf (") \ n"); return;} else {for (INT I = cur; I> = 0; I --) {space (cur); if (I <cur) printf ("else"); if (I) printf ("If % C <% C then ", s [I-1], cur + 'A'); printf ("\ n"); vector <char> SS (s); SS. insert (ss. begin () + I, 'A' + cur); Deal (cur + 1, SS) ;}} int main () {int CAS; scanf ("% d ", & CAS); While (CAS --) {scanf ("% d", & N); For (INT I = 0; I <n; I ++) STR [I] = 'A' + I; printf ("program sort (input, output); \ nvar \ n"); printf ("% C ", STR [0]); For (INT I = 1; I <n; I ++) printf (", % C", STR [I]); printf (": integer; \ nbegin \ n "); printf (" readln (% C ", STR [0]); For (INT I = 1; I <n; I ++) printf (", % C", STR [I]); printf ("); \ n"); vector <char> S; S. push_back ('A'); Deal (1, S); printf ("end. \ n "); If (CAS) printf (" \ n ");} return 0 ;}