80分,是否是逻辑有错误?
查看原帖
80分,是否是逻辑有错误?
1070752
a13901280570楼主2024/1/22 16:29
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
	int n, x;
	cin >> n >> x;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	int s = 0;
	for (int i = 0; i < n; i++) {
		int t = 0;
		if (a[i] > x) {
			t = a[i] - x;
			a[i] = x;
			s += t;
		}
	}

	for (int i = 0; i < n - 1; i++) {
		int t = 0;
		if (a[i] + a[i + 1] > x) {
		    t = a[i] + a[i + 1] - x;
		   a[i + 1] -= t;
		    s += t;
		}
		t = 0;
	}
	cout << s << endl;
	return 0;
}
2024/1/22 16:29
加载中...