UVA 10106 - Product
将数学运算过程程序化,注意地址的变动,缩位进位就可以啦!
#include<stdio.h>
#include<string.h>
#define MAXN1 250 + 10
#define MAXN2 500 + 10
char s1[MAXN1], s2[MAXN1];
int num[MAXN2],num1[MAXN1],num2[MAXN1];
void solve()
{
int len1 = strlen(s1);
for(int i = 0; i < len1; i ++)
num1[i] = s1[len1-i-1] - '0';
int len2 = strlen(s2) ;
for(int j = 0; j < len2; j ++)
num2[j] = s2[len2-1-j] - '0';
for(int p = 0; p < len2; p ++)
{
for(int q = 0; q < len1; q ++)
{
num[p+q] += num2[p] * num1[q];//printf("num[p+q]=%d",num[p+q]);
if(num[p+q]>9)
{
num[p+q+1] += num[p+q]/10;
num[p+q] %= 10;
}
}
}
}
void output()
{
int i;
for(i = MAXN2; i >= 0; i --)
{
if(i == 0) break;
if(num[i] != 0) break;
}
for(int j = i; j >=0; j --)
printf("%d",num[j]);
printf("\n");
}
void input()
{
while(scanf("%s",s1) == 1)
{
memset(num,0,sizeof(num));
scanf("%s",s2);
solve();
output();
}
}
int main()
{
input();
return 0;
}