zoukankan      html  css  js  c++  java
  • uva120

    题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,

    进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小)。输出需要翻转

    的次数,如果已经结束不需要翻转输出0。

    max_element(first,last)输出最大元素的位置

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<sstream>
    using namespace std;
    int n;
    int a[10005];
    string s;
    void solve(int p)
    {
        for(int i=0;i<p-i;i++)
        {
            swap(a[i],a[p-i]);
        }
        cout<<n-p<<" ";
    }
    int main()
    {
        while(getline(cin,s))
        {
            cout<<s<<endl;
            stringstream ss(s);
            n=0;
            while(ss>>a[n])
                n++;
            for(int i=n-1;i>0;i--)
            {
                int p=max_element(a,a+i+1)-a;//查找最大元素的位置 
                if(p==i)//如果=i则说明它在自己的位置上 
                    continue;
                if(p>0)//否则将最大元素翻转到顶部 
                    solve(p);
                solve(i);//翻转到符合条件的位置 
            }
            cout<<"0"<<endl;
        }
    }
  • 相关阅读:
    UVA 10056 What is the Probability ?
    Reporting Services Report 带参数
    頁面刷新後,滾動條位置保持不變
    use this as the default and do not ask again
    JQuery Tab 滑动们导航菜单效果
    poj3256
    poj2060
    poj3280
    poj3261
    poj2135
  • 原文地址:https://www.cnblogs.com/renwjing/p/7397376.html
Copyright © 2011-2022 走看看