先上题目:

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. operator[] 重载

    #include <iostream>#include <vector>#include <string> class Assoc {    struct Pair ...

  2. [Swift]实现优先队列PriorityQueue

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. protobuf 编译 java js文件详解

    首先下载protobuf.exe 下载地址:https://download.csdn.net/download/qq_34756156/10220137 MessageBody.proto synt ...

  4. JavaScript 关于DOM的事件操作

    一.JavaScript的组成 JavaScript基础分为三个部分: ECMAscript:JavaScript的标准语法.包括变量,表达式,运算符,函数,if语句,for语句等. DOM:文档对象 ...

  5. 本地Gradle配置方法,免去长时间的更新同步等待

    通常gradle项目在gradle\wrapper\gradle-wrapper.properties中配置在线gradle: distributionBase=GRADLE_USER_HOME di ...

  6. 华为 荣耀 等手机解锁BootLoader

    下载工具按提示操作即可 链接:https://pan.baidu.com/s/1qZezd1q 密码:8pad 备用链接:https://pan.baidu.com/s/1nwv0heD

  7. SQL Server之存储过程

    存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行. 存储过程中可以包含逻辑控制语句和数据操纵语句,它 ...

  8. Python批量添加库搜索路径

    被win10 给坑了,换回Win7. 重装系统后,继续使用Python,Eclipse不用重装,pydev不用重装,只需重装Python2.7.6 X64 for win即可.然后,默认已安装的Pyt ...

  9. emlog通过pjax实现无刷新加载网页--完美解决cnzz统计和javascript失效问题

    想要更详细了解pjax,需要查看官网 或者看本站文章:jQuery.pjax.js:使用AJAX和pushState无刷新加载网页(官网教程中文翻译) 效果看本站,音乐无刷新播放,代码高亮和复制js加 ...

  10. Redis 之list链表结构及命令详解

    1.lpush  key   value   从左放一个值 2.rpush  key   value  从右放一个值 3.lrange  key  start   stop  获取链表数据(start ...