题目1069:查找学生信息
题目1069:查找学生信息
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:18114
解决:4872
- 题目描述:
-
输入N个学生的信息,然后进行查询。
- 输入:
-
输入的第一行为N,即学生的个数(N<=1000)
接下来的N行包括N个学生的信息,信息格式如下:01 李江 男 2102 刘唐 男 2303 张军 男 1904 王娜 女 19然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:02030104
- 输出:
-
输出M行,每行包括一个对应于查询的学生的信息。
如果没有对应的学生信息,则输出“No Answer!”
- 样例输入:
-
4 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 5 02 03 01 04 03
- 样例输出:
-
02 刘唐 男 23 03 张军 男 19 01 李江 男 21 04 王娜 女 19 03 张军 男 19
- 答疑:
- 解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7793-1-1.html
- 不知道为什么最开始自己写的代码,总是错误,我也很无奈,明明都差不多的。。
- 我的错误代码
-
#include <stdio.h> #include <algorithm> #include <set> #include <vector> #include <string> #include <string.h> #include<iostream> using namespace std ; struct node { char name[20]; char sex[10]; int age; char num[20]; }stu[1010]; int main( ) { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%s %s %s %d",stu[i].num,stu[i].name,stu[i].sex,&stu[i].age); int m; scanf("%d",&m); while(m--) { char str[20]; scanf("\n%s",str); int flag=0,num; for(int i=0;i<n;i++) { if(strcmp(str,stu[i].num)==0) { num = i; flag = 1; break; } } if(flag) printf("%s %s %s %d\n",stu[num].num,stu[num].name,stu[num].sex,stu[num].age); else printf("No Answer!\n"); } return 0 ; }
后来看了别人的博客,用了动态数组就对了。。。
#include <stdio.h> #include <algorithm> #include <set> #include <vector> #include <string> #include <string.h> #include<iostream> using namespace std ; struct node { string name; string sex; int age; string num; }; int main( ) { int n; while(cin >> n) { vector<struct node> stu(n); for(int i=0; i<n; i++) cin >> stu[i].num >> stu[i].name >> stu[i].sex >> stu[i].age; int m; cin >> m; while(m--) { string str; cin >> str; int flag=0,num1; for(int i=0; i<n; i++) { if(str==stu[i].num) { num1 = i; flag = 1; break; } } if(flag) cout << stu[num1].num << " " << stu[num1].name << " " << stu[num1].sex << " " << stu[num1].age << endl; else cout << "No Answer!" << endl; } } return 0 ; }
别人的不用动态数组AC的代码
#include<stdio.h> #include<string.h> struct node{ char num[100]; char name[100]; char sex[10]; int age; }stu[1005]; int main() { int i,j,m,n; char s[10005][100]; while(scanf("%d",&n)!=EOF){ for(i=0;i<n;i++){ scanf("%s %s %s %d",stu[i].num,stu[i].name,stu[i].sex,&stu[i].age); } scanf("%d",&m); for(i=0;i<m;i++) scanf("%s",s[i]); for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(strcmp(stu[j].num,s[i])==0){ printf("%s %s %s %d\n",stu[j].num,stu[j].name,stu[j].sex,stu[j].age); break; } } if(j==n) printf("No Answer!\n"); } } return 0; }
彼时当年少,莫负好时光。