CSP-202109-1-数组推导
解题思路
- 如果
currentValue
与 previousValue
相同,说明这个值不是一个独特的新值,因此只将它加到 sumTotal
上。 - 如果
currentValue
与 previousValue
不相同,说明这是一个新的独特值,因此既将它加到 sumTotal
也加到 sumUnique
上。 - 更新
previousValue
为当前循环中的 currentValue
,以便在下一次循环中使用。
#include <iostream>
using namespace std;
int main() {
int n, previousValue_B = 0, sumMin = 0, sumMax = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int currentValue_B;
cin >> currentValue_B;
if (currentValue_B == previousValue_B) {
sumMax += currentValue_B;
}
else {
sumMax += currentValue_B;
sumMin += currentValue_B;
}
previousValue_B = currentValue_B;
}
cout << sumMax << endl << sumMin;
return 0;
}