算法笔记3.2例题习题_查找元素

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--;
	}
}
好的,我了解了您的问题。让我来回答您的问题。 MyDate 是一个日期类,它可以存储年、月、日的信息,并且能够对日期进行一些操作。下面是完善后的 MyDate 类的代码示例: ```java public class MyDate { private int year; private int month; private int day; public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void setDay(int day) { this.day = day; } public void setDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public String toString() { return String.format("%04d-%02d-%02d", year, month, day); } public boolean equals(Object obj) { if (obj instanceof MyDate) { MyDate date = (MyDate) obj; return year == date.year && month == date.month && day == date.day; } return false; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + day; result = prime * result + month; result = prime * result + year; return result; } public MyDate nextDay() { int nextDay = day + 1; int nextMonth = month; int nextYear = year; if (nextDay > getDaysInMonth(year, month)) { nextDay = 1; nextMonth++; if (nextMonth > 12) { nextMonth = 1; nextYear++; } } return new MyDate(nextYear, nextMonth, nextDay); } public MyDate nextMonth() { int nextMonth = month + 1; int nextYear = year; if (nextMonth > 12) { nextMonth = 1; nextYear++; } int maxDay = getDaysInMonth(nextYear, nextMonth); int nextDay = Math.min(day, maxDay); return new MyDate(nextYear, nextMonth, nextDay); } public MyDate nextYear() { int nextYear = year + 1; int maxDay = getDaysInMonth(nextYear, month); int nextDay = Math.min(day, maxDay); return new MyDate(nextYear, month, nextDay); } private int getDaysInMonth(int year, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (isLeapYear(year)) { return 29; } else { return 28; } default: throw new IllegalArgumentException("Invalid month: " + month); } } private boolean isLeapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } else { return false; } } } ``` 在这个类中,我们实现了以下方法: - 构造方法 MyDate(int year, int month, int day):用于创建一个日期对象。 - getYear()、getMonth()、getDay():用于获取年、月、日。 - setYear()、setMonth()、setDay():用于设置年、月、日。 - setDate(int year, int month, int day):用于设置年、月、日。 - toString():用于以字符串形式返回当前日期。 - equals(Object obj):用于判断当前日期是否与另一个日期相等。 - hashCode():用于生成当前日期的哈希码。 - nextDay()、nextMonth()、nextYear():用于获取当前日期的下一天、下一个月、下一年。 - getDaysInMonth(int year, int month):用于获取指定年月的天数。 - isLeapYear(int year):用于判断指定年是否为闰年。 这些方法可以帮助我们对日期进行各种操作,比如获取下一天的日期、判断两个日期是否相等等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值