UVA110- Meta-Loopless Sorts(模拟全排列)
来源:互联网 发布:sqlserver大数据处理 编辑:程序博客网 时间:2024/06/05 10:07
题目链接
题意:给你n个数,要求按照题目所给的规则大小排序,输出所有可能的结果。
思路:其实求出来的所有序列是n个数的全排列,那么难点在于怎么按照题目所给的格式输出。我们可以看出其实是在已知的序列上插空,所以就可以使用回溯来插入元素,这里可以使用vector,方便元素的插入。
代码:
#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;}
0 0
- UVA110- Meta-Loopless Sorts(模拟全排列)
- uva110 - Meta-Loopless Sorts
- UVa 110 Meta-Loopless Sorts (递归&代码模拟&全排列)
- uva110-Meta-Loopless Sorts(没有循环的排序程序)
- 110 - Meta-Loopless Sorts
- UVaOJ110---Meta-Loopless Sorts
- 110Meta-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
- UvaOJ 110 Meta-Loopless Sorts
- UVa 110 - Meta-Loopless Sorts
- 2014.08.15 周五-没记录-web
- IOS7状态栏隐藏
- 内存管理高级总结
- 获取option中的值
- C# 产生随机数( DateTime.Now.Ticks)
- UVA110- Meta-Loopless Sorts(模拟全排列)
- Codeforces 220B - Little Elephant and Array 离线树状数组
- poj1509 Glass Beads,后缀自动机
- RPM 简单介绍
- nyoj 会场安排
- xib的实际操作
- QTP中RO与TO的定义区别
- C++笔试题(阿里)——sizeof运算符
- 堆数据结构的实现以及堆排序