hdu 2018 數列有序!(java)

來源:互聯網
上載者:User

標籤:java   演算法   

問題:

再插入數字時,沒有中斷迴圈,導致從輸入數處再次迴圈,如下錯誤結果:

3 3

1 2 4

1 2 3 3

4 3

1 2 4 5

1 2 3 3 4

當迴圈完成後記得中斷;


數列有序! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 57301    Accepted Submission(s): 24693


Problem Description有n(n<=100)個整數,已經按照從小到大順序排列好,現在另外給一個整數x,請將該數插入到序列中,並使新的序列仍然有序。 
Input輸入資料包含多個測試執行個體,每組資料由兩行組成,第一行是n和m,第二行是已經有序的n個數的數列。n和m同時為0標示輸入資料的結束,本行不做處理。 
Output對於每個測試執行個體,輸出插入新的元素後的數列。 
Sample Input
3 31 2 40 0
 
Sample Output
1 2 3 4

代碼:

import java.util.*;public class Main{public static void main(String args[]){Scanner cin=new Scanner(System.in);while(cin.hasNext()){int n=cin.nextInt();int m=cin.nextInt();if(n==0&&m==0)break;else{int a[]=new int[100];for(int i=0;i<n;i++)a[i]=cin.nextInt();if(m>a[n-1])a[n]=m;else{for(int i=0;i<n;i++){if(m<a[i]){int j=i;for( i=n;i>j;i--)a[i]=a[i-1];a[i]=m;break;}}}for(int i=0;i<=n;i++)if(i==0)System.out.print(a[i]);elseSystem.out.print(" "+a[i]);}System.out.println();}}}




hdu 2018 數列有序!(java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.