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语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
随机推荐
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 博弈
D. Vasya and Chess Vasya decided to learn to play chess. Classic chess doesn't seem interesting to ...
- ubuntu下一款有点感觉的 linux音乐播放器 clementine(小橘子))
https://www.clementine-player.org/ 在linux听音乐的感觉确实不是很好,音乐播放器很多.但是仅仅只是数量上的优势,在确实不是很好用.自带的rhythmbox确实很占 ...
- C#上移,下移TreeView中的树节点顺序
C#上移,下移TreeView中的树节点顺序 2009-08-12 20:10 1494人阅读 评论(2) 收藏 举报 c#buttonobjectnullstring C#中,通过单击上移,下移按钮 ...
- poj3233Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23187 Accepted: ...
- 通过JS制作一个简易数码时钟
设计思路: 数码时钟即通过图片数字来显示当前时间,需要显示的图片的URL根据时间变化而变化. a.获取当前时间Date()并将当前时间信息转换为一个6位的字符串; b.根据时间字符串每个位置对应的数字 ...
- Java数组!!!你知多少?
这是一篇网上搜到的文章,对数组进行了充分的说明.楼主懂了数组的俩种初始化方式:静态初始化和动态初始化方式.(贴过来备用哦) 下面我带大家一起加深一下对Java数组的认识: 1.理解数组 数组也是一种数 ...
- 关于Core里的 StartUp里的方法的理解。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; ...
- 安装完MongoDB后尝试mongod -dbpath命令为什么会一直卡在连接端口?
1.现象如下 Linux Windows 2.原因 其实,这不是卡住了,而是告诉我们.数据库已经启动,而且这个东东还不能关掉,关掉意味着数据库也关了.一开始我也是傻逼逼的在那等了一天,哎.... 3. ...
- 【SQL】多表查询
多表查询,即查询可以从两个或多个表中获取数据.在Oracle中,有两种类型的连接格式:ANSI SQL连接格式和Oracle特有的连接格式.Oracle建议采用符合ANSI标准的连接格式. 1.内连接 ...
- Embedded之Stack之三
Stack Overflow While stacks are generally large, they don't occupy all of memory. It is possible to ...