网络热身赛 Gary's Calculator 哈工大oj上的1314题
来源:互联网 发布:北京网络职业学院 编辑:程序博客网 时间:2024/05/17 23:02
超级恶心的一道题目,让我真是怀念于哥的java啊!!!用java做,10分钟肯定搞定!
注意数组不可开的太小,而且,每次记得刚开始记得清0(wa了好多次啊),我的方法是把乘法单独计算完,然后加上其他的数
哈工大网址http://www.paopaogu.cn/index.php?act=problem&id=1314
hdu 公开热身赛题目:
Problem Description
Gary has finally decided to find a calculator to avoid making simple calculational mistakes in his math exam. Unable to find a suitable calculator in the market with enough precision, Gary has designed a high-precision calculator himself. Can you help him to write the necessary program that will make his design possible?
For simplicity, you only need to consider two kinds of calculations in your program: addition and multiplication. It is guaranteed that all input numbers to the calculator are non-negative and without leading zeroes.
Input
There are multiple test cases in the input file. Each test case starts with one positive integer N (N < 20), followed by a line containing N strings, describing the expression which Gary's calculator should evaluate. Each of the N strings might be a string representing a non-negative integer, a "*", or a "+". No integer in the input will exceed 109.
Input ends with End-of-File.
Output
For each test case, please output one single integer (with no leading zeros), the answer to Gary's expression. If Gary's expression is invalid, output "Invalid Expression!" instead. Please use the format indicated in the sample output.
SampleInput
3
100 + 600
3
20 * 4
2
+ 500
5
20 + 300 * 20
SampleOutput
Case 1: 700
Case 2: 80
Case 3: Invalid Expression!
Case 4: 6020
#include <iostream>#define max(a,b) ((a)>(b)?(a):(b))#include<cstdio>#include<cstring>using namespace std;char ss[25][13];char result[300];char change[25];int sum[300],mark[25],total[300];void mulp(){ int i,j,alen,blen,u; alen=strlen(result);blen=strlen(change); for(i=0;i<=alen+blen-1;i++) { sum[i]=0; } for(i=0;i<=alen-1;i++) { for(j=0;j<=blen-1;j++) { sum[i+j]=sum[i+j]+(result[i]-'0')*(change[j]-'0'); } } for(i=0;i<alen+blen-1;i++) { if(sum[i]>=10) { sum[i+1]=sum[i+1]+sum[i]/10; sum[i]=sum[i]%10; } }for(i=0;i<alen+blen-1;i++){result[i]=sum[i]+'0';}}int main(){ int n,i,j,x,k,w,v,flag,count=0,flag1;char char1; while(scanf("%d",&n)!=EOF) {count++;memset(total,0,sizeof(total));memset(mark,0,sizeof(mark));memset(ss,0,sizeof(ss)); for(i=0;i<n;i++) cin >> ss[i];if(n%2==0){printf("Case %d: Invalid Expression!\n",count);continue;}flag1=1;for(i=0;i<n;i+=2){if(ss[i][0]=='+'||ss[i][0]=='*')flag1=0;//这里不能出现break,因为要把整个字符串都读取看看是否符合条件,这错了几次 }for(i=1;i<n;i+=2){if(ss[i][0]!='+'&&ss[i][0]!='*')flag1=0;//同上 }if(flag1==0){printf("Case %d: Invalid Expression!\n",count);continue;}flag=0;for(i=1;i<n;i+=2){if(ss[i][0]=='*'){mark[i-1]=1;mark[i+1]=1;if(flag==0){ memset(result,0,sizeof(result)); v=0; for( k=strlen(ss[i-1])-1;k>=0;k--) {result[v++]=ss[i-1][k]; }}memset(change,'0',sizeof(change));v=0;for(k=strlen(ss[i+1])-1;k>=0;k--){change[v++]=ss[i+1][k];}mulp();if(i<=n-2&&ss[i+2][0]=='*'){ flag=1;}else {flag=0; for(w=0;w<strlen(result)-1;w++){total[w]+=result[w]-'0';if(total[w]>=10){total[w]-=10;total[w+1]++;}}}}}for(i=0;i<n;i+=2){if(!mark[i]){memset(change,'0',sizeof(change));v=0;for(k=strlen(ss[i])-1;k>=0;k--){change[v++]=ss[i][k];}for(w=0;w<=strlen(change)-1;w++){total[w]+=change[w]-'0';if(total[w]>=10){total[w]-=10;total[w+1]++;}}}}int f;for(f=200;f>=0;f--){if(total[f]!=0)break;}printf("Case %d: ",count);if(f==-1)printf("0");else {for(i=f;i>=0;i--) printf("%d",total[i]);}printf("\n"); } return 0;}
- 网络热身赛 Gary's Calculator 哈工大oj上的1314题
- 【JAVA】Gary's Calculator
- HDU Gary's Calculator
- B - Gary's Calculator
- HDU 2424-Gary's Calculator
- hdu2424 Gary's Calculator 高精度计算
- HDU 2424 Gary's Calculator 模拟 + 高精度
- HDU 2424 Gary's Calculator(细节)
- HDU 2424-Gary's Calculator(表达式计算+大数)
- B - Gary's Calculato
- 【哈工大oj】1037 - 组合数末尾的零(位运算,好题)
- HIT OJ 1069 哈工大
- HIT OJ 1076 哈工大OJ
- HIT OJ 1087 哈工大OJ
- HIT OJ 1156 哈工大OJ
- 哈工大第二次热身赛1006(悲剧team12 c9q7m9t4)
- HDU 4706 Children's Day (热身赛第一题)
- HDU 4726 Kia's Calculation热身赛2 1011题(贪心 每次都找最大位的放在前面)
- 从GridView中导出全部页数据示例
- ResolveUrl() 和 ResolveClientUrl()
- c# 树状视图(TreeView类)
- 在asp.net中对url地址统一编码的示例
- java代码缺陷自动分析工具之FindBugs介绍
- 网络热身赛 Gary's Calculator 哈工大oj上的1314题
- 在C#得到指定日期所在周的第一天与最后一天的方法
- VC++ 读取新浪证券行情接口(Level 1行情)
- 传智播客开课的第三天
- 为什么要交流
- 利用DevExpress的WebChartControl绘制柱状图,线状图与饼状图示例
- Android自定义Button按钮显示样式
- 在.net(C#)中查找本地及局域网内所有sql server实例
- LINUX系统延时函数