求助大佬C++类和普通哪个好啊??
查看原帖
求助大佬C++类和普通哪个好啊??
1216410
AlexWoods楼主2024/3/3 14:18

//用类:

#include<iostream>
using namespace std;
class daytime
{private:
	int sch[7]; 
	int aftsch[7];
	int total[7];
public:
	daytime()
	{
		for (int i = 0; i < 7; ++i)
		{
			cin >> sch[i] >> aftsch[i];
		}
	}
	void whehappy()
	{
		int day = 0;
		int total[7];
		int temp=0;
		for (int i = 0; i < 7; i++)
		{
			total[i] = sch[i] + aftsch[i];
			if (total[i] > total[temp])
			{
				temp = i;
			}
		}
		if (temp != 0)
			cout << temp + 1 << endl;
		else cout << 0 << endl;
		
	}
};
int main()
{
	daytime a;
	a.whehappy();
	return 0;
}

// 用普通:

#include<iostream>
using namespace std;
int main()
{
	int sch[7][2];
	int temp=0;
	int sum[7] = { 0 };
	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 2; j++)
			cin >> sch[i][j];
	}
	for (int i = 0; i <7; i++)
	{
  	sum[i] = sch[i][0] + sch[i][1];
		if (sum[i] > sum[temp])
		{
			temp = i;
		}
	}
	if (temp != 0)
		cout << temp + 1 << endl;
	else cout << 0 << endl;
	return 0;
}
2024/3/3 14:18
加载中...