Succeeded on local but failed online.Using input data:"93553535314", "25247452591474"


  • 0
    R

    Input: "93553535314", "25247452591474"
    Output: "236198844760500367431283'" (why is a '?)
    Expected: "2361988447605003674312836"

    char* multiply(char* num1, char* num2) {
        char r1[100], r2[100], mult[200];			//变量声明和初始化
    	unsigned long result[200];                  //declare&initialize
    	long i, j, k;
    	unsigned long reg;
    
    	for (i = 0; i < 200; i++)
    	{
    		result[i] = 0;
            mult[i]='\0';
    	}
    
    	if (num1[0] == 48 || num2[0] == 48)                     //nil participates the muliplication is no possibile 
    	{	
    	    return "0";
    	}
    	else
    	{
    		j = strlen(num1);										//乘数被乘数倒序,方便进位运算;char转long,避免溢出
    		for (i = 0; i < j; i++)                                 //reverse the  sequence of the array,in oreder to be imprehensive;
    			r1[j - i - 1] = num1[i] - 48;                       // the char string was changed into long array, in order to 
    		r1[j] = -1;                                             //reach a higher digit
    
    		j = strlen(num2);
    		for (i = 0; i<j; i++)
    			r2[j - i - 1] = num2[i] - 48;
    		r2[j] = -1;
    
    		for (i = 0; r1[i] != -1; i++)						//按位相乘并累加进位
    		{                                                   //every digit of two long data was mutiplied; the result was recorded in 
    			for (j = 0; r2[j] != -1; j++)                   //the result[] one by one; no element would be more than 81;so, if the 
    			{                                               //element is more than 9, the carry digit should be count to the  
    				result[i + j] += r1[i] * r2[j];             //sequencial element.this step will repeat as many times as the digits 
    				if (result[i + j]>9)                        //multiplier(r1) has
    				{
    					result[i + j + 1] += result[i + j] / 10;
    					result[i + j] %= 10;
    				}
    			}
    
    		}
    
    		k = i + j;
    		if (!result[k - 1])									//去掉最高位的0进位
    			k--;                                            //to kill the highest 0 digit
    
    		for (i = 0; i < k; i++)								//long转char,
    			mult[k - i - 1] = result[i] + 48;               //long array changes to char string 
    		mult[k] = '\0';
    	
    	return mult;}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.