你哪来这么多事(三):学生信息删除
學生信息刪除
Time Limit: 1 Sec??Memory Limit: 128 MB?? 64bit IO Format: %lld
Description
某班 有n個學生(不超過100),每個學生的信息包括學號(long no),姓名(char name[9])和成績(int score)。
編寫程序,先將n個學生的信息錄入到結構體數組中(n個學生的數據已按學號升序有序),
然后需要刪除m個學生。最后輸出剩下的所有學生的信息。
注意:main函數已經給定(如下所示)。
? ? ? ? ? ?請將程序補充完整。
? ? ? ? ? ?提交時只需要提交自己補充的代碼部分,不需要提交給定的main函數的代碼部分。
#include<stdio.h>
int main()
{int i,n,m,t;long xh;struct student stu[100];while(scanf("%d",&n)!=EOF){input(stu,n); //讀入n個學生的數據scanf("%d",&m);for(i=0;i<m;i++){scanf("%ld",&xh);t=delet(stu,n,xh); //刪除學號為xh的學生,刪除不成功返回-1,刪除成功則返回1if(t==-1)printf("delete Failed\n",xh);else{n--;printf("delete Success\n");}}print(stu,n); //輸出n個學生的信息}return 0;
}
Input
包含多組測試數據,每組測試數據第一行包含1個正整數n,表示學生人數。
接下來的n行,每行為1個學生的詳細信息(已按學號升序有序),包括學號,姓名和成績,各數據之間用空格隔開。
接下來的一行包括一個正整數m,表示待刪除的學生人數。
最后是m行,每行1個整數,表示待刪除學生的學號。
其中,n<100,m<n。
?
Output
每組測試數據輸出占多行。
首先前面m行為m個學生輸出是否成功的信息,如果刪除成功則輸出“delete Success”,否則輸出“delete Failed”。
最后輸出剩下所有學生的詳細信息,數據依次為:學號,姓名,成績。各部分數據之間用空格隔開。(具體詳見樣例輸出)
?
Sample Input
3 1001 aaa 86 1002 bbb 78 1003 ccc 70 2 1004 1001
?
Sample Output
delete Failed delete Success 1002 bbb 78 1003 ccc 70
題目分析:
會做第一題了就會做這一題。
不過插入和刪除的順序是反著來的,對吧?
搜索這個學號,搜到了則將這個學號之后的每個學生信息向前一位覆蓋。
搜不到則返回 -1 。
以下為完整版代碼=-=
你哪來這么多事【學生篇】完結,下一篇博客將圖解隊列插入……
#include<stdio.h>
struct student
{long no;char name[9];int score;
};
void input(struct student stu[100],int n)
{int i;for(i=1;i<=n;i++){scanf("%ld %s %d",&stu[i].no,stu[i].name,&stu[i].score);}return;
}
int delet(struct student stu[100],int n,long xh)
{int i,j;for(i=1;i<=n;i++)if(stu[i].no==xh){for(j=i+1;j<=n;j++)stu[j-1]=stu[j];return 1;}return -1;
}
void print(struct student stu[100],int n)
{int i;for(i=1;i<=n;i++)printf("%ld %s %d\n",stu[i].no,stu[i].name,stu[i].score);return;
}
int main()
{int i,n,m,t;long xh;struct student stu[100];while(scanf("%d",&n)!=EOF){input(stu,n); //讀入n個學生的數據scanf("%d",&m);for(i=0;i<m;i++){scanf("%ld",&xh);t=delet(stu,n,xh); //刪除學號為xh的學生,刪除不成功返回-1,刪除成功則返回1if(t==-1)printf("delete Failed\n");else{n--;printf("delete Success\n");}}print(stu,n); //輸出n個學生的信息}return 0;
}
?
總結
以上是生活随笔為你收集整理的你哪来这么多事(三):学生信息删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 世界上超过二百万人口的城市中国占多少
- 下一篇: 可疑人员检测报警系统费用大概要多少钱?