C,20分求调!!
查看原帖
C,20分求调!!
1026131
fanerhao楼主2023/7/10 23:59
#include <stdio.h>
#include<string.h>
#include<math.h>
#define A 501

int Pow(int a, int b) {
	
	int result = 1;
	for (int i = 0; i < b; i++) {
		result *= a;
	}
	return result;
}

void func(int n[], char m[], int x, int len) {
	
	int j = 0;
	for (int i = len - 1; i >= 0; i--) {
		n[j / x] += (m[i] - '0') * Pow(10, j % x);
		j++;
	}
}

int main() {
	
	char a[A], b[A];
	gets(a), gets(b);
	
	int step = 4, carry = 0;
	int maxlen, resultlen;
	int numa[A] = {0}, numb[A] = {0}, numc[A] = {0};
	func(numa, a, step, strlen(a));
	func(numb, b, step, strlen(b));
	maxlen = strlen(a) > strlen(b) ? strlen(a) : strlen(b);
	
	for (int i = 0; i <= maxlen / step; i++) {
		numc[i] = (numa[i] + numb[i]) % Pow(10, step) + carry;
		carry = (numa[i] + numb[i]) / Pow(10, step);
	}
	
	resultlen = numc[maxlen / step] > 0 ? maxlen/step : maxlen/step - 1;
	printf("%d", numc[resultlen]);
	for (int i = resultlen - 1; i >= 0; i--) {
		printf("%04d", numc[i]);
	}
}
2023/7/10 23:59
加载中...