hdu2424Gary's Calculator
来源:互联网 发布:淘宝助理官方版下载 编辑:程序博客网 时间:2024/05/17 22:26
Gary's Calculator
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 857 Accepted Submission(s): 188
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.
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.
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.
Sample Input
3100 + 600320 * 42+ 500520 + 300 * 20
Sample Output
Case 1: 700Case 2: 80Case 3: Invalid Expression!Case 4: 6020
Source
2008 Asia Hangzhou Regional Contest Online
Recommend
lcy
这道题是我第一次套用模板。
用了两个模板。
高精度乘法,和高精度加法。。
本来这道题挺简单的,但是让我漏了几个条件,wa了十几次。。
还有看了一些前辈的blog,感到用java就能十分钟做出来。因为java已经有高精度了。。
#include<stdio.h>#include<string.h>#include<malloc.h>int n;char ch[15];void mult(char a[],char b[],char s[]){ int i,j,k=0,alen,blen,sum=0,res[150][150]={0},flag=0; char result[150]; alen=strlen(a);blen=strlen(b); for (i=0;i<alen;i++)for (j=0;j<blen;j++) res[i][j]=(a[i]-'0')*(b[j]-'0');for (i=alen-1;i>=0;i--) { for (j=blen-1;j>=0;j--) sum=sum+res[i+blen-j-1][j]; result[k]=sum%10; k=k+1; sum=sum/10; }for (i=blen-2;i>=0;i--) { for (j=0;j<=i;j++) sum=sum+res[i-j][j]; result[k]=sum%10; k=k+1; sum=sum/10; }if (sum!=0) {result[k]=sum;k=k+1;}for (i=0;i<k;i++) result[i]+='0';for (i=k-1;i>=0;i--) s[i]=result[k-1-i];s[k]='\0';while(1) {if (strlen(s)!=strlen(a)&&s[0]=='0') strcpy(s,s+1);elsebreak; }}void add(char a[],char b[],char back[]){ int i,j,k,up,x,y,z,l; char *c; if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2; c=(char *) malloc(l*sizeof(char)); i=strlen(a)-1; j=strlen(b)-1; k=0;up=0; while(i>=0||j>=0){if(i<0) x='0'; else x=a[i];if(j<0) y='0'; else y=b[j];z=x-'0'+y-'0';if(up) z+=1;if(z>9) {up=1;z%=10;} else up=0;c[k++]=z+'0';i--;j--;} if(up) c[k++]='1'; i=0; c[k]='\0'; for(k-=1;k>=0;k--) back[i++]=c[k]; back[i]='\0';} int main(){int flag,flag2,ca = 1;char pre1[102],pre2[102],b[102];while(scanf("%d",&n)!=EOF){flag = 1,flag2 = 1;memset(pre1,'\0',sizeof(pre1));memset(pre2,'\0',sizeof(pre2));for(int i=1;i<=n;i++){scanf("%s",ch);if(n%2==0) //一直漏了这个条件,wa呀waflag=0;if(flag){if(i%2==1 ){if(ch[0]=='+'||ch[0]=='*')//还有这个{flag = 0;continue;}if(flag2==1){memset(b,'\0',sizeof(b));add(pre1,pre2,b);strcpy(pre1,b);strcpy(pre2,ch);}else{memset(b,'\0',sizeof(b));mult(pre2,ch,b);strcpy(pre2,b);}}else{if((ch[0]!='+' && ch[0]!='*') || strlen(ch)!=1){flag = 0;continue;/*printf("Case %d: Invalid Expression!\n",ca++);break;*/}if(ch[0]=='+')flag2 = 1; //1代表加法elseflag2 = 2; //2代表乘法}}}add(pre1,pre2,b);if(!flag )printf("Case %d: Invalid Expression!\n",ca++);else{printf("Case %d: ",ca++);int i=0;while(b[i]=='0')i++;if(i==strlen(b))printf("0");for(;i<strlen(b);i++)printf("%c",b[i]);printf("\n");}}return 0;}
- hdu2424Gary's Calculator
- Android's calculator 计算器
- 【JAVA】Gary's Calculator
- HDU Gary's Calculator
- B - Gary's Calculator
- HDU 2424-Gary's Calculator
- B. Grandfather Dovlet’s calculator
- 算法训练 Beaver's Calculator
- pku 1421 poj 1421 Peter's calculator
- hdu2424 Gary's Calculator 高精度计算
- Codeforces-Edu6-B. Grandfather Dovlet’s calculator
- CodeForces 620B Grandfather Dovlet’s calculator
- B. Grandfather Dovlet’s calculator【水题】
- Beaver's Calculator 【归并排序+贪心】
- HDU 2424 Gary's Calculator 模拟 + 高精度
- 蓝桥杯- 算法训练-Beaver's Calculator
- Beaver's Calculator coderforces 70A1 蓝桥杯
- HDU 2424 Gary's Calculator(细节)
- 实模式与保护模式
- 数据结构与算法学习笔记——队列
- 英语从头学
- 谈USB硬件设备
- JavaSwing图形界面编程之JTable(二)
- hdu2424Gary's Calculator
- python中的invalid syntax问题的解决
- java基础入门----Collections1 集合框架工具类
- Socket I/O 模型 学习
- Android 漫游之路------Android电话拨号器(点击事件的几种写法)、Android短信发送器
- C++字符串string常用函数使用说明
- OCP-1Z0-052-V8.02-25题
- struts2+hibernate
- 程序员的样子