#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;



 }

问题是将大数乘以atmost,每个1000位,这是链接:Problem。我用字符串输入并用数组乘法。我在ideone上试过,它工作得非常好,但在SPOJ上却出现了SIGSEV错误。请帮忙。

最佳答案

你的字符数组a和b最多只能容纳1002位数字,而问题语句是“每个最多10000位十进制数字”。

关于c - SPOJ上的SIGSEGV错误,通过代码在ideone上正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20646756/

10-11 21:05