// // ACM UVa Problem #110 //http://acm.uva.es/p/v1/110.html // // Author: ATField // Email: atfield_zhang@hotmail.com // #include <iostream> #include <vector> #include <cstdlib> usingnamespace std; void indent(int n) ...{ // output indent for( int j =0; j < n; ++j ) cout <<""; } void make_prog( int n, int end, const vector<int>&seq ) ...{ if( n == end ) ...{ // we already come to the end // just output the vector indent(n); cout <<"writeln("; for( int i =0; i < n; ++i ) ...{ if( i >0 ) cout <<","; cout <<char( 'a'+ seq[i] ); } cout <<")"<< endl; } else ...{ // big if statement for( int i = n; i >=0; --i ) ...{ indent(n); if( i < n ) cout <<"else "; if( i >0 ) cout <<"if "<<char( 'a'+ seq[i -1] ) <<" < "<<char( 'a'+ n ) <<" then"; cout << endl; vector<int> new_seq = seq; new_seq.insert( new_seq.begin() + i, n ); make_prog( n +1, end, new_seq ); } } } void output_program(int n) ...{ vector<int> seq; seq.push_back(0); // 'a' is the initial sequence // // Output the begin part of the program // cout <<"program sort(input,output);"<< endl <<"var"<< endl; // var for( int i =0; i < n; ++i ) ...{ if( i >0 ) cout <<","; cout <<char( 'a'+ i ); } cout <<" : integer;"<< endl; cout <<"begin"<< endl; // readln cout <<" readln("; for( int i =0; i < n; ++i ) ...{ if( i >0 ) cout <<","; cout <<char( 'a'+ i ); } cout <<");"<< endl; // // Output the big if statement // make_prog(1, n, seq); // output the whole program from 1 to n cout <<"end."<< endl; } int main(int argc, char* argv[]) ...{ int m; cin >> m; // total m programs to make for( int i =0; i < m; ++i ) ...{ int n; cin >> n; output_program(n); } return0; }