Luck Number
链接:https://ac.nowcoder.com/acm/contest/878/J
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
Let us remind you that lucky numbers are possible integers whose decimal representation only contains luck digit 6 and 8, also, it much have factors 4 and 7. For example 868, 6888 are luck and 678, 886 are not.
You are given n positive integers, you should check whether the integer is lucky.
输入描述:
The first line contains one integers n (1≤n≤1000). The next n
line contains n integers ai (1≤ai≤1e100)—the numbers that you should check.
输出描述:
N lines, if ai is lucky number, print
“YES”, else print “NO”.
示例1
输入
2
868
886
输出
YES
NO
题目大意:给你n个数每个数大小在1-1e100之间,让你判断每个数是否为lucky number。lucky number条件:只含8和6,且有因子4和7。
一看到10100就本能地想到了大数,但比赛的时候不知道怎么回事,YD写的大数全T了。。。我们用大数来除以28,看看能否整除,即p=a[i]%28。。。。蜜汁T。。。然后一直找bug,不过我们都不怎么会大数。。。。所以看不出个所以然来。。。
换种想法,我们要算a[i]%28,大数也有取模运算我们怎么取的呢。。。直接从高位往下一直取模就行了:
int len=strlen(s);
for (int i=1; i<len; i++) {
a=a*10+s[i]-'0';
a%=28;
}
于是这题也就结束了。。。所以用不着几百行的大数模板(关键是我不会大数QAQ。。。)
代码量很小384B,运行的时间也只有6ms
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
char s[200];
int main()
{
int n;
scanf ("%d",&n);
while (n--){
scanf ("%s",s);
int a=s[0]-'0';
int len=strlen(s);
for (int i=1; i<len; i++){
a=a*10+s[i]-'0';
a%=28;
}
for (int i=0; i<len; i++){
if (s[i]!='8' && s[i]!='6') {
a=1;break;
}
}
if (a) printf ("NO\n");
else printf ("YES\n");
}
return 0;
}