测试数据有多组,第一行为样例数m。对于每个样例,第一行为学生人数n(n不超过20),接下来n行每行4个整数分别表示学号、姓名、性别和年龄,最后一行表示查询的学号。
输出m行,每行表示查询的学生信息,格式参见样例。
1 4 1 李江 男 21 2 刘唐 男 23 3 张军 男 19 4 王娜 女 19 2
2 刘唐 男 23
#include<iostream> #include<string> using namespace std; struct stu{ int no; string name; string gender; int age; }student[20]; int main(){ int m,n; cin>>m; while(m>0){ cin>>n; int refer; for(int i=0;i<n;i++) cin>>student[i].no>>student[i].name>>student[i].gender>>student[i].age; cin>>refer; cout<<student[refer-1].no<<' '<<student[refer-1].name<<' '; cout<<student[refer-1].gender<<' '<<student[refer-1].age<<endl; m--; } }
#include <bits/stdc++.h> using namespace std; struct student { int id; string name; string gender; int age; }; student arr[21]; bool compare(student a, student b) { return a.id < b.id; } int main() { int m = -1;//样例数 int n = -1;//学生人数 int num = -1;//查询的学号 while(cin >> m) { for (int i = 0; i < m; ++i) { cin >> n; for (int j = 1; j <= n; ++j) { cin >> arr[j].id >> arr[j].name >> arr[j].gender >> arr[j].age; } sort(arr + 1, arr + n + 1, compare); { cin >> num; int ceiling = n, bottom = 1; int mid = -1; while(bottom <= ceiling) { mid = bottom + (ceiling - bottom) / 2; if(arr[mid].id == num) break; else if(arr[mid].id < num) { bottom = mid + 1; } else { ceiling = mid - 1; } } cout << arr[mid].id <<' ' << arr[mid].name <<' ' << arr[mid].gender <<' ' << arr[mid].age<<endl; } } } return 0; }
#include<iostream> using namespace std; (807)#include<string> typedef struct Student{ int order; string name; string gender; int age; }Stu; #define maxsize 1000000 int main(){ int m; int n; int inquire; Stu student[21]; cin>>m; for(int i=0;i<m;++i){ cin>>n; for(int j=0;j<n;++j){ cin>>student[j].order>>student[j].name>>student[j].gender>>student[j].age; } cin>>inquire; for(int k=0;k<n;++k){ if(student[k].order==inquire){ cout<<student[k].order<<" "<<student[k].name<<" "<<student[k].gender<<" "<<student[k].age<<endl; break; } } } }
我认为此题的关键在于学生信息的存储。对此,我们可以采用类来处理。在之后的编写中,则要注意样例个数。可采用for循环处理
#include <stdio.h> (737)#include <stdlib.h> struct Student { int number; char name[100]; char sex[10]; //用例是英文存储的性别 int age; }; int main() { Student stu[20]; int z, n, temp; scanf("%d", &z); //吸取第一个没用的数字 while(~scanf("%d", &n)) { for(int i=0; i<n; i++) scanf("%d %s %s %d", &stu[i].number, stu[i].name, stu[i].sex, &stu[i].age); scanf("%d", &temp); for(int i=0; i<n; i++) //循环查询 { if(stu[i].number == temp) printf("%d %s %s %d\n", stu[i].number, stu[i].name, stu[i].sex, stu[i].age); } } }
#include<stdio.h> #include<vector> #include<string> using namespace std; typedef struct Student{ int id; string name; string sex; int age; }Student; int main(){ int N; int cycle; scanf("%d",&cycle); for(int turn=0;turn<cycle;turn++){ scanf("%d",&N); vector<Student> vec; Student s0; vec.push_back(s0); for(int i=0;i<N;i++){ int id,age; char name[100]={0},sex[10]={0}; scanf("%d %s %s %d",&id,name,sex,&age); Student s; s.id = id; s.name = name; s.sex = sex; s.age = age; vec.push_back(s); } int index; scanf("%d",&index); printf("%d %s %s %d\n",vec[index].id,vec[index].name.c_str(),vec[index].sex.c_str(),vec[index].age); } }
//针对元素多信息,比较单一信息的方法,避免繁琐的输入输出 #include <iostream> #include <string> using namespace std; int main(){ int m; cin >> m; while (m--){ int n; cin >> n; string stu[n]; getchar(); for(int i=0; i<n; ++i){ getline(cin, stu[i]); } string target; cin >> target; for(int i=0; i<n; ++i){ string id; for(int j=0; 1; ++j){ if(stu[i][j]==' ') break; id.push_back(stu[i][j]); } if(id == target){ cout << stu[i] << endl; break; } } } }
#include <cstdio> struct Student{ int Number; char Name[100]; char sex[20]; int age; }; int main(){ int m; scanf("%d",&m); for(int i = 0; i < m; ++i){ int n; scanf("%d",&n); Student stu[n+1]; for(int i = 1; i <= n; ++i){ scanf("%d %s %s %d", &stu[i].Number,&stu[i].Name,&stu[i].sex,&stu[i].age); } int id; scanf("%d",&id); printf("%d %s %s %d\n", stu[id].Number,stu[id].Name,stu[id].sex,stu[id].age); } return 0; }
#include<iostream> using namespace std; typedef struct Student { string name; string sex; int age; }; int main(void) { int m; cin >> m; Student arr[21]; while(m--) { int n; cin >> n; while(n--) { string name,sex; int num,age; cin >> num >> name >> sex >> age; arr[num].name = name; arr[num].sex = sex; arr[num].age = age; } int num_find; cin >> num_find; cout << num_find << ' ' << arr[num_find].name << ' ' << arr[num_find].sex << ' '; cout << arr[num_find].age << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { int n,m,q; int a,b; char c; string name; string sex; cin >> n; for(int i = 0; i < n; i++) { cin >> m; int id[m]; int age[m]; string NAME[m]; string SEX[m]; for (int j = 0; j < m; j++) { cin >> a >> name >> sex >> b; id[j] = a; NAME[j] = name; SEX[j] = sex; age[j] = b; } cin >> q; for(int k = 0;k < m;k++) { if(id[k] == q) { cout << id[k] << " " << NAME[k] << " " << SEX[k] << " " << age[k] << endl; } } } return 0; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { static class student { int id; String name; String gender; int age; public student(int id, String name, String gender, int age) { this.id = id; this.name = name; this.gender = gender; this.age = age; } @Override public String toString() { return id + " " + name + " " + gender + " " + age; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); for (int i = 0; i < n; i++) { int num = Integer.parseInt(br.readLine()); ArrayList<student> list = new ArrayList<>(); for (int j = 0; j < num; j++) { String[] s = br.readLine().split(" "); int id = Integer.parseInt(s[0]); String name = s[1]; String gender = s[2]; int age = Integer.parseInt(s[3]); student st = new student(id, name, gender, age); list.add(st); } int aim = Integer.parseInt(br.readLine()); System.out.println(list.get(aim - 1)); } } }
#include<stdio.h> #include <stdlib.h> struct Student { int num; char name[100]; char sex[10]; int age; }student[20]; int main() { int m,n,j,k; scanf("%d",&m); while(m--) { scanf("%d",&n); for(j=0;j<n;j++) scanf("%d %s %s %d",&student[j].num,student[j].name,student[j].sex,&student[j].age); scanf("%d",&k); for(j=0;j<n;j++) if(student[j].num==k) printf("%d %s %s %d",student[j].num,student[j].name,student[j].sex,student[j].age); } return 0; }
#include<stdio.h> struct Student{ int num; char name[20]; char sex[10]; int age; }stu[20]; int main() { int m,n,t,i,j; scanf("%d",&m); while(m--) { //输入 scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%s%s%d",&stu[i].num,&stu[i].name,&stu[i].sex,&stu[i].age); } scanf("%d",&t); printf("%d %s %s %d\n",stu[t-1].num,stu[t-1].name,stu[t-1].sex,stu[t-1].age); } return 0; }
Stu stus[20]; int i,j,m,n,index; scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d",&n); for(j=0;j<n;j++) { scanf("%d %s %s %d",&stus[j].num,&stus[j].name,&stus[j].sex,&stus[j].age); } scanf("%d",&index); for(j=0;j<n;j++) if(index==stus[j].num) { printf("%d %s %s %d\n",stus[j].num,stus[j].name,stus[j].sex,stus[j].age); break; } }
我这个在dev运行都没有错误啊? 为什么通过不了测试用例 /*查找学生信息*/ #include<cstdio> (802)#include<cstring> #include<iostream> using namespace std; const int maxn = 1010; //定义学生信息的结构体 struct stu{ int id; string name; string gender; int age; }buf[maxn]; //将考试的id号作为数组下标,进行统计 int main() { int m; cin>>m; //输入预查询的人数 int n; int id; int age; string name; string gender; cin>>n; for(int i = 0; i<n; i++) { cin>>id>>name>>gender>>age; buf[id].id = id; buf[id].name = name; buf[id].gender = gender; buf[id].age = age; } // int m; // cin>>m; //输入预查询的人数 for(int i = 0; i<m; i++) { cin>>id; cout<<buf[id].id<<" "<<buf[id].name<<" "<<buf[id].gender<<" "<<buf[id].age<<endl; } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 20 typedef struct stu { char no[5]; char name[15]; char sex[5]; int age; }STU; STU s[N]; int main() { int m, n; while(~scanf("%d", &n)) { for(int k = 0; k < n; k++) { scanf("%d", &m); memset(s, 0, N); for(int i = 0; i < m; i++) scanf("%s%s%s%d", s[i].no, s[i].name, s[i].sex, &(s[i].age)); char search[5]; scanf("%s", search); int tag = 0; for(int i = 0; i < m; i++) { if(!strcmp(search, s[i].no)) { tag = 1; printf("%s %s %s %d\n", s[i].no, s[i].name, s[i].sex, s[i].age); break; } } if(!tag) printf("No such student!\n"); } } return 0; }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); for (int i = 0; i < m; i++) { int n = scanner.nextInt(); HashMap<Integer, Student> map = new HashMap<>(); for (int j = 0; j < n; j++) { int id = scanner.nextInt(); map.put(id,new Student(id,scanner.next(),scanner.next(),scanner.nextInt())); } int search = scanner.nextInt(); System.out.println(map.get(search)); } } static class Student{ Integer id; String name; String sex; Integer age; public Student(Integer id, String name, String sex, Integer age) { this.id = id; this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return id+" "+name+" "+sex+" "+age; } } }
//用map哈希表,简单方便 #include<iostream> (720)#include<cstdio> #include<map> (747)#include<cstring> using namespace std; map<string,string>Student; int main(){ int m,n; cin>>m; while(m--){ cin>>n; getchar();//吃回车 for(int i=0;i<n;i++){ string str,key,value; getline(cin,str); int pos=str.find(" "); key=str.substr(0,pos); value=str.substr(pos+1); Student[key]=value; } string num; cin>>num; cout<<num<<" "<<Student[num]<<endl; } return 0; }