先上题目:

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,还有只有系数的情况。
  只要分好这些情况的话,然后统计好不同次数的系数,然后打印的时候小心一点就可以了。
 
上代码:
 
 #include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 1002
#define max(x,y) (x > y ? x : y)
using namespace std; int c[MAX];
char s[MAX];
int maxn; typedef struct{
bool isx;
bool isnum;
bool ise;
int r;
int num;
int e;
}word; word w[MAX];
int tot; void deal(){
int num,e;
if(w[tot].isnum== && w[tot].isx==){return ;}
else if(w[tot].isnum== && w[tot].isx==)
{
num=;
if(w[tot].ise!=) e=w[tot].e;
else e=;
}
else if(w[tot].isnum== && w[tot].isx==){num=w[tot].num;e=;}
else if(w[tot].isnum== && w[tot].isx==)
{
num=w[tot].num;
if(w[tot].ise!=) e=w[tot].e;
else e=;
}
c[e]+=w[tot].r*num;
maxn=max(e,maxn);
} int main()
{
int l;
char o;
//freopen("data.txt","r",stdin);
while(scanf("%s",s)!=EOF){
getchar();
maxn=;
memset(c,,sizeof(c));
memset(w,,sizeof(w));
tot=;
l=strlen(s);
w[tot].r=;
for(int i= (s[]=='+' ? : );i<l;i++){
o=s[i];
if(o=='X') {w[tot].isx=;}
else if(o=='+'){
deal();
tot++;
w[tot].r=;
continue;
}
else if(o=='-'){
deal();
tot++;
w[tot].r=-;
continue;
} if(''<=o && o<='' && w[tot].isx==){
w[tot].num=w[tot].num*+(o-'');
w[tot].isnum=;
}else if(w[tot].isx!= && (o>='' && o<='')){
w[tot].e=w[tot].e*+(o-'');
w[tot].isx=;
w[tot].ise=;
}
} deal();
int count=; for(int i=maxn;i>;i--){
if(c[i]==) continue;
if(count && c[i]>) printf("+");
if(c[i]!= && c[i]!=-) printf("%dX^%d",c[i],i);
else{
if(c[i]==-) printf("-");
printf("X^%d",i);
}
count++;
}
if(count && c[]>) {
printf("+");
count++;
}
if(c[]!=){
if(c[]!= && c[]!=-) printf("%d",c[]);
if(c[]==-) printf("-");
printf("X");
count++;
}
if(count && c[]>) printf("+");
if((count> && c[]!=) || count==) printf("%d",c[]);
printf("\n");
}
return ;
}

1606

FZU - 1606 - Format the expression的更多相关文章

  1. C# ORM中Dto Linq Expression 和 数据库Model Linq Expression之间的转换

    今天在百度知道中看到一个问题,研究了一会便回答了: http://zhidao.baidu.com/question/920461189016484459.html 如何使dto linq 表达式转换 ...

  2. spring 定时任务@Scheduled

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  3. UITest 单元测试常用的断言

    XCTFail(format…) 生成一个失败的测试: XCTFail(@"Fail"); XCTAssertNil(a1, format...) 为空判断, a1 为空时通过,反 ...

  4. XCTest各种断言

    XCTFail(format…) 生成一个失败的测试: XCTAssertNil(a1, format...)为空判断,a1为空时通过,反之不通过: XCTAssertNotNil(a1, forma ...

  5. How to only capute sub-matched character by grep

    File content: <a href="ceph-0.80.9-82.1.x86_64.rpm"><img src="/icons/rpm.gif ...

  6. 爱上iOS单元测试系列之爱上她就要先了解她:单元测试入门

    前言 对于单元测试一开始我是拒绝的.单元测试是一个什么东东,因为我喜欢做iOS开发是因为喜欢写APP的啊,一切和这一目标不相干的东西我没兴趣啊,所以从事iOS开发几年都没去深入学习过单元测试(主要是之 ...

  7. iOS中的预编译指令的初步探究

    目录 文件包含 #include #include_next #import 宏定义 #define #undef 条件编译 #if #else #endif #if define #ifdef #i ...

  8. iOS 单元测试之XCTest详解(一)

    iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...

  9. iOS开发:XCTest单元测试(附上一个单例的测试代码)

    测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...

随机推荐

  1. VC UI界面库大集合

    Guitoolkit http://www.beyondata.com/pwc.html The Ultimate Toolbox http://www.codeproject.com/KB/MFC/ ...

  2. Labeling Balls(拓扑)

    http://poj.org/problem?id=3687 看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了..后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边. #include ...

  3. Django day08 多表操作 (二) 添加表记录

    一: 一对多 1. 一对多新增 两种方式:  publish = 对象    publish_id = id 1. publish_id 和 publish 的区别就是: 1)publish_id 可 ...

  4. python 关于文件操作的一些理解

    在用python进行数据处理编程中,往往涉及到文件IO口读写,IO口的读写性能会极大的影响程序的运行时间.在进行文件写入时,一般会存在两种情况.第一种是数据到来马上进行数据写入,即来一条写一条,第二种 ...

  5. python 12:list(range(...)) (转化参数列表)

    numbers = list(range(1,11)) #把范围产生的数字串转化为列表储存 print(numbers) 运行结果应该是: [1,2,3,4,5,6,7,8,9,10]

  6. Python启动浏览器Firefox\Chrome\IE

    # -*- coding:utf-8 -*- import os import selenium from selenium import webdriver from selenium.webdri ...

  7. spring框架搭建(一)

    spring介绍 spring是一个轻量级控制反转(IOC)和面向切面(AOP)的容器框架,它主要是为了解决企业应用开发复杂性而诞生的. 简单来说spring是一个一站式轻量级开源框架. IOC:In ...

  8. P2241 统计方形(数据加强版)

    题目背景 1997年普及组第一题 题目描述 有一个n*m方格的棋盘,求其方格包含多少正方形.长方形 输入输出格式 输入格式: n,m因为原来数据太弱,现规定m小于等于5000,n小于等于5000(原来 ...

  9. 单个句子<code> 多行代码显示<pre> 键盘输入<kbd>

    1.用来显示单个句子或者单个单词:<code>……</code> 2.用来显示多行代码:<pre>……</pre> 当行数高度大于340px,自动出现y ...

  10. 简繁体互换工具:opencc

    简繁体互换工具:opencc opencc是一个简体.繁体相互转换的命令行工具. 安装 下载软件包.在下载页面下载软件包(如1.0.4版本) 解压.通过命令解压:tar -xzvf opencc-1. ...