大数加法
/***洛谷P1601 A+B Problem(高精)***/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 10005
char num1[maxn], num2[maxn];
int _num1[maxn], _num2[maxn];
int _max(int a, int b){
return a > b ? a : b;
}
void _add(char a[], char b[]){
int i, j;
int l_1 = strlen(a);
int l_2 = strlen(b);
int len = _max(l_1, l_2);
for(i = l_1 - 1, j = 0; i >= 0; i--, j++){
_num1[j] = (int)(a[i] - '0');
}
for(i = l_2 - 1, j = 0; i >= 0; i --, j++){
_num2[j] = (int)(b[i] - '0');
}
_num1[len] = 0;
for(i = 0; i <= len; i++){
_num1[i] += _num2[i];
if(_num1[i] > 9){
_num1[i] %= 10;
_num1[i + 1]++;
}
}
if(_num1[len] > 0){
len++;
}
for(i = len - 1, j = 0; i >= 0; i--, j++){
num1[j] = (char)('0' + _num1[i]);
}
num1[len] = '\0';
}
int main()
{
scanf("%s%s", num1, num2);
_add(num1, num2);
printf("%s\n", num1);
return 0;
}
大数减法
/***洛谷P2142 高精度减法***/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 10005
char num1[maxn], num2[maxn];
int _num1[maxn], _num2[maxn];
int _max(int a, int b){
return a > b ? a : b;
}
int cmp(char a[], char b[]){
int l_a = strlen(a);
int l_b = strlen(b);
if(l_a > l_b) return 1;
else if(l_a < l_b) return 0;
else{
int i;
for(i = 0; i < l_a; i++){
if(a[i] > b[i]) return 1;
else if(a[i] < b[i]) return 0;
}
return 2;
}
}
void _sub(char a[], char b[]){
int l_a = strlen(a);
int l_b = strlen(b);
int len = _max(l_a, l_b);
int i, j;
memset(_num1, 0, sizeof(_num1));
memset(_num2, 0, sizeof(_num2));
for(i = l_a - 1, j = 0; i >= 0; i--, j++){
_num1[j] = (int)(a[i] - '0');
}
for(i = l_b - 1, j = 0; i >= 0; i--, j++){
_num2[j] = (int)(b[i] - '0');
}
for(i = 0; i < len; i++){
if(_num1[i] >= _num2[i]){
_num1[i] -= _num2[i];
}
else{
_num1[i] = (_num1[i] + 10 - _num2[i]);
_num1[i + 1]--;
}
}
for(i = len - 1; i >= 0; i--){
if(_num1[i] != 0) break;
}
len = i + 1;
for(i = len - 1, j = 0; i >= 0; i--, j++){
num1[j] = (char)('0' + _num1[i]);
}
num1[len] = '\0';
}
int main()
{
scanf("%s%s", num1, num2);
int tag = cmp(num1, num2);
if(tag == 2) puts("0");
else if(tag == 1){
_sub(num1, num2);
printf("%s\n", num1);
}
else{
_sub(num2, num1);
printf("-%s\n", num1);
}
return 0;
}
大数乘法
/***洛谷P1303 A*B Problem***/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 4005
char num1[maxn], num2[maxn];
int _num1[maxn], _num2[maxn], res[maxn];
void _mul(char a[], char b[]){
int l_a = strlen(a);
int l_b = strlen(b);
int i, j;
for(i = l_a - 1, j = 0; i >= 0; i--, j++){
_num1[j] = (int)(num1[i] - '0');
}
for(i = l_b - 1, j = 0; i >= 0; i--, j++){
_num2[j] = (int)(num2[i] - '0');
}
for(i = 0; i < l_a; i++){
for(j = 0; j < l_b; j++){
res[i + j] += _num1[i] * _num2[j];
}
}
for(i = 0; i < l_a + l_b; i++){
if(res[i] >= 10){
res[i + 1] += (res[i] / 10);
res[i] %= 10;
}
}
int len;
for(i = l_a + l_b; i >= 0; i--){
if(res[i] != 0){
len = i + 1;
break;
}
}
if(i == -1){
num1[0] = '0'; num1[1] = '\0'; return;
}
for(i = len - 1, j = 0; i >= 0; i--, j++){
num1[j] = (char)('0' + res[i]);
}
num1[len] = '\0';
}
int main()
{
scanf("%s%s", num1, num2);
_mul(num1, num2);
printf("%s\n", num1);
return 0;
}