1145: 零起點學算法52——數組中刪數II


1145: 零起點學算法52——數組中刪數II

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

Description

在給定的數組中刪除數

 

Input

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

 

Output

 

刪除在第一行的n個整數中的數字m(多個的話都要刪除),然后按照順序輸出剩下的數。如果該數組中所有數均被刪除,請直接輸出換行

 

Sample Input

 
5 1 2 3 4 3
3

 

Sample Output

1 2 4

 

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,cout=0;
10         scanf("%d",&m);
11         for(int i=0;i<n;i++){
12             if(m==a[i]){
13                 t=a[i];
14                 a[i]=a[i+1];
15                 a[i+1]=t;
16                 cout++;
17             }
18         }
19         
20         for(int i=0;i<n-cout;i++){
21             printf("%d ",a[i]);
22         }
23         printf("%d\n",a[n-cout]);
24     }
25     return 0;
26 }

 

改正后AC代碼:

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

 


免責聲明!

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



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