Given an array a of positive integers with length n, determine if there exist three distinct indices i, j, k such that ai+aj+ak ends in the digit 3.
Input
The first line contains an integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains an integer n (3≤n≤2*10^5) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤10^9) — the elements of the array.
The sum of n across all test cases does not exceed 2*10^5.
Output
Output t lines, each of which contains the answer to the corresponding test case. Output "YES" if there exist three distinct indices i, j, k satisfying the constraints in the statement, and "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
给定一个长度为n的数组,确定是否存在三个不同的索引i,j,k使得ai+aj+ak以数字3结尾。
Input
6
4
20 22 19 84
4
1 11 1 2022
4
1100 1100 1100 1111
5
12 34 56 78 90
4
1 9 8 4
6
16 38 94 25 18 99
Output
YES
YES
NO
NO
YES
YES
———————————————————————————————————————————
题解:
看是否有三个数的和的个位为3,即直接看三个数的个位的和的个位是否为3,而个位只有0-9十种情况,我们要从n个个位数中选取3个,所以我们可以对n个个位数进行简化,超过3个的通通舍弃,因为不可能会选到。那么最多就只有30个数了,这样直接三层循环去找也不会超时。
#include<iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t,n,i,p;
cin>>t;
while(t--){
cin>>n;
int a[10]={0};
int s[30],k,t,j,d=0;
for(i=0;i<n;i++){
cin>>p;
//存储0-9出现的次数
a[p%10]++;
}
for(i=0;i<10;i++){
if(a[i]>3){
a[i]=3;
}
for(j=0;j<a[i];j++){
s[d]=i;
d++;
}
}
for(i=0;i<d-2;i++){
for(j=i+1;j<d-1;j++){
for(k=j+1;k<d;k++){
if((s[i]+s[j]+s[k])%10==3){
goto fin;
}
}
}
}
cout<<"NO"<<endl;
continue;
fin:
cout<<"YES"<<endl;
}
return 0;
}