查询成绩(要求用链表完成)
生活随笔
收集整理的這篇文章主要介紹了
查询成绩(要求用链表完成)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
知道如何創建鏈表了。
本題繼續要求用鏈表完成。輸入同學成成績后,現在再輸入1個同學的姓名,輸出該同學的成績。
Input
輸入一些學生的信息,每個學生信息一行,分別為學號、姓名和成績,中間用空格隔開,其中學號和成績均為整數,姓名為不超過15個僅包含大小寫字母的字符。
如果輸入的一行是非正整數時,表示結束
在成績結束后,輸入一行,為一個同學的姓名
Output
輸出一個整數,表示該同學的成績
Sample Input
1001 xiangwang 90
1002 xiaoli 85
1003 xiaohong 97
1004 xiaoma 76
-1
xiaoli
Sample Output
85
本題某種程度上說很死板,我很確定代碼沒錯,但就是超時,而代碼已經縮減到我能做到的最少了,如果堅持用cin輸入基本就會超時,只能用scanf。但讓我獲得了一個很好的知識點,在c++中cin做的操作比scanf要來的多。
#include<iostream> #include<malloc.h> #include<cstring> using namespace std; struct node {int score;char name[15];struct node *next; };//struct結構體 struct node * Create_Stu_Doc() {struct node *head,*tail,*p;//頭尾和p指針int num,score;char name[15];head = tail =NULL;scanf("%d", &num);while(num > 0) {scanf("%s%d",name,&score);p = (struct node *) malloc(sizeof(node));p->score=score;strcpy(p->name,name);p->next = NULL;if(head==NULL) head=p;//這邊還能再簡化但代碼更繁瑣,能通過就不改了else tail->next=p;tail = p;scanf("%d", &num);}return head; }; int main() {struct node *head,*p;char name[15];head=Create_Stu_Doc();scanf("%s",name);for(p=head; p!=NULL; p=p->next) {if(strcmp(p->name,name)==0) {printf("%d",p->score);}}return 0; }總結
以上是生活随笔為你收集整理的查询成绩(要求用链表完成)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java oauth2登录以及权限_还得
- 下一篇: 虚拟专题:联邦学习 | 联邦学习算法综述