J.Luck Number--(模拟大数取模)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值