Codeup: Find student information

Title description

Enter the information of N students, and then inquire.

Input

The first line of input is N, that is, the number of students (N <= 1000). The
next N lines include the information of N students. The information format is as follows:
01 Li Jiangnan 21
02 Liu Tangnan 23
03 Zhang Junnan 19
04 Wang Nawen 19
Then Enter an M (M <= 10000), then there will be M rows, representing M queries, each row enter a student number, the format is as follows:
02
03
01
04

Output

Output M lines, each line includes information corresponding to the student being queried.
If there is no corresponding student information, output "No Answer!"

Sample input

5
001 Zhang Sanman 19
002 Li Siman 20
003 Wang Wuman 18
004 Zhao Liunv 17
005 Liu Qinv 21
7
003
002
005
004
003
001
006

Sample output

003 Wang
Wunan 18 002 Li Sinan 20
005 Liu Qinu 21
004 Zhao Liunu 17
003 Wang Wunan 18
001 Zhang Sannan 19
No Answer!

#include<stdio.h>
#include<string.h>
const int maxn=1000;
struct Student{
	char num[10];
	char name[20];
	char sex[10];
	int age;
}stu[maxn];

int main(){
	int n;

	while(scanf("%d",&n)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%s%s%s%d",stu[i].num,stu[i].name,stu[i].sex,&stu[i].age);
		}	
	char num1[100];	
	int m;
	int i;
		scanf("%d",&m);
		while(m--){
			scanf("%s",num1);	
			for(i=0;i<n;i++){
				if(!strcmp(stu[i].num,num1)){
				printf("%s %s %s %d\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age);
				break;
				}		
			}
			if(i==n) printf("No Answer!\n");	
			}
		}
	return 0;
}

First write the input. When searching, use the for loop to traverse all the stored information. Compare it with the input information using strcmp (). Note that strcmp () returns 0 when it is equal; greater than returns a positive number, and less than returns a negative number.
When the search fails: when i traverses all the data of 0-n-1 and it is still not found, then i = n, so when i = n, the student information is not found.
This question really left me exhausted. After an afternoon of doing it, it was rare to implement it. It is very correct to run the sample myself, but the test on codeup is the wrong answer. I have been looking for problems. Finally, I spent money to download the test data and source code. It is found that the length of the name column in the data is more than 70 digits, which is far beyond the size of the array I set. I should not consider this problem when using string. There is also a & in the printf and I have not noticed it because there is no error on the compiler. ,normal operation.
Here are two answers:

#include<string.h>
int maxsize=1003;
typedef struct{
  char number[1000];
  char name[200];
  char sex[200];
  int age;
}StudentInfo;
int main(){
  int n, m, i, j;
  char search[1000];
  StudentInfo stdinfo[maxsize];
  while(scanf("%d", &n)==1){
    for(i=0; i<n; ++i){
      scanf("%s%s%s%d",stdinfo[i].number,stdinfo[i].name, stdinfo[i].sex, &stdinfo[i].age);
    }
    scanf("%d", &m);
    for(i=0; i<m; ++i){
      scanf("%s",search);
      for(j=0; j<n; ++j){
        if(strcmp(search, stdinfo[j].number)==0){ 
          printf("%s %s %s %d\n",stdinfo[j].number,stdinfo[j].name, stdinfo[j].sex, stdinfo[j].age);
          break;
        }
      }
      if(j==n) printf("No Answer!\n");
    }
  }
  return 0;
}

#include<string.h>

struct STU
{
	char id[100],name[100],sex[100];
	int age;
}stu[1000];

int main()
{
	int n,m,i;
	char s[100];
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%s%s%s%d",stu[i].id,stu[i].name,stu[i].sex,&stu[i].age);
		scanf("%d",&m);
		while(m--)
		{
			scanf("%s",s);
			for(i=0;i<n;i++){
			
				if(!strcmp(stu[i].id,s))
				{
					printf("%s %s %s %d\n",stu[i].id,stu[i].name,stu[i].sex,stu[i].age);
					break;
				}}
			if(i==n)
				puts("No Answer!");
		}
	}
	return 0;
}
Published 19 original articles · Likes2 · Visits 752

Guess you like

Origin blog.csdn.net/zan1763921822/article/details/104130259