HDU 1002 A + B Problem II
2011-12-06 14:02
330 查看
大数相加题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
#include<stdio.h>
#include<string>
#define maxn 1010
int a[maxn], b[maxn];
int main()
{
int s,len1,len2,i,j,k,up,count=1;
char str1[1001],str2[1001];
scanf("%d", &s);
for(j=0;j<s;j++)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%s",str1);
scanf("%s",str2);
len1=strlen(str1);
len2=strlen(str2);
for(i=len1-1,k=0;i>=0;i--)
{
a[k++]=str1[i]-'0';
}
for(i=len2-1,k=0;i>=0;i--)
{
b[k++]=str2[i]-'0';
}
for(i=0,up=0;i<maxn;i++)
{
a[i]=a[i]+b[i]+up;
up=a[i]/10;
a[i]=a[i]%10;
}
for(i=maxn-1;i>=0;i--)
if(a[i]) break;
printf("Case %d:\n", count++);
printf("%s + %s = ",str1,str2);
for(k=i;k>=0;k--)
printf("%d", a[k]);
printf("\n");
if(j<s-1)//**格式**//
printf("\n");
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
#include<stdio.h>
#include<string>
#define maxn 1010
int a[maxn], b[maxn];
int main()
{
int s,len1,len2,i,j,k,up,count=1;
char str1[1001],str2[1001];
scanf("%d", &s);
for(j=0;j<s;j++)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%s",str1);
scanf("%s",str2);
len1=strlen(str1);
len2=strlen(str2);
for(i=len1-1,k=0;i>=0;i--)
{
a[k++]=str1[i]-'0';
}
for(i=len2-1,k=0;i>=0;i--)
{
b[k++]=str2[i]-'0';
}
for(i=0,up=0;i<maxn;i++)
{
a[i]=a[i]+b[i]+up;
up=a[i]/10;
a[i]=a[i]%10;
}
for(i=maxn-1;i>=0;i--)
if(a[i]) break;
printf("Case %d:\n", count++);
printf("%s + %s = ",str1,str2);
for(k=i;k>=0;k--)
printf("%d", a[k]);
printf("\n");
if(j<s-1)//**格式**//
printf("\n");
}
return 0;
}
相关文章推荐
- HDU 1002 A + B Problem II
- [ACM]hdu 1002 A + B Problem II (复习大数相加)
- hdu 1002 A + B Problem II
- hdu1002 A + B Problem II
- [ACM]hdu 1002 A + B Problem II (复习大数相加)
- HDU 1002 A + B Problem II
- [HDU1002] A + B Problem II
- hdu 1002 A + B Problem II
- HDU1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU_1002 A + B Problem II
- hdu_1002 A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu 1002 A + B Problem II(大数)
- HDU 1002 A + B Problem II
- hdu 1002.A + B Problem II
- hdu 1002 A+B Problem II