求大佬指教
  • 板块学术版
  • 楼主Tomwsc
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/2 16:20
  • 上次更新2025/2/2 16:29:56
查看原帖
求大佬指教
1418967
Tomwsc楼主2025/2/2 16:20

第一份代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 10 , MAXM = 2025;
int n;
int cnt;
struct Node {
	int p , t;
};
Node a[MAXM];
bool flag[MAXN];

inline bool cmp(Node a , Node b) {
	return a.t < b.t;
}

signed main() {
	freopen("P1413_2.in" , "r" , stdin);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(register int i = 1;i <= n;i ++)
		cin >> a[i].p >> a[i].t;
	sort(a + 1 , a + 1 + n , cmp);
	for(register int i = 1;i <= n;i ++) {
		if(flag[i])
			continue;
		++ cnt;
		cout << cnt << " ";
		for(register int j = 1;j <= n;j ++)
			if(a[j].p == a[i].p && a[j].t < a[i].t + 60 && !flag[j])
				flag[j] = true;
	}
	cout << cnt;
	return 0;
}

第二份代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 10 , MAXM = 2025;
int n;
struct Node {
	int p , t;
};
Node a[MAXM];
bool flag[MAXN];
int cnt;

inline bool cmp(Node a , Node b) {
	return a.t < b.t;
}

signed main() {
	freopen("P1413_2.in" , "r" , stdin);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(register int i = 1;i <= n;i ++)
		cin >> a[i].p >> a[i].t;
	sort(a + 1 , a + 1 + n , cmp);
	for(register int i = 1;i <= n;i ++) {
		if(flag[i])
			continue;
		++ cnt;
		cout << cnt << " ";
		for(register int j = 1;j <= n;j ++)
			if(a[j].p == a[i].p && a[j].t < a[i].t + 60 && !flag[j])
				flag[j] = true;
	}
	cout << cnt;
	return 0;
}

唯一的不同点是 cnt 的定义位置变了,为什么输出也变了?

2025/2/2 16:20
加载中...