zoukankan      html  css  js  c++  java
  • UVA 11925

    题意:

      给出一个1到n的排列,给出操作顺序,使升序排列能变为所给排列。

    分析:

      正常冒泡排序的想法。如果前两个数,前面的大于后面的,则换(特例是n,1不能换)。否则,就用2的逆操作,把最后的数放前面。不过用了vector数组存放

    代码:

      

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int n;
    vector<int> a,ans;
    int main()
    {
    int i,j;
    while(~scanf("%d",&n)&&n)
    {
    a.clear();
    ans.clear();
    for(int i=0;i<n;++i)
    {
    int k;
    scanf("%d",&k);
    a.push_back(k);
    }
    while(1)
    {
    if(a[0]==1)
    {
    bool ok=true;
    for(i=0;i<n;++i)
    if(a[i]!=i+1)
    {
    ok=false;
    break;
    }
    if(ok)
    break;
    }
    if(a[0]<a[1]||(a[1]==1&&a[0]==n))
    {
    ans.push_back(2);
    a.insert(a.begin(),a[n-1]);
    a.erase(a.end()-1);
    }
    else
    {
    ans.push_back(1);
    swap(a[0],a[1]);
    }
    }
    for(i=ans.size()-1;i>=0;--i)
    printf("%d",ans[i]);
    printf(" ");
    }
    return 0;
    }
  • 相关阅读:
    2019.10.07题解
    2019.10.06题解
    2019.10.05'题解
    2019.10.05题解
    java邮件发送
    注释类型 XmlType
    Spring 注解
    @SuppressWarnings(unchecked)作用解释
    vm文件
    Apache Shiro 使用手册(一)Shiro架构介绍
  • 原文地址:https://www.cnblogs.com/137033036-wjl/p/4928538.html
Copyright © 2011-2022 走看看