3.2 Find elements

/*
Question A: Count the number of students with the same grades
Topic description
Read in the grades of N students and output the number of students who get a given score.

enter
The test input contains several test cases, each in the format of
Line 1: N
Line 2: The grades of N students, separated by a space between two adjacent numbers.
Line 3: given score
The input ends when N=0 is read. Where N does not exceed 1000, and the grade point is an integer between 0 and 100 (inclusive).
output
For each test case, the output of the number of students who received a given score.

sample input
4
70 80 90 100
80
3
65 75 85
55
5
60 90 90 90 85
90
0
Sample output
1
0
3
*/

#include<cstdio>
int main(){
	int N;
	while(scanf("%d",&N)!=EOF){
		int a[N];
		if(N==0) break;
		for(int i=0;i<N;i++){
			scanf("%d",&a[i]);
		}
		int x;
		scanf("%d",&x);
		int count=0;
		for(int i=0;i<N;i++){
			if(a[i]==x) count++;
		}
		printf("%d\n",count);
	}
}
/*
Problem B: Find x
Topic description
Input a number n, then input n different values, and then input a value x, output the subscript of this value in this array (starting from 0, if it is not in the array, output -1).

enter
There are multiple sets of test data, enter n (1<=n<=200), then enter n numbers, and then enter x.
output
For each set of inputs, output the result.

sample input
4
1 2 3 4
3
Sample output
2
*/

#include<cstdio>
const int maxn = 210;
int a[maxn];
int main(){
	int n,x;
	while(scanf("%d",&n)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%d",&x);
		int k;
		for(k=0;k<n;k++){
			if(a[k]==x){
				printf("%d\n",k);
				break;
			}
		}
		if(k==n){
		printf("-1\n");
		}
	}
	return 0;
}
/*
Question C: Find Student Information
Topic description
Enter the information of N students, and then make a query.

enter
The first line of input is N, that is, the number of students (N<=1000)
The next N lines contain the information of N students in the following format:
01 Li Jiang male 21
02 Liu Tang male 23
03 Zhang Junnan 19
04 Wang Na female 19
Then enter an M (M<=10000), and then there will be M lines, representing M queries, and enter a student number in each line. The format is as follows:
02
03
01
04
output
Output M lines, each line containing a piece of information corresponding to the queried student.
If there is no corresponding student information, output "No Answer!"

sample input
5
001 Zhang Sannan 19
002 Li four male 20
003 King Five Men 18
004 Zhao Liunv 17
005 Liu Qinu 21
7
003
002
005
004
003
001
006
Sample output
003 King Five Men 18
002 Li four male 20
005 Liu Qinu 21
004 Zhao Liunv 17
003 King Five Men 18
001 Zhang Sannan 19
No Answer!
*/

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

struct info{
	char number[1010];
	char name[1010];
	char sex[2];
	int age;
}stu[1010];


int main(){
	int n,m,i,j;
	while(scanf("%d",&n)!=EOF){
		memset(stu,0,sizeof(stu));
		for(i=0;i<n;i++){
			//scanf("%s %s %s %d",stu[i].number,stu[i].name,stu[i].sex,&stu[i].age);
			cin>>stu[i].number>>stu[i].name>>stu[i].sex>>stu[i].age;
		}
		//scanf("%d",&m);
		cin>>m;
		char x[1010];
		for(i=0;i<m;i++){
			scanf("%s",x);
			for(j=0;j<n;j++){
				if(strcmp(x,stu[j].number)==0){
					//printf("%s %s %s %d\n",stu[j].number,stu[j].name,stu[j].sex,stu[j].age);
					cout<<stu[j].number<<" "<<stu[j].name<<" "<<stu[j].sex<<" " <<stu[j].age<<endl;
					break;
				}
			}
			if(j==n){
				printf("No Answer!\n");
			}
		}
	}
	return 0;
}
/*
Problem D: Find
Topic description
input array length n
input array a[1...n]
Enter the search number m
Enter the search number b[1...m]
Output YES or NO to find YES otherwise NO.

enter
Enter multiple sets of data.
Enter n for each group, then n integers, then m, then m integers (1<=m<=n<=100).
output
Output YES if in n arrays else output NO.

sample input
6
3 2 5 4 7 8
2
3 6
Sample output
YES
NO
*/

#include<cstdio>
int main(){
	int n,m,i,j;
	while(scanf("%d",&n)!=EOF){
		//printf("%d\n",n);
		int a[n];
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			int x;
			scanf("%d",&x);
			for(j=0;j<n;j++){
				if(a[j]==x){
					printf("YES\n");
					break;
				}
			}
			if(j==n){
				printf("NO\n");
			}
		}
	}		
	return 0;
}
/*
Question E: Student Inquiries
Topic description
Enter the information of n students, each line includes student number, name, gender and age, and each attribute is separated by a space. Finally, enter a student ID, and output the student information corresponding to the student ID.

enter
There are multiple sets of test data, and the first row is the number of samples m. For each example, the first line is the number of students n (n is not more than 20), and the next n lines are four integers representing student ID, name, gender and age, respectively, and the last row is the student ID of the query.
output
Output m lines, each line represents the queried student information, see the example for the format.

sample input
1
4
1 Li Jiang male 21
2 Liu Tang male 23
3 Zhang Junnan 19
4 Wang Na female 19
2
Sample output
2 Liu Tang male 23
*/

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

struct info{
	string number;
	string name;
	string sex;
	int age;
}stu[30];


int main(){
	int m,n;
	string x;
	while(scanf("%d",&m)!=EOF){
		for(int i=0;i<m;i++){
			cin>>n;
			for(int j=0;j<n;j++){
				cin>>stu[j].number>>stu[j].name>>stu[j].sex>>stu[j].age;	
			}
			cin>>x;
			for(int k=0;k<n;k++){
				if(x==stu[k].number){
					cout<<stu[k].number<<" "<<stu[k].name<<" "<<stu[k].sex<<" "<<stu[k].age<<endl;
					break;
				}
			}
		}
	}
	return 0;
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325693321&siteId=291194637