首页 > 代码库 > 【HDOJ】1315 Basic

【HDOJ】1315 Basic

这道题目巨坑啊,注意__int64,int wa了一个下午。

 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4  5 #define MAXN 105 6  7 char vals[] = "0123456789abcdef"; 8 char s[MAXN]; 9 int l;10 bool flag;11 12 bool check(char ch, int base) {13     if (base <= 10)14         return ( ch>=0 && ch<=0+base-1);15     else16         return ( (ch>=0&&ch<=9) || (ch>=a && ch<a+base-10) );17 }18 19 int getv(int i, int base, __int64 *v) {20     __int64 ret = 0;21 22     while (flag && i < l) {23         if (s[i] == #)24             break;25         if (flag)   flag = check(s[i], base);26         if (s[i] >= a)27             ret = ret*base + s[i]-a+10;28         else29             ret = ret*base + s[i]-0;30         ++i;31     }32 33     *v = ret;34     return i;35 }36 37 void solve() {38     flag = true;39     l = strlen(s);40 41     int i = 0, k=0;42     __int64 tmp = -1, base = 10;43     bool ltag = false;44 45     while (flag && i < l) {46         if (s[i] == #) {47             if (!ltag) {48                 ltag = true;49                 base = tmp;50                 if (base<2 || base>16)51                     flag = false;52             } else {53                 if (s[i-1] == #)54                     flag = false;55                 ltag = false;56             }57             ++i;58             ++k;59         } else {60             if (i && !ltag)61                 flag = false;62             i = getv(i, base, &tmp);63         }64     }65 66     if (k & 1)67         flag = false;68 }69 70 int main() {71     int t;72 73 #ifndef ONLINE_JUDGE74     freopen("data.in", "r", stdin);75     freopen("data.out", "w", stdout);76 #endif77 78     scanf("%d", &t);79     while (t--) {80         scanf("%s", s);81         solve();82         if (flag) {83             printf("yes\n");84         } else {85             printf("no\n");86         }87     }88 89     return 0;90 }

 

【HDOJ】1315 Basic