UVa 120 - Stacks of Flapjacks
2012-08-23 10:49
381 查看
翻饼问题,注意一下几点:
1、翻饼规则:只能从底层往上翻,也就是将前i个元素reverse,所以这里有个技巧,就是每次找到前i个元素里面最大的元素(假设是第m个),将其翻到第一层(前m个元素reverse),然后再翻到第i层(前i个元素reverse)(翻回来的时候一定是输出n-i,不是i)
2、输出的时候一定要输出输入行
1、翻饼规则:只能从底层往上翻,也就是将前i个元素reverse,所以这里有个技巧,就是每次找到前i个元素里面最大的元素(假设是第m个),将其翻到第一层(前m个元素reverse),然后再翻到第i层(前i个元素reverse)(翻回来的时候一定是输出n-i,不是i)
2、输出的时候一定要输出输入行
#include <stdio.h> #include <stdlib.h> #include <string.h> int max(int a[],int n) { int i,max=a[0]; for(i=1; i<n; i++) { if(a[i]>max) max=a[i]; } return max; } int find(int a[],int n,int find) { int i; for(i=0; i<n; i++) { if(a[i]==find) break; } return i; } void flap(int l,int r,int a[]) { for(int j=l; j<r/2; j++) { int temp=a[j]; a[j]=a[r-1-j]; a[r-1-j]=temp; } } int main() { int a[40],flag,f,m,count=0; char t; while(scanf("%d%c",&a[count],&t)==2) { count++; if(t=='\n') { for(int i=0; i<count; i++) { if(!i) printf("%d",a[i]); else printf(" %d",a[i]); } printf("\n"); for(int i=0; i<count; i++) { if(!i) { flag=0; for(int j=0; j<count-1; j++) { if(a[j]>a[j+1]) flag++; } if(flag==0) { printf("0\n"); memset(a,0,count); count=0; break; } } m=max(a,count-i); f=find(a,count-i,m); if(!f) { flap(0,count-i,a); printf("%d ",i+1); } else if(f!=count-i-1) { flap(0,f+1,a); printf("%d ",count-f); flap(0,count-i,a); printf("%d ",i+1); } if(i) { flag=0; for(int j=0; j<count-1; j++) { if(a[j]>a[j+1]) flag++; } if(!flag) { printf("0\n"); memset(a,0,sizeof(a)); count=0; break; } } } } } return 0; }
相关文章推荐
- UVa 120 - Stacks of Flapjacks
- Stacks of Flapjacks 翻煎饼-UVA120 白书第五章
- Uva 120 Stacks of Flapjacks
- uva 120 Stacks of Flapjacks 简单构造
- UVA - 120 Stacks of Flapjacks
- uva 120 - Stacks of Flapjacks
- UVA 120 Stacks of Flap jacks
- UVa 120 (构造) Stacks of Flapjacks
- UVa 120 Stacks of Flapjacks (STL deque&reverse)
- UVA120 Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks (排序)
- uva 120 stacks of flapjacks ——yhx
- UVa 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks 煎饼
- Uva120——Stacks of Flapjacks
- uva 120 stacks of flapjacks ——yhx
- UVA 120 - Stacks of Flapjacks
- uva120 stacks of flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks