UVA 120 - Stacks of Flapjacks
来源:互联网 发布:淘宝购物车加不进去 编辑:程序博客网 时间:2024/05/16 05:22
输入数字是以回车结束每一行,注意只输入一个数字情况。感觉这道题答案应该不唯一。下面代码是从后往前搜索,如果在相应位置就过去;如果不在就交换到第0位置,然后在做一次交换到相应位置。
#include <stdio.h>#include <stdlib.h>#include <string.h>int cmp(const void *_a, const void *_b) { int* a = (int *)_a; int* b = (int *)_b; return *a - *b;}void swap(int a[], int i, int j) { while (i < j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; i++; j--; }}int main() { int a[35], b[35], n; while (scanf("%d", &a[0]) != EOF) { // 接收输入的数字 n = 1; if (getchar() != '\n') while (scanf("%d", &a[n++])) if (getchar() == '\n') break; // 输出原先的序列并复制一份给b数组 for (int i=0; i<n; i++) { b[i] = a[i]; printf("%d ", a[i]); } printf("\n"); // 对b数组进行排序 qsort(b, n, sizeof (int), cmp); // 从后往前遍历a数组 for (int i=n-1; i>=0; i--) { // 如果a[i]的值在排好序的位置就不执行下面的for循环 if (a[i] == b[i]) continue; for (int j=i-1; j>=0; j--) { // 如果a[i]的值与位置不符就从前面找到该位置的数 if (b[i] == a[j]) { // 如果该数在第0个位置就直接交换 if (j == 0) { printf("%d ", n - i); swap(a, 0, i); } // 如果不在第0个位置就先交换到第0位置再交换到相应位置 else { printf("%d ", n - j); swap(a, 0, j); printf("%d ", n - i); swap(a, 0, i); } } } } printf("0\n"); } return 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 120Stacks 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
- AntiPattern: Batch Code Review
- Linux 内核模块 编程
- Wince6 RIL层移植
- matlab tree 结构
- 单线程模型中Message、Handler、Message Queue、Looper之间的关系
- UVA 120 - Stacks of Flapjacks
- Hadoop RPC源码分析之Client
- 蒙提霍尔问题&箱子问题
- jffs2文件系统——MTD驱动挂载
- CLisp 28:调用C程序详细指导
- Ubuntu 10.04 搭建Android开发环境
- GPRS管理与创建APN拨号连接
- 井深结构图
- 回文串判断(先错后对,OJ系统好严格啊)