UVA 120 Stacks of Flapjacks ——思路题
#include #include #include #include using namespace std;int stacks[1000], operation[1000];int cnts = 0, cnto = 0;void solve(int lower_edge) { if (lower_edge <= 1) return; int maxp = 1, maxval = stacks[1]; for (int i = 2; i <= lower_edge; i++) { if (stacks[i] >= maxval) { maxval = stacks[i]; maxp = i; } } if (maxp != lower_edge) { if (maxp != 1) { operation[++cnto] = maxp; for (int i = 1, j = maxp; i < j; i++, j--) { swap(stacks[i], stacks[j]); } } operation[++cnto] = lower_edge; for (int i = 1, j = lower_edge; i < j; i++, j--) { swap(stacks[i], stacks[j]); } } solve(lower_edge - 1);}void output() { for (int i = 1; i <= cnto; i++) { printf("%d ", cnts - operation[i] + 1); } printf("0\n");}int main(){ int a; char c; while (scanf("%d%c", &a, &c) != EOF) { stacks[++cnts] = a; if (c == '\n') { printf("%d", stacks[1]); for (int i = 2; i <= cnts; i++) { printf(" %d", stacks[i]); } printf("\n"); solve(cnts); output(); cnts = 0, cnto = 0; } } return 0;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~