You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn’t contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn’t have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integer n. The representation of number n doesn’t contain any leading zeroes and its length doesn’t exceed 100 digits.
Output
Print “NO” (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print “YES” in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
Examples
1
input | |
---|---|
3454 | |
Output | |
YES | |
344 |
2
input | |
---|---|
10 | |
Output | |
YES | |
0 |
3
input | |
---|---|
111111 | |
Output | |
NO |
题目大意:
给定一个数字,你可以删除几个数字(也可以不删),使得处理后的数字在每位上的数字不做重新排序的前提下,能被8整除。
题目理解:
(这题真的要给我整死了,数学好是真的好的)
只要末尾三位数是8的倍数整个数便可以被8整除;
因为是随意输出一种,所以我们只要分三步枚举即可,分别判断最后一位(if(a[n]==8||a[n]==0)则答案是"yes"并输出;
否则判断(a[n-1]*10+a[n])%8是否为0,若是则"yes"并输出;
否则判断(a[n-2]*100+a[n-1]*10+a[n])%8是否为0,若是则"yes"并输出否则"no";
程序结束。
ac代码:
#include<iostream>
#include<cstring>
using namespace std;
char a[110];
int p[110];
int main()
{
cin>>a;
int n=strlen(a);
for(int i=0; i<n; i++)
p[i]=a[i]-'0';//字符转化为数字;
for(int i=0; i<n; i++)
if(p[i]%8==0)
{
cout<<"YES"<<endl<<p[i]<<endl;//一位数的判断;
return 0;
}
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
if((p[i]*10+p[j])%8==0)
{
cout<<"YES"<<endl<<p[i]*10+p[j]<<endl;//两位数的判断;
return 0;
}
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
for(int k=j+1; k<n; k++)
if((p[i]*100+p[j]*10+p[k])%8==0)
{
cout<<"YES"<<endl<<p[i]*100+p[j]*10+p[k]<<endl;//三位数的判断;
return 0;
}
cout<<"NO"<<endl;
return 0;
}
Keep loneliness and code company。