HDU 4140

来源:互联网 发布:怎样使用网络电视 编辑:程序博客网 时间:2024/05/21 14:46
// env:gcc
#include <stdio.h>#include <stdlib.h>#include <string.h>x = 0;nc = 0;long long res;sg,cef,idx;long long pw(int x, int a);main(){        int t;        for(scanf("%d", &t); t--;) run();}run(int c){        scanf("%d", &x);        result();        printf("Case #%d: %I64d\n", ++nc, res);}peek(){        int c = getchar();               ungetc(c, stdin);        return c;}skip(){        int c;        while((c=getchar())<=32);        ungetc(c, stdin);}expect(int ch){        if(peek()==ch) return getchar(),1;        return 0;}result(){        int ans = 0, ch;        res = 0;        skip();        do{                term();                ch = peek();        }while(ch=='+'||ch=='-');}term(){        sign_();        coef_();        index_();        res += (~sg?-cef:cef)*pw(x, idx);}sign_(){        sg = expect('-') ? -1 : expect('+'), 1;}coef_(){        if(peek()>='0' && peek()<='9')                scanf("%d", &cef);        else                cef=1;}index_(){        if (expect('X'))                if (expect('^'))                       scanf("%d", &idx);                else                       idx=1;        else                idx = 0;}long long pw(int x, int a){        long long d=1, b=x;        while(a){                if(a&1) d*=b;                b*=b;                a>>=1;        }        return d;}

也木有蛮多好说的 把文法想清楚,翻译一遍就行了