网络热身赛 Gary's Calculator 哈工大oj上的1314题

超级恶心的一道题目,让我真是怀念于哥的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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值