感觉逻辑没问题,就是不对:(
查看原帖
感觉逻辑没问题,就是不对:(
1533114
Henryblaaack楼主2025/7/24 21:31

这题我明白得用结构体+数组所以我用了,然后紧跟一个输入最后枚举判断,成了的就输出,自信一交,吧嗒全红,尝试自行debug了一会失败,求各位调一下:(

#include <iostream>
#include <cmath>
using namespace std;
int n;
struct student
{
	string name;
	int maths,chinese,english,total = 0;
};
struct student students[1005];
int main()
{
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		cin >> students[i].name >> students[i].chinese 
		>> students[i].maths >> students[i].english;
		students[i].total = students[i].chinese + students[i].maths + students[i].english;
	}
	for(int i = 1;i <= n;i++)
	{
		for(int j = i + 1;j <= n;j++)
		{
			if(fabs(students[i].total - students[j].total) <= 10 && (fabs(students[i].chinese - students[j].chinese) <= 5 &&
			fabs(students[i].maths - students[j].maths) <= 5 && fabs(students[i].english - students[j].english) <= 5))
			{
				cout << students[i].name << " " << students[j].name << endl;
				break;
			}
		}
	}
	return 0;
}
2025/7/24 21:31
加载中...