【HDOJ】2451 Simple Addition Expression

规律题。

 1 /* 2451 */
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 #define MAXN 15
 7 
 8 char s[MAXN];
 9 __int64 a[MAXN];
10 int c[10] = {0,1,2,3,4,4,4,4,4,4};
11 
12 int main() {
13     int len;
14     __int64 ans;
15     int i, j, k;
16     
17     #ifndef ONLINE_JUDGE
18         freopen("data.in", "r", stdin);
19         freopen("data.out", "w", stdout);
20     #endif
21     
22     a[0] = 1;
23     a[1] = 3;
24     for (i=2; i<MAXN; ++i)
25         a[i] = a[i-1]*4;
26     while (scanf("%s", s) != EOF) {
27         len = strlen(s);
28         ans = 0;
29         for (i=0,j=len-1; i<len; ++i,--j) {
30             if (s[i] > '3') {
31                 ans += a[j+1];
32                 break;
33             } else {
34                 ans += c[s[i]-'0']*a[j];
35             }
36         }
37         printf("%I64d\n", ans);
38     }
39     
40     return 0;
41 }

 

posted on 2015-01-23 15:29  Bombe  阅读(197)  评论(0编辑  收藏  举报

导航