【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a058
【Tag】for 迴圈,array 陣列
- 題目要求輸出3k、3k+1、3k+2的數各有幾個?
- 把每個數字除以 3 取餘的結果當成 array 的下標,利用 array 統計3k、3k+1、3k+2 出現的次數。
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int x, ans[3] = {};
for (int i = 0; i < n; i++) {
cin >> x;
ans[x % 3]++;
}
for (int i = 0; i < 3; i++) {
cout << ans[i] << " ";
}
return 0;
}