@xunuo
2017-01-18T21:16:34.000000Z
字数 3046
阅读 1169
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
来源:
HDU:HDU 2424 ary's Calculator
vjudge:B-ary's Calculator
高精度
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.
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.
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.
3
100 + 600
3
20 * 4
2
+ 500
5
20 + 300 * 20
Case 1: 700
Case 2: 80
Case 3: Invalid Expression!
Case 4: 6020
题意
高精度的加法和乘法的连续计算
代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[105],b[105],c[105];
char s[105],ss1[105],ss2[105],sum[105];
void add(char s1[],char s2[],char sum1[])///高精度加法......模板
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
int l1=strlen(s1);
int l2=strlen(s2);
for(int i=0;i<l1;i++)
a[i]=s1[l1-1-i]-'0';
for(int i=0;i<l2;i++)
b[i]=s2[l2-1-i]-'0';
int l=(l1>l2)?l1:l2;
for(int i=0;i<l;i++)
{
c[i]+=(a[i]+b[i]);
if(c[i]>=10)
{
c[i]-=10;
c[i+1]+=1;
}
}
if(c[l]>0)
l++;
for(int i=l;i>0;i--)
{
if(c[i]==0)
l--;
else
break;
}
for(int i=l;i>=0;i--)
sum1[l-i]=c[i]+'0';
}
void mult(char s1[],char s2[],char mul[])///高精度乘法.....模板
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
int l1=strlen(s1);
int l2=strlen(s2);
for(int i=0;i<l1;i++)
a[i]=s1[l1-1-i]-'0';
for(int i=0;i<l2;i++)
b[i]=s2[l2-1-i]-'0';
for(int i=0;i<l1;i++)
for(int j=0;j<l2;j++)
{
c[i+j]+=a[i]*b[j];
if(c[i+j]>=10)
{
c[i+j+1]+=(c[i+j])/10;
c[i+j]=c[i+j]%10;
}
}
int l=l1+l2;
for(int i=l;i>0;i--)
{
if(c[i]==0)
l--;
else
break;
}
for(int i=l;i>=0;i--)
mul[l-i]=c[i]+'0';
}
int main()
{
int n;
int t=1;
while(scanf("%d",&n)!=EOF)
{
memset(ss1,'\0',sizeof(ss1));
memset(ss2,'\0',sizeof(ss2));
int vis=1,flag=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(n%2==0)///输入为偶数个时,不符合条件
vis=0;
else
{
if(i%2==1)///当该输入的是数字时
{
if(s[0]=='+'||s[0]=='*')///出现了'+'与'*',不符合条件
{
vis=0;
continue;
}
if(flag==0)///当出现'+'号时
{
memset(sum,'\0',sizeof(sum));///注意每次清空
add(ss1,ss2,sum);
strcpy(ss1,sum);///手动跑一次就明白了
strcpy(ss2,s);
}
else
{
/*当出现'*'号时*/
memset(sum,'\0',sizeof(sum));
mult(ss2,s,sum);
strcpy(ss2,sum);
}
}
/*为奇数,该输出符号时*/
else
{
if(strlen(s)!=1&&(s[0]!='+'||s[0]!='*'))///在其间输出了其他的东西
{
vis=0;
continue;
}
else
{
if(s[0]=='+')///+号
flag=0;
else
flag=1;///*号
}
}
}
//printf("%s\n",sum);
}
add(ss1,ss2,sum);///最后还要加一次
printf("Case %d: ",t);
t++;
if(vis==0)
printf("Invalid Expression!\n");
else
printf("%s\n",sum);///顺序输出
}
return 0;
}