技巧之 -- 8 的倍数
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.
Example
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO
題目分析 :
給你一個(gè)長(zhǎng)度小于 100 的串,讓你刪除其中的一些字符,讓剩下的數(shù)是 8 的倍數(shù),如果沒(méi)有輸出 NO
思路分析 :
這個(gè)題當(dāng)時(shí)是沒(méi)有寫(xiě)出來(lái)的,發(fā)現(xiàn)自己好笨 ... 連自己打個(gè)表看下規(guī)律都找不到,只要你打個(gè) 2000 以內(nèi)的表就能發(fā)現(xiàn)規(guī)律,你會(huì)發(fā)現(xiàn)只要之后 3位是 8的倍數(shù),不管它前面是什么數(shù)字,那么這個(gè)數(shù)一定是8的倍數(shù)
至于證明呢,我就只有一個(gè)比較 low 的辦法了,因?yàn)樽钚〉?位數(shù)是1000, 它是 8 的倍數(shù),那么它在加上一個(gè)是 8倍數(shù)的三位數(shù)一定就能得到一個(gè)是8倍數(shù)的3位數(shù)。
那么根據(jù)這個(gè)簡(jiǎn)單的證明,我們是不就能得到進(jìn)一步推廣,比如我們將這個(gè)題 的對(duì) 8 的判斷更改為對(duì) 125 的判斷,同樣也是先看最小的四位數(shù)是 1000,只要求它最后的三位是 125的倍數(shù)就可以了
代碼示例 :
char pre[105];
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%s", pre+1);
int len = strlen(pre+1);
for(int i = 1; i <= len; i++){
int num = pre[i] - '0';
if (num % 8 == 0) {printf("YES
%d
", num); return 0;}
for(int j = i+1; j <= len; j++){
int num = 10*(pre[i]-'0') + (pre[j]-'0');
if (num % 8 == 0) {printf("YES
%d
", num); return 0;}
for(int k = j+1; k <= len; k++){
int num = 100*(pre[i]-'0') + 10*(pre[j]-'0') + (pre[k]-'0');
if (num % 8 == 0) {printf("YES
%d
", num); return 0;}
}
}
}
printf("NO
");
return 0;
}
東北日出西邊雨 道是無(wú)情卻有情
總結(jié)
以上是生活随笔為你收集整理的技巧之 -- 8 的倍数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: randperm函数
- 下一篇: 电喷摩托车冷车启动困难热车正常是什么原因