FZU - 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:
- 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
- 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
Sample Output
#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的更多相关文章
- C# ORM中Dto Linq Expression 和 数据库Model Linq Expression之间的转换
今天在百度知道中看到一个问题,研究了一会便回答了: http://zhidao.baidu.com/question/920461189016484459.html 如何使dto linq 表达式转换 ...
- spring 定时任务@Scheduled
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- UITest 单元测试常用的断言
XCTFail(format…) 生成一个失败的测试: XCTFail(@"Fail"); XCTAssertNil(a1, format...) 为空判断, a1 为空时通过,反 ...
- XCTest各种断言
XCTFail(format…) 生成一个失败的测试: XCTAssertNil(a1, format...)为空判断,a1为空时通过,反之不通过: XCTAssertNotNil(a1, forma ...
- 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 ...
- 爱上iOS单元测试系列之爱上她就要先了解她:单元测试入门
前言 对于单元测试一开始我是拒绝的.单元测试是一个什么东东,因为我喜欢做iOS开发是因为喜欢写APP的啊,一切和这一目标不相干的东西我没兴趣啊,所以从事iOS开发几年都没去深入学习过单元测试(主要是之 ...
- iOS中的预编译指令的初步探究
目录 文件包含 #include #include_next #import 宏定义 #define #undef 条件编译 #if #else #endif #if define #ifdef #i ...
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- iOS开发:XCTest单元测试(附上一个单例的测试代码)
测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
随机推荐
- operator[] 重载
#include <iostream>#include <vector>#include <string> class Assoc { struct Pair ...
- [Swift]实现优先队列PriorityQueue
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- protobuf 编译 java js文件详解
首先下载protobuf.exe 下载地址:https://download.csdn.net/download/qq_34756156/10220137 MessageBody.proto synt ...
- JavaScript 关于DOM的事件操作
一.JavaScript的组成 JavaScript基础分为三个部分: ECMAscript:JavaScript的标准语法.包括变量,表达式,运算符,函数,if语句,for语句等. DOM:文档对象 ...
- 本地Gradle配置方法,免去长时间的更新同步等待
通常gradle项目在gradle\wrapper\gradle-wrapper.properties中配置在线gradle: distributionBase=GRADLE_USER_HOME di ...
- 华为 荣耀 等手机解锁BootLoader
下载工具按提示操作即可 链接:https://pan.baidu.com/s/1qZezd1q 密码:8pad 备用链接:https://pan.baidu.com/s/1nwv0heD
- SQL Server之存储过程
存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行. 存储过程中可以包含逻辑控制语句和数据操纵语句,它 ...
- Python批量添加库搜索路径
被win10 给坑了,换回Win7. 重装系统后,继续使用Python,Eclipse不用重装,pydev不用重装,只需重装Python2.7.6 X64 for win即可.然后,默认已安装的Pyt ...
- emlog通过pjax实现无刷新加载网页--完美解决cnzz统计和javascript失效问题
想要更详细了解pjax,需要查看官网 或者看本站文章:jQuery.pjax.js:使用AJAX和pushState无刷新加载网页(官网教程中文翻译) 效果看本站,音乐无刷新播放,代码高亮和复制js加 ...
- Redis 之list链表结构及命令详解
1.lpush key value 从左放一个值 2.rpush key value 从右放一个值 3.lrange key start stop 获取链表数据(start ...