# APCS實作題2021年1月第1題:購買力
> 日期:2023年9月14日
> 作者:王一哲
> 題目來源:110年1月實作題第1題
> [ZeroJudge 題目連結](https://zerojudge.tw/ShowProblem?problemid=f605)
<br />
## 題目
### 問題描述
市場上有 $n$ 個商品,你也知道這 $n$ 個商品最近3天的價格。
你想要購買所以有近期價格大幅波動的商品,也就是近三天價格最高與最低差異至少 $d$ 的所有物品,而購買物品的費用是它近 3 天的價格的平均值,保證這個平均值會是整數。
給定 $n$ 個物品最近3天的價格,以其所設定的 $d$,輸出總共購買的商品數量以及費用總和。
<br />
### 輸入說明
配分
- 50分:$n = 1$
- 50分:$1 \leq n \leq 100$, $1 \leq d \leq 100$,所有價格皆為 $[1, 100]$ 內的整數。
<br />
### 輸出說明
在一行輸出兩個數字以空格分隔,依序是購買個商品數量以及總共的花費。
<br />
### 範例一:輸入
```
1 3
24 27 21
```
### 範例一:正確輸出
```
1 24
```
<br />
### 範例二:輸入
```
3 4
24 33 42
51 48 60
77 77 77
```
### 範例二:正確輸出
```
2 86
```
<br />
## Python 程式碼
於 ZeroJudge 測試結果,最長運算時間約為 21 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
n, d = map(int, input().split())
num, total = 0, 0
for _ in range(n):
data = list(map(int, input().split()))
diff = max(data) - min(data)
if diff >= d:
num += 1
total += sum(data) // len(data)
print("{:d} {:d}".format(num, total))
```
<br /><br />
## C++ 程式碼
於 ZeroJudge 測試結果,最長運算時間約為 3 ms,使用記憶體最多約為 324 kB,通過測試。
```cpp=
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int n, d; cin >> n >> d;
int num = 0, total = 0;
for(int i=0; i<n; i++) {
int high = INT_MIN, low = INT_MAX, price, tot = 0;
for(int j=0; j<3; j++) {
cin >> price;
tot += price;
if (price > high) high = price;
if (price < low) low = price;
}
if (high - low >= d) {
num++;
total += tot / 3;
}
}
cout << num << " " << total << endl;
return 0;
}
```
<br /><br />
---
###### tags:`APCS`、`Python`、`C++`