其他分享
首页 > 其他分享> > C语言高精度乘法运算

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();
}

标签:150000,高精度,int,numberM,++,C语言,numberN,--,乘法
来源: https://blog.csdn.net/ziyue13/article/details/116892294