Description
给你一个 nn 个数的数组 a_1 ,a_2, ..., a_n(无序)一次操作可以把两个数替换成这两个数的和。
比如数组 [2, 1, 4][2,1,4] 可以变成: [3, 4], [1, 6], [2, 5][3,4],[1,6],[2,5] 。
请问,在不限次合并操作之后,数组中最多能有多少个数可以被 3整除。
Input
第一行包含一个整数t(1≤t≤1000) ,表示有 t 个样例每个样例的第一行有一个整数 n(1≤n≤100) 表示数组中数的个数, 第二行有 n 个整数 a_1 ,a_2, ..., a_n (1≤ai≤1e9) 为数组元素。
Output
每个样例输出一行包含 m 表示该数组中在操作之后最多能有 m 个数可以被 3 整除。
Sample Input 1
2
5
3 1 2 3 1
7
1 1 1 1 1 2 2
Sample Output 1
3
3
Hint
[3,1,2,3,1]−>[3,3,3,1]
[1,1,1,1,1,2,2]−>[1,1,1,1,2,3]−>[1,1,1,3,3]−>[2,1,3,3]−>[3,3,3]
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
while(T --)
{
int res = 0;
int n;
cin >> n;
int ones = 0, twos = 0;
for(int i = 0; i < n; i ++)
{
int x;
cin >> x;
if(x % 3 == 0) res ++;
else if(x % 3 == 1) ones ++;
else twos ++;
}
if(ones <= twos)
{
res += ones;
twos -= ones;
res += twos / 3;
}
else
{
res += twos;
ones -= twos;
res += ones / 3;
}
cout << res << endl;
}
return 0;
}