您的位置:首页 > 其它

HDU 2019 数列有序!

2013-07-10 11:34 253 查看
数列有序!
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 34123    Accepted Submission(s): 14555


Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
 
 
Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
 
 
Output
对于每个测试实例,输出插入新的元素后的数列。
 
 
Sample Input
3 3
1 2 4
0 0
 
 
Sample Output
1 2 3 4
 

 

 

代码如下:

#include<iostream>

using namespace std;

 

int main()

{

         int n;

         int m;

         int str[102];

         int str1[102];

         while(cin>>n>>m)

         {

                   if(n==0 && m==0)

                   {

                            break;

                   }

                   else

                   {

                            int pos=0;

                            int i;

                            for(i=0; i<n; i++)

                            {

                                     cin>>str[i];

                            }

                            for(i=0; i<n+1; i++)

                            {

                                     if(m>str[i])

                                     {

                                               str1[i]=str[i];

                                     }

                                     else

                                     {

                                               pos=i;

                                               str1[pos]=m;

                                               break;

                                    

                                     }

                            }

                            for(i=pos; i<n+1; i++)

                            {

                                     str1[i+1]=str[i];

                            }

                            for(i=0; i<n+1; i++)

                            {

                                     cout<<str1[i];

                                     if(i<n)

                                               cout<<' ';

                            }

                   }

                   cout<<endl;

         }

         return 0;

}

虽然一次通过,这里要注意的是输出格式问题。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: