C语言高精度乘法运算

#include<stdio.h>
#include<string.h>
int c[3000];
//两个数相乘,结果保存在数组c中
void chengfa(char numberN[150000],char numberM[150000]){
    int n = strlen(numberN), m = strlen(numberM);
    int a[n], b[m];
    int i, j;
    for (i = 0, j = n - 1; i < n; i++, j--) {
        a[i] = numberN[j] - '0';
    }
    for (i = 0, j = m - 1; i < m; i++, j--) {
        b[i] = numberM[j] - '0';
    }
    for (i = 0; i < 3000; i++) {
        c[i] = 0;
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            c[i + j] += a[i] * b[j];
        }
    }   
    for (i = 0; i < n + m; i++) {
        if (c[i] >= 10) {
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
    }
}
//输出数组c
void shuchu(){
	int i,j;
    for (j = 2999; j > 0; j--) {
        if (c[j] != 0)
        break;
    }
   	for (i = j; i >= 0; i--) {
   		printf("%d", c[i]);
   	}
}
int main() {
	char numberN[150000], numberM[150000];
    scanf("%s%s", numberN, numberM);
    chengfa(numberN, numberM);
    shuchu();
}
上一篇:Tenka1 Programmer Contest 2019 D - Three Colors


下一篇:P2458 [SDOI2006]保安站岗