1144: 零起點學算法51——數組中刪數


1144: 零起點學算法51——數組中刪數

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 3304  Accepted: 933
[Submit][Status][Web Board]

Description

在給定的數組中刪除一個數 

 

Input

多組測試,每組第一行輸入1個整數n(n<20),然后是n個整數
第二行輸入1個整數m 

 

Output

刪除在第一行的n個整數中第一次出現數字m並刪除,然后按照順序輸出剩下的數,

 

Sample Input

 
4 1 2 3 4
3

 

Sample Output

1 2 4

 

HINT

 

m有可能在原數組中找不到,找不到則輸出原數組


 

 

Source

 
 1 #include<stdio.h>
 2 int main(){
 3     int n,a[20];
 4     while(scanf("%d",&n)!=EOF){
 5         for(int i=0;i<n;i++){
 6             scanf("%d",&a[i]);
 7         }
 8         
 9         int m,t,sign=1;
10         scanf("%d",&m);
11         for(int i=0;i<n;i++){
12             if(m==a[i]){
13                 t=i;
14                 sign=0;
15                 break;
16             }
17         }
18         
19         if(sign){
20             for(int i=0;i<n-1;i++){
21                 printf("%d ",a[i]);
22             }
23             printf("%d\n",a[n-1]);
24         }
25         else{
26             for(int i=0;i<t;i++){
27                 printf("%d ",a[i]);
28             }
29             for(int j=t+1;j<n-1;j++){
30                 printf("%d ",a[j]);
31             } 
32             printf("%d\n",a[n-1]);
33         } 
34     }
35     return 0;
36 }

//感覺有問題,如果刪除的數是最后一個?? 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM