uva 120 Stacks of Flapjacks(模拟)
来源:互联网 发布:桌面音响推荐 知乎 编辑:程序博客网 时间:2024/06/06 18:29
题意是选定一个位置,那个位置之前的数字全部翻转,求能翻转成从大到小排列的操作。
我觉得深搜可以a,并且可以找到最短的翻转路径,但是。。。功力不够。。。写不出来。。。
模拟,找出目前序列最大的值,将其翻向排头,再甩回排尾,完成一次操作,继续重复前面的过程,直到所有数字归位。
代码:
#include <stdio.h>const int Maxn = 31;int a[Maxn];int n;void flip(int cut){ printf("%d ", n - cut); for (int i = 0; i <= cut >> 1; i++) { int tmp = a[i]; a[i] = a[cut - i]; a[cut - i] = tmp; }}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL char tmp; n = 0; while (scanf("%d%c", &a[n++], &tmp) != EOF) { if (tmp == '\n')//输入结束 { printf("%d", a[0]);//最后一个数不打印空格 for (int i = 1; i < n; i++) printf(" %d", a[i]); printf("\n"); int cnt = n; while (cnt)//cnt代表最后一个数的下标,当最大数找出并归位,舍弃 { int max = 0, cut; for (int i = 0; i < cnt; i++)//找出最大值 { if (max < a[i]) { cut = i; max = a[i]; } } if (cut != cnt - 1)//若最大值不在最后一个位置,翻转 { if (cut != 0) flip(cut);//先将最大值翻向头位 flip(cnt - 1);//翻回尾来 } cnt--; } printf("0\n"); n = 0;//初始化n } } return 0;}
0 0
- uva 120 Stacks of Flapjacks(模拟)
- UVA 120 --- Stacks of Flapjacks 模拟
- uva 120 Stacks of Flapjacks(检索)
- UVa 120 - Stacks of Flapjacks (排序)
- UVA 120 - Stacks of Flapjacks(构造)
- UVA 120 Stacks of Flapjacks (煎饼)
- UVA 120 Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- uva 1069 - Always an integer(数学+暴力)
- 【C语言天天练(二十)】scanf函数详解
- 学习SVM(libsvm)的点滴
- Install vimgdb
- 缓冲区溢出漏洞攻击
- uva 120 Stacks of Flapjacks(模拟)
- 配置文件参数说明:
- error:用printf或者是cout输出指针时,不能输出指针所指的内容!
- 泛型方法重写
- 本人在CSDN上的技术博客访问量突破了10万次,特此截图留念
- 周期串
- 死锁定义、产生原因、必要条件、如何规避
- python判断文件和文件夹是否存在、创建文件夹
- 编程题目:PAT 1009. 说反话 (20)