精华内容
下载资源
问答
  • 大数相乘

    2016-07-22 09:28:56
    大数相乘
    # include <stdio.h>
    # include <string.h>
    
    #define MAX 1005
    
    void chtoint(int num[], char ch[], int len);
    
    int main(void)
    {
    	char s1[MAX];
    	char s2[MAX];
    	int n;
    	scanf("%d", &n);
    	while (n--)
    	{
    		int num1[MAX] = {0};
    		int num2[MAX] = {0};
    		int cmul[MAX] = {0};
    		int csum[MAX] = {0};
    		scanf("%s %s", s1, s2);
    		int len1 = strlen(s1);
    		int len2 = strlen(s2);
    		chtoint(num1, s1, len1);
    		chtoint(num2, s2, len2);
    		int i, j, x;
    		for (i = 0; i < len1; i++)
    		{
    			int mul = 0;
    			int c = 0;
    			memset(cmul, 0, sizeof(cmul));
    			for (j = 0; j <= len2; j++)
    			{
    				mul = num1[i] * num2[j] + c;
    				cmul[j] = mul % 10;
    				//printf("cmul[%d] = %d\n", j, cmul[j]);
    				c = mul / 10;
    				//printf("c = %d\n", c);
    			}
    			int sum;
    			int ci = 0;
    			for (j = 0, x = i; x < MAX , j < MAX; x++)
    			{
    				sum = csum[x] + cmul[j++] + ci;
    				csum[x] = sum % 10;
    				ci = sum / 10;
    			}
    		}
    		for (i = MAX-1; i >= 0; i--)
    		{
    			if (csum[i] != 0)
    			{
    				break;
    			}
    		}
    		for (; i >= 0; i--)
    		{
    			printf("%d", csum[i]);
    		}
    		printf("\n");
    	}
    	return 0;
    }
    
    void chtoint(int num[], char ch[], int len)
    {
    	int i, j;
    	for (i = len - 1, j = 0; i >= 0; i--)
    	{
    		num[j++] = ch[i] - '0';
    	}
    	return ;
    }

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,422
精华内容 968
关键字:

大数相乘