1934: 找x
时间限制: 1 Sec 内存限制: 32 MB
题目描述
输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
输入
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
输出
对于每组输入,请输出结果。
样例输入
4
1 2 3 4
3
样例输出
2
#include<iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
int a[200] = { 0 };
for (int i = 0;i < n;i++) {
cin >> a[i];
}
int x, t = -1;
cin >> x;
for (int i = 0;i < n;i++) {
if (a[i] == x) t = i;
}
cout << t<<endl;
}
}
问题 A: 统计同成绩学生人数
时间限制: 1 Sec 内存限制: 32 MB
题目描述
读入N名学生的成绩,将获得某一给定分数的学生人数输出。
输入
测试输入包含若干测试用例,每个测试用例的格式为
第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数
当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。
输出
对每个测试用例,将获得给定分数的学生人数输出。
样例输入
4
70 80 90 100
80
3
65 75 85
55
5
60 90 90 90 85
90
0
样例输出
1
0
3
#include<iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
if (n == 0) return 0;
else {
int a[1000] = { 0 }, s, num = 0;
for (int i = 0;i < n;i++) {
cin >> a[i];
}
cin >> s;
for (int i = 0;i < n;i++) {
if (a[i] == s) num++;
}
cout << num << endl;
}
}
}
问题 B: 找x
时间限制: 1.000 Sec 内存限制: 32 MB
题目描述
输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
输入
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
输出
对于每组输入,请输出结果。
样例输入 Copy
4
1 2 3 4
3
样例输出 Copy
2
#include<iostream>
using namespace std;
int main() {
int n, x;
while (cin >> n) {
int a[200] = { 0 }, s = -1;
for (int i = 0;i < n;i++) {
cin >> a[i];
}
cin >> x;
for (int i = 0;i < n;i++) {
if (a[i] == x) {
s = i;
cout << s << endl;
}
if (i == n - 1 && s == -1) {
cout << s << endl;
}
}
}
}
问题 C: 查找学生信息
时间限制: 1.000 Sec 内存限制: 32 MB
题目描述
输入N个学生的信息,然后进行查询。
输入
输入的第一行为N,即学生的个数(N<=1000)
接下来的N行包括N个学生的信息,信息格式如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
02
03
01
04
输出
输出M行,每行包括一个对应于查询的学生的信息。
如果没有对应的学生信息,则输出“No Answer!”
样例输入 Copy
5
001 张三 男 19
002 李四 男 20
003 王五 男 18
004 赵六 女 17
005 刘七 女 21
7
003
002
005
004
003
001
006
样例输出 Copy
003 王五 男 18
002 李四 男 20
005 刘七 女 21
004 赵六 女 17
003 王五 男 18
001 张三 男 19
No Answer!
//50分到底哪里有问题呢
#include<iostream>
#include<cstring>
using namespace std;
typedef struct stu {
char sno[20];
char name[20];
char sex[20];
int age;
};
int main() {
int n,m;
stu s[1001];
char a[10000][20] = { 0 };
cin >> n;
for (int i = 0;i < n;i++) {
cin >> s[i].sno >> s[i].name >> s[i].sex >> s[i].age;
}
cin >> m;
for (int i = 0;i < m;i++) {
cin >> a[i];
}
for (int i = 0;i < m;i++) {
int f = 0;
for (int j = 0;j < n;j++) {
if (strcmp(s[j].sno,a[i])==0) {
f = 1;
cout << s[j].sno<<" "<<s[j].name << " " << s[j].sex << " " << s[j].age << endl;
break;
}
else if (f == 0 && j == n - 1) {
cout << "No Answer!" << endl;
}
}
}
}
问题 D: 查找
时间限制: 1.000 Sec 内存限制: 32 MB
题目描述
输入数组长度 n
输入数组 a[1…n]
输入查找个数m
输入查找数字b[1…m]
输出 YES or NO 查找有则YES 否则NO 。
输入
输入有多组数据。
每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m<=n<=100)。
输出
如果在n个数组中输出YES否则输出NO。
样例输入 Copy
6
3 2 5 4 7 8
2
3 6
样例输出 Copy
YES
NO
#include<iostream>
using namespace std;
int main() {
int n, m;
while (cin >> n) {
int a[101] = { 0 };
int b[101] = { 0 };
for (int i = 0;i < n;i++) {
cin >> a[i];
}
cin >> m;
for (int i = 0;i < m;i++) {
cin >> b[i];
}
for (int i = 0;i < m;i++) {
int f = 0;
for (int j = 0;j < n;j++) {
if (b[i] == a[j]) {
f = 1;
cout << "YES" << endl;
break;
}
if (f == 0 && j == n - 1) {
cout << "NO" << endl;
}
}
}
}
}
问题 E: 学生查询
时间限制: 1.000 Sec 内存限制: 32 MB
题目描述
输入n个学生的信息,每行包括学号、姓名、性别和年龄,每一个属性使用空格分开。最后再输入一学号,将该学号对应的学生信息输出。
输入
测试数据有多组,第一行为样例数m。对于每个样例,第一行为学生人数n(n不超过20),加下来n行每行4个整数分别表示学号、姓名、性别和年龄,最后一行表示查询的学号。
输出
输出m行,每行表示查询的学生信息,格式参见样例。
样例输入 Copy
1
4
1 李江 男 21
2 刘唐 男 23
3 张军 男 19
4 王娜 女 19
2
样例输出 Copy
2 刘唐 男 23
#include<iostream>
using namespace std;
typedef struct stu {
int sno;
char name[20];
char sex[20];
int age;
};
int main() {
int m;
cin >> m;
while (m > 0) {
int n;
stu s[20];
cin >> n;
for (int i = 0;i < n;i++) {
cin >> s[i].sno >> s[i].name >> s[i].sex >> s[i].age;
}
int x;
cin >> x;
for (int i = 0;i < n;i++) {
if (s[i].sno == x) {
cout << s[i].sno << " " << s[i].name << " " << s[i].sex << " " << s[i].age << endl;
break;
}
}
m--;
}
}