zoukankan      html  css  js  c++  java
  • 数列有序!



    Problem Description
    有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
     
    Input
    输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
     
    Output
    对于每个测试实例,输出插入新的元素后的数列。
     
    Sample Input
    3 3 1 2 4 0 0
     
          一开始用set,wa,可能是set维护成本吧。
     1 #include<bits/stdc++.h>
     2 #define LL long long
     3 using namespace std;
     4 int main()
     5 {
     6     int ans[110];
     7     int n,m;
     8     while(cin>>n>>m&&n+m)
     9     {
    10         for(int i=1;i<=n;++i)
    11             cin>>ans[i];
    12         int l=lower_bound(ans+1,ans+n+1,m)-ans;
    13         for(int i=n;i>=l;i--)
    14         {
    15             ans[i+1]=ans[i];
    16         }
    17         ans[l]=m;
    18         cout<<ans[1];
    19         for(int i=2;i<=n+1;++i)
    20             cout<<" "<<ans[i];
    21         cout<<endl;
    22 
    23     }
    24 }
    View Code
  • 相关阅读:
    文件路径选择中的三态逻辑
    .net版本号
    使用MSBuild编译vs多个解决方案
    CEF截图
    使用SharpZIpLib写的压缩解压操作类
    软件试用期设置
    list转datatable
    excel 导入
    网站登录简单验证码
    UEditor编辑器
  • 原文地址:https://www.cnblogs.com/Auroras/p/10798378.html
Copyright © 2011-2022 走看看