Yzoi Easy Round 2_ Simplification (Simplify.c/cpp/pas)

Source: Internet
Author: User

Description

Given a polynomial, output the result of its simplification.

Input

A string that contains only the polynomial of the letter x, without parentheses and fractions, with no extra spaces.

Output

A string that, after simplifying the polynomial, outputs the items in the order of the times from large to small.

Input Sample

X^3+3*x^4-2*x^3+1-x

Output Sample

3*x^4-x^3-x+1

Hint

Each coefficient <10, the number of times <6, the number of items <20. The string length does not exceed 100.

The annoying analog code is as follows:

#include <iostream> #include <cstdio>using namespace std;const int maxn=1000+10;string s;int a[maxn],lc=1, Len;bool First=true;int Main () {//freopen ("simplify.in", "R", stdin)//freopen ("Simplify.out", "w", stdout);cin> >s;len=s.size (); if (len==1) {Cout<<s[0];return 0;} for (int i=0;i<len;i++) {if (s[i]== ' x ') {if (i==3) {if (s[0]== '-') {if (s[4]== ' ^ ') {a[s[5]-' 0 ']-=s[1]-' 0 ';} else{a[1]-=s[1]-' 0 ';} Continue;}} if (s[0]>= ' 0 ' &&s[0]<= ' 9 ' &&i==2) {if (s[3]== ' ^ ') {a[s[4]-' 0 ']+=s[0]-' 0 ';} else{a[1]+=s[0]-' 0 ';} Continue;} if (i==0)//{//a[1]++;//continue;//}if (s[i+1]== ' ^ ') {if (i==0) {a[s[i+2]-' 0 ']++;continue;} if (s[i-1]== ' * ') {if (s[i-3]== ' + ') a[s[i+2]-' 0 ']+=s[i-2]-' 0 '; else if (s[i-3]== '-') a[s[i+2]-' 0 ']-= (s[i-2]-' 0 ');} Else{if (i==0) {a[s[i+2]-' 0 ']++;continue;} if (s[i-1]== ' + ' | | s[i-1]== '-') {if (s[i-1]== '-') a[s[i+2]-' 0 ']--;elsea[s[i+2]-' 0 ']++;} else{a[s[i+2]-' 0 ']--;}} Else{if (s[i-1]== ' + ') {a[1]++;} else{a[1]--;}}} Else{if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {if (s[i-1]== ' ^ ' | | s[i+1]== ' * ') continue;a[0]+=s[i]-' 0 ';}}} for (int i=0;i<len;i++) {if (s[i]== ' ^ ') lc=max (lc,s[i+1]-' 0 ');} for (int i=lc;i>=0;i--) {if (a[i]==0) continue;if (i==0) {if (a[i]>0) {if (first) {cout<<a[i];first=false; Continue;} cout<< ' + ' <<a[i];} else{cout<< '-' <<a[i];} Continue;} if (i==1) {if (a[i]>0) {if (a[i]==1) {if (first) {cout<< "X"; first=false;continue;} cout<< "+x";} Else{if (first) {cout<<a[i]<< "*x"; first=false;continue;} cout<< "+" <<a[i]<< "*x";}} Else{if (a[i]==-1) {cout<< "-X";} else{cout<<a[i]<< "*x";}} Continue;} if (a[i]>0) {if (a[i]==1) {if (first) {cout<< "x^" <<i;first=false;continue;} else{cout<< "+x^" <<i;}} Else{if (first) {cout<<a[i]<< "*x^" <<i;first=false;continue;} else{cout<< "+" <<a[i]<< "*x^" <<i;cout<<9999999<<endl;}}} Else{if (a[i]==-1) {cout<< "-x^" <<i;} else{cout<<a[i]<< "*x^" <<i;}} cout<<endl;//for (int i=8;i>=0;i--)//cout<<a[i]<< ";//cout<<endl;//cout<<a[3]<<endl;return 0;} 

Yzoi Easy Round 2_ Simplification (Simplify.c/cpp/pas)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.