120 - Stacks of Flapjacks

题目看了半天......英语啊!!!

好久没做题...循环输入数字都搞了半天...罪过啊!!!

还是C方便一点...其实C++应该更方便的...C++得再看看!!!

#include <iostream>
#include <cstdio> using namespace std; /*翻转*/
void myReverse(int arr[],int s,int e)
{
while (s<e)
{
int temp=arr[s];
arr[s]=arr[e];
arr[e]=temp;
s++;
e--;
}
} int main(void)
{
int temp;
while (scanf("%d",&temp)!=EOF)
{
int myArr[];
myArr[]=temp;
int myCount=; while (getchar()!='\n')
{
cin>>myArr[myCount++];
} /*输出原始的*/
for (int i=;i<myCount-;i++)
{
cout<<myArr[i]<<" ";
}
cout<<myArr[myCount-]<<endl; for (int i=myCount-;i>=;i--) //针对此次最大的数
{
/*先找到此次最大数的位置*/
int thisMaxIndex=i; //注意记录的是下标
for (int j=i-;j>=;j--)
{
if (myArr[j]>myArr[thisMaxIndex])
{
thisMaxIndex=j;
}
} if(thisMaxIndex==i) //此次的最后一个已经是最大的了,不要操作
{
continue;
}
else if(thisMaxIndex==) //最大的第一个数,
{
cout<<(myCount-i)<<" "; //直接从后面翻一下,注意输出
myReverse(myArr,,i);
}
else
{
cout<<(myCount-thisMaxIndex)<<" ";
myReverse(myArr,,thisMaxIndex);
i++; //再继续此次循环
}
} cout<<"0\n"; //最后输出0;
}
return ;
}

总结:

1.while (scanf("%d",&temp)!=EOF)的循环输入判读

2.while (getchar()!='\n')  {  cin>>myArr[myCount++];} 看输入,读了一个数字之后紧接着要么是一个空格,要么是结尾的换行符,这里这代码漂亮!空格读了就不要了然后直接读数字,是换行就退出了循环,紧密结合了实际!

3.每次循环的处理,最外层是由后向前,保证处理过的都是有序的大的数.每次循环,要是不合要求再 i-- ,即再来这次循环!

UVaOJ 120 - Stacks of Flapjacks的更多相关文章

  1. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  2. uva 120 stacks of flapjacks ——yhx

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  3. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)

    题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...

  4. UVa 120 Stacks of Flapjacks【构造法】

    题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...

  5. UVA 120 Stacks of Flapjacks

    每次从最底部开始处理,如果不是最大值,则把最大值翻到底部.这就是最优解.原理自己模拟一下就好... 注意半径不是从1开始.数据处理要仔细. #include <iostream> #inc ...

  6. UVA - 120 Stacks of Flapjacks(煎饼)

    题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大). 分析:依次从锅底向上,优先排数字最大的煎饼.每次 ...

  7. uva Stacks of Flapjacks

                                                     Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     ...

  8. 【思维】Stacks of Flapjacks

    [UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...

  9. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

随机推荐

  1. [Angular 2] Directive intro and exportAs

    First, What is directive, what is the difference between component and directive. For my understandi ...

  2. HDU 4932 贪心

    Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. Nginx Rewrite 实现匹配泛域名规则

    Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru ...

  4. mysql 重要维护工具 图解

        下载地址: http://maatkit.org/get/mk-query-digest更多信息: http://maatkit.org/ | http://code.google.com/p ...

  5. PYTHON 源码阅读

    http://www.wklken.me/posts/2015/09/29/python-source-gc.html http://www.wklken.me/archives.html https ...

  6. QTP 中对象操作

    1.创建对象(将浏览器设置为一个对象) set oIE=createobject("internetexplorer.application") 2.将浏览器设置为可见的 oIE. ...

  7. VS 无法启动程序

    今天遇到这个问题网上找了很多资料,有很多人都遇到了这个问题,也有很多不同的答案,于是我们个答案都试了一遍都没有解决我这个问题的错误,于是就把控制面板中的打开功能和windows功能里面的Interne ...

  8. LeetCode 238

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  9. [改善Java代码]让工具类不可实例化

    建议42: 让工具类不可实例化 Java项目中使用的工具类非常多,比如JDK自己的工具类java.lang.Math.java.util.Collections等都是我们经常用到的.工具类的方法和属性 ...

  10. 实现TableLayout布局下循环取出TableRow控件中的文字内容到list集合

    布局方式为TableLayout,利于实现表单样式展现. <!-- 详情内容区域 --> <ScrollView android:layout_above="@id/id_ ...