(挑战编程_4_2)Stacks of Flapjacks
来源:互联网 发布:mac系统升级10.10下载 编辑:程序博客网 时间:2024/06/06 20:18
http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110402&format=html
#include <iostream>#include <cstdlib>#include <vector>#include <set>#include <string>#include <fstream>#include <algorithm>#include <sstream>#include <string.h>#include <cassert>using namespace std;//#define _MY_DEBUG_// 存放饼int cake[40];// 排好序的int ans[40];// 临时数组int tmp[40];/*MainStructure: 主结构*/void MainStructure();/*Process:处理每一组数据*/void Process();/*从start到end,比较两个数组的数是否相同包括start、不包括end*/bool IsArrEqual(int start, int end);/*从step开始,到end,找到cake中直径最大的数的index*/bool FindMaxFromArr(int start, int end, int &maxIndex);/*翻转index处的数:1.先将index翻转到最顶部,然后再翻转到最底部*/void FlipNum(int start, int count);/*增加step的值*/void IncreaseStep(int &step, int count);/*翻转整数数组*/void SwapIntArr(int *arr, int start, int end);int main(){MainStructure();return 0;}void MainStructure(){#ifdef _MY_DEBUG_string inputStr = "C:/Users/cao/Desktop/算法/要看的书/算法/2挑战编程/input/1.inp";//string inputStr = "D:/2挑战编程/input/110205.inp";string outputStr = "C:/Users/cao/Desktop/算法/要看的书/算法/2挑战编程/input/0000001.outp";ifstream fin;fin.open(inputStr);cin.rdbuf(fin.rdbuf());//ofstream fout;//fout.open(outputStr);//cout.rdbuf(fout.rdbuf());#endifProcess();}void Process(){string str;while (getline(cin, str)){stringstream ss(str);int count = 0;int num = 0;while (ss >> num){cake[count++] = num;}// 显示第一行cout << str << endl;SwapIntArr(cake, 0, count);memcpy(ans, cake, sizeof(int) * count);// 从小到大排序sort(ans, ans + count);// 懒得写比较函数,直接翻转吧!使之变成从大到小排序SwapIntArr(ans, 0, count);int step = 0;// 如果没有排好序IncreaseStep(step, count);while (step < count){// 将index翻转的最顶部,然后翻转的step处FlipNum(step, count);// step一次不一定只增加1!IncreaseStep(step, count);}cout << "0" << endl;}}bool FindMaxFromArr(int start, int end, int &maxIndex){if (start >= end){maxIndex = -1;return false;}maxIndex = end - 1;int max = cake[end - 1];for (int i = end - 2; i >= start; --i){if (cake[i] > max){max = cake[i];maxIndex = i;}}// 如果是第一个if (maxIndex == end - 1){for (int i = end - 2; i >= start; --i){if (cake[i] == max){--maxIndex;}else{break;}}return true;}return false;}void FlipNum(int start, int count){if (start >= count){return;}int index;// 从step开始,到end,找到cake中直径最大的数的indexbool isOK = FindMaxFromArr(start, count, index);// 1.将index翻转到最顶部if (!isOK){SwapIntArr(cake, index, count);cout <<(index + 1) << " ";}// 2.此时最大值已经在顶部,翻转从start到count的数即可SwapIntArr(cake, start, count);cout <<(start + 1) << " ";}void IncreaseStep(int &step, int count){for (int i = step; i < count; ++i){if (cake[i] == ans[i]){++step;}else{break;}}}void SwapIntArr(int *arr, int start, int end){int middle = start + (end - start) / 2;for (int i = start, j = 1; i < middle; ++i, ++j){swap(arr[i], arr[end - j]);}}
- (挑战编程_4_2)Stacks of Flapjacks
- uva 120 Stacks of Flapjacks(检索)
- uva 120 Stacks of Flapjacks(模拟)
- UVa 120 - Stacks of Flapjacks (排序)
- 煎饼(Stacks of Flapjacks,UVA120)
- UVA 120 - Stacks of Flapjacks(构造)
- UVA120:Stacks of Flapjacks(煎饼)
- UVA 120 Stacks of Flapjacks (煎饼)
- Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- uva120 stacks of flapjacks
- uva120 Stacks of Flapjacks
- uva120 Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 110402 Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- android apk反编译和odex转dex
- 文本挖掘
- lucene-索引文件格式
- linux内核input驱动按键
- W5100E01-AVR(W5100评估板)演示手册(二)
- (挑战编程_4_2)Stacks of Flapjacks
- sigcation 用法
- PHP 发送Http Header 404及301\302重定向
- RegOpenKeyEx在64位与32位系统编程注意事项
- HyperWorks 集成桌面环境下二次开发【四】----脚本实例
- J2EE中的13种技术规范
- php header跳转及函数详细说明
- (挑战编程_4_3)Bridge
- php 五大运行模式 CGI FAST-CGI CLI ISAPI APACHE模式