UVA - 110 Meta-Loopless Sorts
来源:互联网 发布:淘宝客没有pid 编辑:程序博客网 时间:2024/05/16 14:42
题目大意:模仿 Pascal 输出 n 个数字比较大小的过程。
解题思路:全排列,向上层序列从后往前插入新元素,如给出 n=3,则最开始为 a,第一层 b,插入为 ab,到第二层 c,为 abc,向前得到 acb,cab,然后返回上一层 b,向前移动为 ba,此基础上第二层bac,bca,cba,返回上一层 b,结束,回到 a,结束。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespace std;char ans[10];int n;void dfs(int now) { if (now == n) { for (int i = 0; i < now; i++) printf(" "); printf("writeln("); for (int i = 0; i < n-1; i++) printf("%c,", ans[i]); printf("%c)\n", ans[n-1]); return; } int used[10]; for (int i = 0; i < now; i++) used[i] = ans[i]; for (int i = now+1; i > 0; i--) { ans[i] = ans[i-1]; ans[i-1] = 'a'+now; if (now > 0) { for (int j = 0; j < now; j++) printf(" "); if (i == now+1) printf("if %c < %c then\n", ans[i-2], now+'a'); else if (i == 1) printf("else\n"); else printf("else if %c < %c then\n", ans[i-2], ans[i-1]); } dfs(now+1); } for (int i = 0; i < now; i++) ans[i] = used[i];}int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &n); printf("program sort(input,output);\nvar\n"); for (int i = 0; i < n; i++) ans[i] = 'a' + i; for (int i = 0; i < n-1; i++) printf("%c,", ans[i]); printf("%c : integer;\nbegin\n readln(", ans[n-1]); for (int i = 0; i < n-1; i++) printf("%c,", ans[i]); printf("%c);\n", ans[n-1]); dfs(0); printf("end.\n"); if (T) printf("\n"); }return 0;}
0 0
- uva 110 - Meta-Loopless Sorts
- uva 110 - Meta-Loopless Sorts
- UVa 110 - Meta-Loopless Sorts
- uva 110 - Meta-Loopless Sorts
- uva 110 Meta-Loopless Sorts
- UVa 110 - Meta-Loopless Sorts
- uva-110-Meta-Loopless Sorts
- UVA - 110 Meta-Loopless Sorts
- UVA 110 Meta-Loopless Sorts
- UVa 110 - Meta-Loopless Sorts
- UVA - 110 Meta-Loopless Sorts
- UVa Problem 110 - Meta-Loopless Sorts
- UVa Problem 110 - Meta-Loopless Sorts
- uva 110 Meta-Loopless Sorts(递归)
- 110 - Meta-Loopless Sorts
- 110Meta-Loopless Sorts
- UvaOJ 110 Meta-Loopless Sorts
- UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)
- LintCode:加油站
- nls_charset_name(n1)
- 为什么Activity生命周期函数是运行在UI线程
- Android屏幕适配
- 二叉树中相距最远的两个节点之间的距离
- UVA - 110 Meta-Loopless Sorts
- 设计模式_代理模式(Proxy)
- nls_charset_id(c1)
- HEXTORAW(c1)
- Android-理解Context
- BMP文件格式
- scala进阶18-隐式对象
- CONVERT(c1,set1,set2)
- MP4 seek状态 sample读取流程