APCS 2017-0304-1秘密差
問題描述:
市場上有n個商品,你也知道這n個商品最近 3 天的價格。 你想要購買所以有近期價格大幅波動的商品,也就是近三天價格最高與最低差異至少d的所有物品,而購買物品的費用是它近 3 天的價格的平均值,保證這個平均值會是整數。 給定n個物品最近 3 天的價格,以其所設定的d,輸出總共購買的商品數量以及費用總和。
Sample input1: 1 3 24 27 21 Sample output1: 1 24
Thought:
In in input number a, b, c
we find if here is any difference
higher than dif
if so, we will add avg
to the
total
and also add the count_meet_dif
.
/*
f605. 1. 購買力
https://zerojudge.tw/ShowProblem?problemid=f605
Time Complexity: O(n)
Space Complexity: O(1)
*/
#include
int main() {
int n;
int dif;
scanf("%d %d", &n, &dif);
int count_meet_dif = 0;
int total = 0; // The total of the avg money of the product that meet the dif
for (int i = 0; i < n; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int avg = (a + b + c) / 3;
if (a - b >= dif || b - a >= dif || a - c >= dif || c - a >= dif || b - c >= dif || c - b >= dif) {
count_meet_dif++;
total+= avg;
}
}
printf("%d %d", count_meet_dif, total);
return 0;
}
Problem
Source