首页 > ACM题库 > HDU-杭电 > HDU 4140-Calculator[解题报告]HOJ
2015
04-16

HDU 4140-Calculator[解题报告]HOJ

Calculator

问题描述 :

The users feedback for your most beloved open-source operating system is gathered, and guess what the most required feature turned out to be? Yes it is finally what we all have been waiting for so long, extending the built-in calculator functionality!
On of the suggested extensions is adding the capability of evaluating polynomials, and that’s what you are thrilled to participate with!
In this problem you’re given a polynomial entered by the user in the calculator and are asked to evaluate it for a certain value.

输入:

The first line of input contains T (0 < T <= 100) the number of polynomials, each test cases consists of two lines, the first line contains an integer (-1000 <= X <= 1000), the value for the variable X for which the polynomial is evaluated.
The second line contains a polynomial P with integer coefficients. P is a sum of terms of the form CX^E , where the coefficient C and the exponent E satisfy the following conditions:
1. E is an integer satisfying (0 <= E <= 30). If E is 0, then CX^E is expressed as C. If E is 1, then CX^E is expressed as CX, unless C is 1 or -1. In those instances, CX^E is expressed as X or -X.
2. C is an integer. If C is 1 or -1 and E is not 0 or 1, then the CX^E will appear as X^E or -X^E.
3. Only non-negative C values that are not part of the first term in the polynomial are preceded by +.
4. Exponents in consecutive terms are strictly decreasing.
5. C fits in a 32-bit signed integer.

输出:

The first line of input contains T (0 < T <= 100) the number of polynomials, each test cases consists of two lines, the first line contains an integer (-1000 <= X <= 1000), the value for the variable X for which the polynomial is evaluated.
The second line contains a polynomial P with integer coefficients. P is a sum of terms of the form CX^E , where the coefficient C and the exponent E satisfy the following conditions:
1. E is an integer satisfying (0 <= E <= 30). If E is 0, then CX^E is expressed as C. If E is 1, then CX^E is expressed as CX, unless C is 1 or -1. In those instances, CX^E is expressed as X or -X.
2. C is an integer. If C is 1 or -1 and E is not 0 or 1, then the CX^E will appear as X^E or -X^E.
3. Only non-negative C values that are not part of the first term in the polynomial are preceded by +.
4. Exponents in consecutive terms are strictly decreasing.
5. C fits in a 32-bit signed integer.

样例输入:

2
-2
2X^2-5X+7
2
-3X^12+X

样例输出:

Case #1: 25
Case #2: -12286

// 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;
}

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/pochioly/article/details/7211700


  1. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!