FZU - 1606 - Format the expression

本文介绍了一道关于数学表达式简化的问题,重点在于如何通过解析输入的多项式字符串并统计不同项的系数来输出最简化的多项式形式。文章详细解释了处理多种特殊情况的方法,并提供了一份完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上题目:

Problem 1606 Format the expression

Accept: 87    Submit: 390
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Oaiei is a good boy who loves math very much, he would like to simplify some mathematical expression, can you help him? For the sake of simplicity, the form of expression he wanted to simplify is shown as follows:

  1. General characters
    • num: an integer (0 <= num <= 1000)
    • X: unknown variable
    • X^num: num power of X
    • numX: the coefficient of the unknown variable X is num
  2. Connector character
    • +: General characters connected with the character which expresses the addition
    • -: General characters connected with the character which expresses the subtraction
Given the expression, your task is conversion the expression to the simplest form.

 Input

Given the expression S, the length of S is less than 200, there is no space in the given string.

 Output

Output the simplest expression S’, you should output S’ accordance to the X with descending order of power. Note that X^1 need only output X, 1X need only output X.

 Sample Input

4X^5+8X^5+4X+3X^0+8
-4X^5-3X^5

 Sample Output

12X^5+4X+11
-7X^5
 
  题意:给你一条多项式,让你输出化简以后的多项式,其中给出的多项式符合题中的要求(即正确的,规范的)。
  模拟题,如果一开始没有分好每一种有可能出现的情况的话,会发现这一题很恶心。
  除了常规情况,还要考虑①开头是负数,②最终结果是0,③X^0,X,X^1,还有只有系数的情况。
  只要分好这些情况的话,然后统计好不同次数的系数,然后打印的时候小心一点就可以了。
 
上代码:
 
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #define MAX 1002
  5 #define max(x,y) (x > y ? x : y)
  6 using namespace std;
  7 
  8 int c[MAX];
  9 char s[MAX];
 10 int maxn;
 11 
 12 typedef struct{
 13     bool isx;
 14     bool isnum;
 15     bool ise;
 16     int r;
 17     int num;
 18     int e;
 19 }word;
 20 
 21 word w[MAX];
 22 int tot;
 23 
 24 
 25 void deal(){
 26     int num,e;
 27     if(w[tot].isnum==0 && w[tot].isx==0){return ;}
 28     else if(w[tot].isnum==0 && w[tot].isx==1)
 29     {
 30         num=1;
 31         if(w[tot].ise!=0) e=w[tot].e;
 32         else e=1;
 33     }
 34     else if(w[tot].isnum==1 && w[tot].isx==0){num=w[tot].num;e=0;}
 35     else if(w[tot].isnum==1 && w[tot].isx==1)
 36     {
 37         num=w[tot].num;
 38         if(w[tot].ise!=0) e=w[tot].e;
 39         else e=1;
 40     }
 41     c[e]+=w[tot].r*num;
 42     maxn=max(e,maxn);
 43 }
 44 
 45 int main()
 46 {
 47     int l;
 48     char o;
 49     //freopen("data.txt","r",stdin);
 50     while(scanf("%s",s)!=EOF){
 51         getchar();
 52         maxn=0;
 53         memset(c,0,sizeof(c));
 54         memset(w,0,sizeof(w));
 55         tot=0;
 56         l=strlen(s);
 57         w[tot].r=1;
 58         for(int i= (s[0]=='+' ? 1 : 0);i<l;i++){
 59             o=s[i];
 60             if(o=='X') {w[tot].isx=1;}
 61             else if(o=='+'){
 62                  deal();
 63                  tot++;
 64                  w[tot].r=1;
 65                  continue;
 66             }
 67             else if(o=='-'){
 68                  deal();
 69                  tot++;
 70                  w[tot].r=-1;
 71                  continue;
 72             }
 73 
 74             if('0'<=o && o<='9' && w[tot].isx==0){
 75                 w[tot].num=w[tot].num*10+(o-'0');
 76                 w[tot].isnum=1;
 77             }else if(w[tot].isx!=0 && (o>='0' && o<='9')){
 78                 w[tot].e=w[tot].e*10+(o-'0');
 79                 w[tot].isx=1;
 80                 w[tot].ise=1;
 81             }
 82         }
 83 
 84         deal();
 85         int count=0;
 86 
 87         for(int i=maxn;i>1;i--){
 88             if(c[i]==0) continue;
 89             if(count && c[i]>0) printf("+");
 90             if(c[i]!=1 && c[i]!=-1) printf("%dX^%d",c[i],i);
 91             else{
 92                 if(c[i]==-1) printf("-");
 93                 printf("X^%d",i);
 94             }
 95             count++;
 96         }
 97         if(count && c[1]>0) {
 98             printf("+");
 99             count++;
100         }
101         if(c[1]!=0){
102             if(c[1]!=1 && c[1]!=-1) printf("%d",c[1]);
103             if(c[1]==-1) printf("-");
104             printf("X");
105             count++;
106         }
107         if(count && c[0]>0) printf("+");
108         if((count>0 && c[0]!=0) || count==0) printf("%d",c[0]);
109         printf("\n");
110     }
111     return 0;
112 }
1606

 

转载于:https://www.cnblogs.com/sineatos/p/3565034.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值