九度oj 1101 计算式的一种有趣解决思路
2016-01-25 20:20
337 查看
首先1101这题出的真缺少条件!!!
题目链接:http://ac.jobdu.com/problem.php?pid=1101
我就在想-1-2这种式子就通不过我(别人)的AC代码。
解题思路到是很赞!!!
设想这种情况,式子中只有+号,那么结果可以看成是若干数之和。实际情况下,不止+号,还有-,*,/号。-号可以看成是减号后一个数的负数,*号的操作方式是符号的前后两个数直接运算。最后就化简成我们所设想的情况。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
using namespace std;
int num[110];
bool judge(char cha){
if(cha=='+' || cha=='-' || cha=='*' || cha=='/')
return true;
return false;
}
int main(){
int numb;
char c;
int count=0;
while(scanf("%d",&numb)!=EOF){
memset(num,0,110*sizeof(int));
num[0]=numb;
count=0;
while(scanf("%c",&c)!=EOF && judge(c)){
cin>>numb;
if(c=='+')
{
num[++count]=numb;
}
if(c=='-')
{
num[++count]-=numb;
}
if(c=='*')
{
num[count]*=numb;
}
if(c=='/')
{
num[count]/=numb;
}
}
int answer=0;
for(int i=0;i<=count;i++){
answer+=num[i];
}
cout<<answer<<endl;
}
}
题目链接:http://ac.jobdu.com/problem.php?pid=1101
我就在想-1-2这种式子就通不过我(别人)的AC代码。
解题思路到是很赞!!!
设想这种情况,式子中只有+号,那么结果可以看成是若干数之和。实际情况下,不止+号,还有-,*,/号。-号可以看成是减号后一个数的负数,*号的操作方式是符号的前后两个数直接运算。最后就化简成我们所设想的情况。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
using namespace std;
int num[110];
bool judge(char cha){
if(cha=='+' || cha=='-' || cha=='*' || cha=='/')
return true;
return false;
}
int main(){
int numb;
char c;
int count=0;
while(scanf("%d",&numb)!=EOF){
memset(num,0,110*sizeof(int));
num[0]=numb;
count=0;
while(scanf("%c",&c)!=EOF && judge(c)){
cin>>numb;
if(c=='+')
{
num[++count]=numb;
}
if(c=='-')
{
num[++count]-=numb;
}
if(c=='*')
{
num[count]*=numb;
}
if(c=='/')
{
num[count]/=numb;
}
}
int answer=0;
for(int i=0;i<=count;i++){
answer+=num[i];
}
cout<<answer<<endl;
}
}
相关文章推荐
- qt5.4.0下配置opencv及demo测试
- nginx做了一层负载均衡代理,nginx转发请求给tomcat时,端口号已经丢失掉了
- iOS8之后的UIAlertViewController
- C++ limits头文件的用法(numeric_limits)
- Linux mint 亮度调节
- Count Primes
- 过去的2.5年人生,我得思考自己,反省自己......不管怎么样,不忘初心,继续加油......
- could not load type 3.0.0.0
- 【智能路由器】新手openwrt平台搭建
- 探索工作流(一)--概述
- html
- Standard Shader各种参数的意义
- Swift 构造过程
- 19个MySQL性能优化主要要点
- NOJ 题目1643 阶乘除法(暴力)
- JavaScript提高:006:ASP.NET使用easyUI TABS标签updatepanel
- 韩顺平 javascript教学视频_学习笔记1_js课程介绍_js基本介绍
- SGU 194 Reactor Cooling-上下界网络流模板题
- dp,sp,px之间的转化
- 我的收集:关于找图片