acm简单题目,但是我写的就是wrong answer。求高人指导啊
Ihaveaverysimpleproblemforyou.GiventwointegersAandB,yourjobistocalculatetheSumofA+B.I...
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:
112233445566778899 + 998877665544332211 = 1111111111111111110
下面是我写的程序
#include <iostream>
#include <string.h>
using namespace std;
#define max 1010
int main()
{
int line,a[max]={0},b[max]={0},c[max]={0},l1,l2,l3,i,j=1,flag;
char A[max],B[max];
cin>>line;
while((line--)&&cin>>A>>B)
{
cout<<"Case "<<j<<":"<<endl;
j++;
l1=strlen(A);
l2=strlen(B);
for(i=0;i<l1;i++)
a[l1-1-i]=A[i]-'0';
for(i=0;i<l2;i++)
b[l2-1-i]=B[i]-'0';
for(i=0;i<l1||i<l2;i++)
{
flag=a[i]+b[i];
if(flag<10)
c[i]=c[i]+flag;
else
{
c[i+1]=1;
c[i]=c[i]+flag%10;
}
}
if(c[i]!=0)
l3=i+1;
else
l3=i;
for(i=l1-1;i>=0;i--)
cout<<a[i];
cout<<"+";
for(i=l2-1;i>=0;i--)
cout<<b[i];
cout<<"=";
for(i=l3-1;i>=0;i--)
cout<<c[i];
cout<<endl;
if(line!=0)
cout<<endl;
}
return 0;
}
求哥哥姐姐们指导啊~~ 展开
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:
112233445566778899 + 998877665544332211 = 1111111111111111110
下面是我写的程序
#include <iostream>
#include <string.h>
using namespace std;
#define max 1010
int main()
{
int line,a[max]={0},b[max]={0},c[max]={0},l1,l2,l3,i,j=1,flag;
char A[max],B[max];
cin>>line;
while((line--)&&cin>>A>>B)
{
cout<<"Case "<<j<<":"<<endl;
j++;
l1=strlen(A);
l2=strlen(B);
for(i=0;i<l1;i++)
a[l1-1-i]=A[i]-'0';
for(i=0;i<l2;i++)
b[l2-1-i]=B[i]-'0';
for(i=0;i<l1||i<l2;i++)
{
flag=a[i]+b[i];
if(flag<10)
c[i]=c[i]+flag;
else
{
c[i+1]=1;
c[i]=c[i]+flag%10;
}
}
if(c[i]!=0)
l3=i+1;
else
l3=i;
for(i=l1-1;i>=0;i--)
cout<<a[i];
cout<<"+";
for(i=l2-1;i>=0;i--)
cout<<b[i];
cout<<"=";
for(i=l3-1;i>=0;i--)
cout<<c[i];
cout<<endl;
if(line!=0)
cout<<endl;
}
return 0;
}
求哥哥姐姐们指导啊~~ 展开
2个回答
展开全部
#include <iostream>
#include <string.h>
using namespace std;
#define max 1010
int main()
{
int line,a[max]={0},b[max]={0},c[max]={0},l1,l2,l3,i,j=1,flag;
char A[max],B[max];
cin>>line;
while((line--)&&cin>>A>>B)
{
cout<<"Case "<<j<<":"<<endl;
j++;
l1=strlen(A);
l2=strlen(B);
for(i=0;i<l1;i++)
a[l1-1-i]=A[i]-'0';
for(i=0;i<l2;i++)
b[l2-1-i]=B[i]-'0';
flag = 0; //进位标志
for(i=0;i<l1||i<l2;i++)
{
c[i]=flag;
if(i<l1)c[i]+=a[i]; //防止访问越界的数出错
if(i<l2)c[i]+=b[i];
if(c[i]<10)
flag=0;
else
{
flag=1;
c[i]=c[i]-10;
}
}
if(flag)c[i++]=1;
l3=i;
for(i=l1-1;i>=0;i--)
cout<<a[i];
cout<<" + "; //空格
for(i=l2-1;i>=0;i--)
cout<<b[i];
cout<<" = ";
for(i=l3-1;i>=0;i--)
cout<<c[i];
cout<<endl;
if(line!=0)
cout<<endl;
}
return 0;
}
#include <string.h>
using namespace std;
#define max 1010
int main()
{
int line,a[max]={0},b[max]={0},c[max]={0},l1,l2,l3,i,j=1,flag;
char A[max],B[max];
cin>>line;
while((line--)&&cin>>A>>B)
{
cout<<"Case "<<j<<":"<<endl;
j++;
l1=strlen(A);
l2=strlen(B);
for(i=0;i<l1;i++)
a[l1-1-i]=A[i]-'0';
for(i=0;i<l2;i++)
b[l2-1-i]=B[i]-'0';
flag = 0; //进位标志
for(i=0;i<l1||i<l2;i++)
{
c[i]=flag;
if(i<l1)c[i]+=a[i]; //防止访问越界的数出错
if(i<l2)c[i]+=b[i];
if(c[i]<10)
flag=0;
else
{
flag=1;
c[i]=c[i]-10;
}
}
if(flag)c[i++]=1;
l3=i;
for(i=l1-1;i>=0;i--)
cout<<a[i];
cout<<" + "; //空格
for(i=l2-1;i>=0;i--)
cout<<b[i];
cout<<" = ";
for(i=l3-1;i>=0;i--)
cout<<c[i];
cout<<endl;
if(line!=0)
cout<<endl;
}
return 0;
}
意法半导体(中国)投资有限公司
2023-06-12 广告
2023-06-12 广告
单片机,即单片微控制器,也称为单片微型计算机,是将中央处理器(CPU)、存储器(ROM,RAM)、输入/输出接口和其他功能部件集成在一块 在一个小块的集成电路上,从而实现对整个电路或系统的数字式控制。单片机不是完成某一个逻辑功能的芯片,而是...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询