#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef ONLINE_JUDGE
// no getchar_unlocked on Windows so just call getchar
#define gc getchar
#else
#define gc getchar_unlocked
#endif
void multiply(char answer[], char a[], char b[])
{
int la = strlen(a);
int lb = strlen(b);
char c[la+lb+3];
int i,j,k,x;
char temp = '0' - 48;
for(i=0;i<la;i++)
a[i] = a[i] - 48;
for(i=0;i<lb;i++)
b[i] = b[i] - 48;
for(i=0;i<la+lb+2;i++){
c[i] = '0' - 48;
}
for(i=lb-1,x=la+lb+1;i>=0;i--)
{
temp = '0' - 48;
k= x;
for(j=la-1;j>=0;j--)
{
int temp1 = (c[k]+ temp + a[j]*b[i]);
c[k] = (int)temp1%10;
temp = (int)temp1/10;
k--;
}
c[k] = temp;
x--;
}
for(i=0;i<la;i++)
{
a[i] = a[i]+48;
}
for(j=0;j<lb;j++){
b[j] = b[j]+48;
}
while(c[k] == '0'-48)
k++;
int p =0;
for(i=k;i<la+lb+2;i++){
answer[p] = c[i] + 48;
p++;
}
answer[p] = '\0';
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen ("/home/gautam/Dropbox/Programming/SPOJ/input.txt", "r", stdin);
#endif // ONLINE_JUDGE
long int t;
scanf("%ld",&t);
while(t>0){
char a[1002];
char b[1002];
scanf("%s", a);
scanf("%s", b);
if(strlen(a) == 1 && a[0] == '0' || strlen(b) == 1 && b[0] == '0')
printf("0\n");
else{
char *answer;
int la = strlen(a);
int lb = strlen(b);
answer= (char *)malloc(sizeof(char)*(la+lb+3));
multiply(answer,a,b);
printf("%s\n", answer);
}
t--;
}
return 0;
}
问题是将大数字乘以每个最多1000位,这里是链接:Problem。我已经在字符串中输入并使用了数组来进行乘法运算。我已经在ideone上尝试了它,它工作得非常好,但是在SPOJ上它会给出SIGSEV错误。请帮忙。
答案 0 :(得分:1)
您的字符数组 a 和 b 最多只能包含 1002 数字,而问题语句显示“最多 10000 每个十进制数字“。