【CodeForces】550C Divisibility by Eight

传送门

题目描述

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.

解题思路

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 string s;
 7 int main(){
 8     cin>>s;
 9     s="00"+s;
10     for(register int i=s.size()-3;i>=0;i--){
11         for(register int j=s.size()-2;j>i;j--){
12             for(register int k=s.size()-1;k>j;k--){
13                 int tot=(s[i]-'0')*100+(s[j]-'0')*10+s[k]-'0';
14                 if(tot%8==0){
15                     cout<<"YES"<<endl;
16                     int flag=0;
17                     for(register int l=2;l<i;l++){
18                         cout<<s[l];
19                         flag=1;
20                     }
21                     if(flag){
22                         cout<<s[i]<<s[j]<<s[k]<<endl;
23                     }
24                     else cout<<tot<<endl;
25                     return 0;
26                 }
27             }
28         }
29     }
30     cout<<"NO"<<endl;
31 }

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9097441.html
今日推荐