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语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
随机推荐
- commons-fileupload上传文件(1)
近期,写一个上传图片的功能.于是用到commons-fileupload这个组件.提过form提交表单到后台(这里没实用到structs框架).在后台List pl = dfu.parseReques ...
- Google Deepmind AI tries it hand at creating Hearthstone and Magic: The Gathering cards
http://www.techrepublic.com/article/google-deepmind-ai-tries-it-hand-at-creating-hearthstone-magic-t ...
- css3 选择器 权重问题 (第一部分)
其实我现在写的这些博文笔记都是我很早之前学习的时候所写的笔记,只不过之前是没有写博客的习惯,所以都是写在word文档中个人需要的时候看而已.最近刚刚开了博客,所以想将自己的笔记贴到博客. 但是现在看来 ...
- 基于移动Web的视图引擎实现
第一步:移动视图引擎实现 using System.Web.Mvc; /// <summary> /// 移动版View引擎 /// </summary> public cla ...
- CentOS6 在线安装PostgreSQL10
本文主要通过实际案例介绍如何在CentOS6环境中在线安装PostgreSQL10,安装环境需具备能够使用yum在线安装功能.具体安装步骤如下, 1 下载对应版本的PGDG文件 从https://yu ...
- Laravel5.1学习笔记6 响应
基本响应 附加头信息到响应 附加Cookie到响应 其他响应 View视图响应 JSON响应 File下载 重定向 重定向到命名路由 重定向到控制器Action 附带闪回Session数据重定向 响应 ...
- intellij 创建java web项目(maven管理的SSH)
intellij 创建java web项目(maven管理的SSH) 环境intellij IDEA14.MAVEN.Spring.Struts2.Hibernate.Java Web.工程搭建. 1 ...
- T-SQL查询高级--理解SQL SERVER中非聚集索引的覆盖,连接,交叉和过滤
写在前面:这是第一篇T-SQL查询高级系列文章.但是T-SQL查询进阶系列还远远没有写完.这个主题放到高级我想是因为这个主题需要一些进阶的知识作为基础..如果文章中有错误的地方请不吝指正.本篇文章 ...
- OpenCV:Python3使用OpenCV
Python3使用OpenCV安装过程应该是这样的,参考:http://blog.csdn.net/lixintong1992/article/details/61617025 ,使用conda ...
- Metric Learning度量学习:**矩阵学习和图学习
DML学习原文链接:http://blog.csdn.net/lzt1983/article/details/7884553 一篇metric learning(DML)的综述文章,对DML的意义.方 ...