现在的位置: 首页 > 综合 > 正文

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

2018年04月26日 ⁄ 综合 ⁄ 共 3117字 ⁄ 字号 评论关闭

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

 

【上篇】
【下篇】

抱歉!评论已关闭.