1. 程式人生 > >Maximum Sum of Digits(CodeForces 1060B)

Maximum Sum of Digits(CodeForces 1060B)

In the first example, you can choose, for example, a=17 and b=18, so that S(17)+S(18)=1+7+1+8=17. It can be shown that it is impossible to get a larger answer.

In the second test example, you can choose, for example, a=5000000001 and b=4999999999, with S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.

相信不少人wa是這個程式碼

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<stack>
 9 #include<vector>
10 #include<queue>
11 #include<set
> 12 #include<algorithm> 13 #define max(a,b) (a>b?a:b) 14 #define min(a,b) (a<b?a:b) 15 #define swap(a,b) (a=a+b,b=a-b,a=a-b) 16 #define maxn 320007 17 #define N 100000000 18 #define INF 0x3f3f3f3f 19 #define mod 1000000009 20 #define e 2.718281828459045 21 #define eps 1.0e18 22 #define PI acos(-1) 23
#define lowbit(x) (x&(-x)) 24 #define read(x) scanf("%d",&x) 25 #define put(x) printf("%d\n",x) 26 #define memset(x,y) memset(x,y,sizeof(x)) 27 #define Debug(x) cout<<x<<" "<<endl 28 #define lson i << 1,l,m 29 #define rson i << 1 | 1,m + 1,r 30 #define ll long long 31 //std::ios::sync_with_stdio(false); 32 //cin.tie(NULL); 33 using namespace std; 34 35 36 int main() 37 { 38 ll n,a,b; 39 cin>>n; 40 ll m=n,k=9; 41 while(m>=k) 42 { 43 k=k*10+9; 44 } 45 k/=10; 46 //cout<<k<<endl; 47 a=k; 48 b=n-k; 49 ll sum=0; 50 while(a) 51 { 52 sum+=a%10; 53 a/=10; 54 } 55 while(b) 56 { 57 sum+=b%10; 58 b/=10; 59 } 60 cout<<sum<<endl; 61 return 0; 62 }

你們可以嘗試一下123這個樣例,輸出為15,但正確答案為24(99+24)。

AC程式碼如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<stack>
 9 #include<vector>
10 #include<queue>
11 #include<set>
12 #include<algorithm>
13 #define max(a,b)   (a>b?a:b)
14 #define min(a,b)   (a<b?a:b)
15 #define swap(a,b)  (a=a+b,b=a-b,a=a-b)
16 #define maxn 320007
17 #define N 100000000
18 #define INF 0x3f3f3f3f
19 #define mod 1000000009
20 #define e  2.718281828459045
21 #define eps 1.0e18
22 #define PI acos(-1)
23 #define lowbit(x) (x&(-x))
24 #define read(x) scanf("%d",&x)
25 #define put(x) printf("%d\n",x)
26 #define memset(x,y) memset(x,y,sizeof(x))
27 #define Debug(x) cout<<x<<" "<<endl
28 #define lson i << 1,l,m
29 #define rson i << 1 | 1,m + 1,r
30 #define ll long long
31 //std::ios::sync_with_stdio(false);
32 //cin.tie(NULL);
33 using namespace std;
34 
35 
36 int main()
37 {
38     ll n,a,b;
39     cin>>n;
40     ll m=n,k=9;
41     while(m>=k)
42     {
43         k=k*10+9;
44     }
45     k/=10;
46     //cout<<k<<endl;
47     a=k;
48     b=n-k;
49     ll sum=0;
50     while(a)
51     {
52         sum+=a%10;
53         a/=10;
54     }
55     while(b)
56     {
57         sum+=b%10;
58         b/=10;
59     }
60     cout<<sum<<endl;
61     return 0;
62 }
View Code