HUD-1002


title: HDU-1002
categories:

  • ACM
  • 大数
    tags:
  • 大数
  • 大数加法
    date: 2020-02-06 18:50:21

此题在杭电oj上有问题,AC了也不一定代表可以正确,还有dev-cpp5.4.0这个环境,结束时自动输出一行换行

题目

A + B Problem II

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 514958 Accepted Submission(s): 98584
*

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:

步骤

代码1:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{	
 	//freopen("input.txt", "r", stdin);
    int n,ai,ci,j,sum,jinwei;
    char a[1000];
	char c[1000]; 
	char he[1000]; 
   	cin>>n;
    for(int k=1;k<=n;k++)
    {
    cout<<"Case "<<k<<":"<<endl;
    cin>>a>>c;
    cout<<a<<" + "<<c<<" = ";
    ai=strlen(a)-1;
    ci=strlen(c)-1;
 	jinwei=0;
 	int hei=0;
 	while(ai>=0&&ci>=0)
 	{
 		sum=a[ai]-'0'+c[ci]-'0'+jinwei;
 		he[hei++]=sum%10+'0';
 		jinwei=sum>=10?1:0;	
 		ai--;
 		ci--;	
 	}
	 while(ai>=0)
 	{
 		sum=a[ai]-'0'+jinwei;
 		he[hei++]=sum%10+'0';
 		jinwei=sum>=10?1:0;	
 		ai--;
 	}
 	while(ci>=0){
 		sum=c[ci]-'0'+jinwei;
 		he[hei++]=sum%10+'0';
 		jinwei=sum>=10?1:0;	
 		ci--;
 	}
 	hei--;
 	if(jinwei!=0)
 	cout<<jinwei;
 	while(hei>=0)
 	cout<<he[hei--];	
 	cout<<endl;
 	if(k!=n)
 	cout<<endl;
}
}

错误的AC代码(未考虑进位,结果错误,但还是AC了,输出1+9=0):

由此可见,杭电oj部分题目的后台测试还是不太准确

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{	
 	//freopen("input.txt", "r", stdin);
    int n,b,d;
    cin>>n;
    char a[1000];
	char c[1000];  
    //cin>>n;
    for(int k=1;k<=n;k++)
    {
    cout<<"Case "<<k<<":"<<endl;
    cin>>a>>c;
    cout<<a<<" + "<<c<<" = ";
	int j;
	for(j=0;j<1000;j++)
    if(a[j]=='\0')
    break;
    b=j;
	for(j=0;j<1000;j++)
    if(c[j]=='\0')
    break;
    d=j;
    //cout<<b<<d;
    int min=b;
 	if(min>d)
 	min=d;
 	int jinwei=0;
 	int sum;
 	for(int i=1;i<=min;i++){
 		sum=a[b-i]-'0'+c[d-i]-'0'+jinwei;
 		c[d-i]=sum%10+'0';
 		//cout<<c[d-i]<<"";
 		jinwei=sum/10;
 		
 	} 
 	//cout<<endl;
 	if(min==b)
  {
  	c[d-min-1]+=jinwei;
  	cout<<c;
  	
  cout<<endl;
  }
  else{
  	a[b-min-1]+=jinwei;
  for(int i=0;i<=b-min-1;i++)
  		cout<<a[i];
  	for(int i=d-min;i<=d-1;i++)
  	cout<<c[i];
  	
  	cout<<endl;
  }if(k!=n)
  cout<<endl;
}

}
发布了25 篇原创文章 · 获赞 1 · 访问量 498

猜你喜欢

转载自blog.csdn.net/qq_43985303/article/details/104212289
HUD