Buscar

526 Some dois números grandes

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

#include <stdio.h>
#include <string.h>
#define MAX_NUM 100000
char N1[MAX_NUM + 1];
char N2[MAX_NUM + 1];
char resp[MAX_NUM + 2];
void preencher(char N[], int max) {
	int i, m = strlen(N);
	if(strlen(N) < max) {
		for(i = strlen(N); i > 0; i--, max--) {
				N[max] = N[i - 1];
		}
		for(i = max; i >= 0; i --) {
			N[i] = '0';
		}
	}
}
void soma() {
	int i, x, j;
	resp[0] = '0';
	for(i = strlen(N1) - 1, j = 0; i >= 0; i--, j++) {
		x = ((N1[i] - '0') + (N2[i] - '0'));
		if((resp[j] == '1') && (x == 9)) {
			resp[j + 1] = '1';
			resp[j] = '0';
		} else if(x >= 10) {
			resp[j + 1] = '1';
			resp[j] = ((resp[j] - '0') + (x % 10)) + '0';
		} else {
			resp[j + 1] = '0';
			resp[j] = ((resp[j] - '0') + (x % 10)) + '0';
		}
	}
}
int main() {
	int i;
	scanf("%s\n%s", N1, N2);
	if(strlen(N1) > strlen(N2)) {
		preencher(N2, strlen(N1) - 1);
	} else{
		preencher(N1, strlen(N2) - 1);
	}
	soma();
	for(i = strlen(resp) - 1; i >= 0; i--){
		if((i == strlen(resp) - 1) && (resp[i] == '0')) {
		} else {
			printf("%c", resp[i]);
		}
	}
	printf("\n");
	return 0;
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando