NC25240. Lucky Number
描述
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
Java(javac 1.8) 解法, 执行用时: 385ms, 内存消耗: 23036K, 提交时间: 2019-05-04 13:03:55
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n; n=in.nextInt(); for(int t=0;t<n;t++) { BigInteger a; a=in.nextBigInteger(); String s; s=a.toString(); int len=s.length(); int flag=1; for(int i=0;i<len;i++) { if(s.charAt(i)-'0'!=6&&s.charAt(i)-'0'!=8) flag=0; } if((a.mod(BigInteger.valueOf(4))).compareTo(BigInteger.ZERO)==0&&(a.mod(BigInteger.valueOf(7))).compareTo(BigInteger.ZERO)==0) { flag=1; } else flag=0; if(flag==1) System.out.println("YES"); else System.out.println("NO"); } } }
C++14(g++5.4) 解法, 执行用时: 5ms, 内存消耗: 352K, 提交时间: 2019-05-04 22:30:57
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1005; char a[maxn]; int main() { int t; scanf("%d",&t); while(t--) { int flag=1,sum=0; scanf("%s",a); for(int i=0;i<strlen(a);i++) { if(a[i]!='6'&&a[i]!='8') flag=0; sum=(sum*10+a[i]-'0')%28; } if(sum!=0) flag=0; if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 376K, 提交时间: 2019-05-07 21:17:57
#include<bits/stdc++.h> using namespace std; int n; char s[105]; int main(){ for(scanf("%d",&n);n;n--){ scanf("%s",s); int res1=0,res2=0,flag=1; for(int i=0;s[i];i++){ if(s[i]^'6'&&s[i]^'8'){ flag=0;break; }res1=(res1*10+s[i]-'0')%4; res2=(res2*10+s[i]-'0')%7; }if(res1||res2)flag=0; puts(flag?"YES":"NO"); } return 0; }
Python3(3.5.2) 解法, 执行用时: 53ms, 内存消耗: 7176K, 提交时间: 2019-05-05 09:12:13
n=int(input()) for i in range(n): a=int(input()) #print(a) if(a%28 !=0): print("NO") continue while a%10==6 or a%10==8: a//=10 if a==0: print("YES") else: print("NO")