hdu-1296-Polynomial Problem

Polynomial Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1164    Accepted Submission(s): 467


Problem Description
We have learned how to obtain the value of a polynomial when we were a middle school student. If f(x) is a polynomial of degree n, we can let

If we have x, we can get f(x) easily. But a computer can not understand the expression like above. So we had better make a program to obtain f(x).
 

Input
There are multiple cases in this problem and ended by the EOF. In each case, there are two lines. One is an integer means x (0<=x<=10000), the other is an expression means f(x). All coefficients ai(0<=i<=n,1<=n<=10,-10000<=ai<=10000) are integers. A correct expression maybe likes
1003X^5+234X^4-12X^3-2X^2+987X-1000
 

Output
For each test case, there is only one integer means the value of f(x).
 

Sample Input
  
  
3 1003X^5+234X^4-12X^3-2X^2+987X-1000
 

Sample Output
  
  
264302 Notice that the writing habit of polynomial f(x) is usual such as X^6+2X^5+3X^4+4X^3+5X^2+6X+7 -X^7-5X^6+3X^5-5X^4+20X^3+2X^2+3X+9 X+1 X^3+1 X^3 -X+1 etc. Any results of middle process are in the range from -1000000000 to 1000000000.

这道题其实并不难,就是需要耐心,并且很考验代码能力,

我是听取了学长倒着来的建议后才A的,花费我很多时间!

思路:

在这个式子中每个小部分只有两个数!如ax^b,最多只有a,b这两个数,或者有一个,或者一个都没有

所以对于一个数,可能的情况有五种

aX^b    X^b    aX    X     a

五种情况一一讨论(虽然有点笨,但是很简单)

#include<stdio.h>
#include<string.h>
char s[10000];
int n,flag;
int ys(int a,int b,int flag0)  //设置一个函数,用来处理得到的a,b两个数(里面包含了那五种情况)
{
    //printf("%d %d %d*-*\n",a,b,flag0);
    int num=1;
    if(b==0&&a!=0&&flag0==1)
    {
        return a*n;
    }
    else if(a==0&&b==0&&flag0==1)
    {
        //printf("*\n");
        return n;
    }
    else if(a==0&&b!=0&&flag0==1)
    {
        for(int i=1;i<=b;i++)
        {
            num=num*n;
        }
        return num;
    }
    else if(b==0&&a!=0&&flag0==0)
    {
        return a;
    }
    else
    {
        for(int i=1; i<=b; i++)
        {
            num=num*n;
        }
        return a*num;
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",s);
        int len=strlen(s);
        int a=0,b=0,w=1,sum=0,flag=0;
        for(int i=len-1; i>=0; i--) //  a,b就是用来找这两个数
        {
            if(s[i]=='X')
            {
                w=1;
                flag=1;
            }
            else if(s[i]=='+')
            {
                //printf("%d %d %d %d[+]\n",a,b,flag,ys(a,b,flag));
                sum+=ys(a,b,flag);
                a=0,b=0,w=1;
                flag=0;
            }
            else if(s[i]=='-')
            {
                //printf("%d %d %d %d[-]\n",a,b,flag,ys(a,b,flag));
                sum-=ys(a,b,flag);
                a=0,b=0,w=1;
                flag=0;
            }
            else if(s[i]=='^')
            {
                b=a;
                a=0;
                w=1;
                flag=1;
            }
            else
            {
                a+=(s[i]-'0')*w;
                w=w*10;
            }
        }
        if(s[0]!='-')
        {
            //printf("%d %d %d %d\n",a,b,flag,ys(a,b,flag));
            sum+=ys(a,b,flag);
        }
        printf("%d\n",sum);
    }
}



 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值