杭電2019 數列有序!

注意一下如果插入的數相等!!
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,m;  while(cin>>n>>m)  {   int i,t;   if(m==n &&m==0)    return 0;   int *x;   x=new int[n];   for(i=0;i<n;i++)    cin>>x[i];   for(i=0;i<n;i++)   {    if(m<x[i])     break;   }   if(i==n)   {    for(i=0;i<n;i++)    {    cout<<x[i]<<' ';    }    cout<<m<<endl;   }   else   {    for(t=0;t<=i-1;t++)    {    cout<<x[t]<<' ';    }    cout<<m<<' ';    for(t=i;t<n-1;t++)    {    cout<<x[t]<<' ';    }    cout<<x[n-1]<<endl;   }   delete [] x;

   

 }  return 0; }   

發佈了57 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章