64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
freopen("/home/miao/Documents/program/c++/HOJ/in.txt", "r", stdin);
char str1[1010], str2[1010];
int t, str_len1, str_len2, str_max, k, num = 1;
int a[1010] = {0}, b[1010] = {0}, c[1010] = {0};
std::cin>>t;
while(t--)
{
std::cin>>str1;
str_len1 = strlen(str1);
for (int i=0; i<str_len1; i++)
a[i] = str1[str_len1 - 1 - i] - '0'; //because str1 is string, so we should convert it to integer, sub the '0', ASCII code.
std::cin>>str2;
str_len2 = strlen(str2);
for (int i=0; i<str_len2; i++)
b[i] = str2[str_len2 - 1 - i] - '0';
if (str_len1 > str_len2)
str_max = str_len1;
else
str_max = str_len2;
k = 0;
for (int i=0; i<str_max; i++)
{
c[i] = (a[i] + b[i] + k) % 10;
k = (a[i] + b[i] + k) / 10;//进位
}
if (k != 0)
c[str_max] = 1;
else
c[str_max] = 0;
std::cout<<"Case "<<num<<":"<<endl;
num++;
std::cout<<str1<<" + "<<str2<<" = ";
if (c[str_max] == 1)
std::cout<<"1";
for (int i = str_max-1; i >= 0; i--)
std::cout<<c[i];
std::cout<<endl;
if (t >= 1)
std::cout<<endl;
}
return 0;
}
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j<n;j++) {
scanf("%s%s",a1,a2);
L=strlen(a1);
for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';
L=strlen(a2);
for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';
for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];
for (i=0;i<MAXLEN;i++) {
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}
printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);
z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");
printf("\n");
}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110