大数加法
代码实现:
#include <stdio.h>
#include <string.h>
typedef char Str[1000];
void PrintNum(int sum[] , int len)///---------输出
{
int i;
for(i = len-1 ; i >= 0 ; i--)
printf("%d",sum[i]);
printf("\n");
}
void Add(Str a , int len_a , Str b , int len_b)///-----计算a+b(只能:+)
{
int k = 0 , c = 0;
int max_len = len_a > len_b ? len_a : len_b;
int sum[max_len+2];
for( ; len_b >= 0 ; ){///---a+b
sum[k++] = (a[len_a--]+b[len_b--]-96+c)%10;
c = (a[len_a+1]+b[len_b+1]-96+c) > 9 ? 1 : 0;
}
while(len_a > -1){///-----复制a
sum[k++] = (a[len_a]-48+c)%10;
c = (a[len_a]-48+c) > 9 ? 1 : 0;
len_a--;
}
if(c > 0)
sum[k++] = 1;
PrintNum(sum , k);
}
int main()///-------main()
{
Str a , b;
while(~scanf("%s %s", a , b)){
int len_a = strlen(a);
int len_b = strlen(b);
if(len_a >= len_b)
Add(a , len_a-1 , b , len_b-1);
else
Add(b , len_b-1 , a , len_a-1);
}
return 0;
}
大数减法
代码实现
#include <stdio.h>
#include <string.h>
typedef char Str[1000];
void PrintNum(int sum[] , int len , char CH)///----输出
{
int i;
if(CH == '-')
printf("%c",CH);
for(i = len-1 ; i >= 0 ; i--){
if(i == len-1 && sum[i] == 0){
while(sum[i] == 0) i--;
}
printf("%d",sum[i]);
}
printf("\n");
}
void Sub(Str a , Str b , char CH)///-----计算a-b(a,b只能为:+)
{
int len_a = strlen(a)-1;
int len_b = strlen(b)-1;
int sum[len_a+2];
int k = 0 , c = 0 , n = 0 , m = 0;
for( ; len_b >= 0 ; ){///-------计算a-b
m = a[len_a]-48 ; n = b[len_b]-48;
sum[k++] = (m-n-c) < 0 ? (10+m-n-c) : (m-n-c);
c = m >= (n+c) ? 0 : 1;
len_a-- ; len_b--;
}
while(len_a > -1){///------复制a
m = a[len_a]-48;
sum[k++] = (m-c) < 0 ? (10+m-c) : (m-c);
c = (m-c) >= 0 ? 0 : 1;
len_a--;
}
PrintNum(sum , k , CH);
}
int main()///-----main()
{
Str a , b;
while(~scanf("%s %s",a , b)){///---多组输入
if(strcmp(a , b) > 0)
Sub(a , b , '+');
else if((strcmp(a , b)) == 0)
printf("0\n");
else
Sub(b , a , '-');
}
return 0;
}
大数乘法
代码实现
#include <stdio.h>
#include <string.h>
typedef char Str[1000];
void PrintNum(int sum[] , int len)///-------输出
{
int i;
for(i = len-1 ; i >= 0 ; i--){
if(i == len-1 && sum[i] == 0){
while(sum[i] == 0) i--;
}
printf("%d",sum[i]);
}
printf("\n");
}
void Mul(Str a , int len_a , Str b , int len_b)///--------计算乘积a*b(只能:+)
{
int i , j , k , c;
int len = len_a*len_b;
int sum[len];
memset(sum , 0 , sizeof(sum));
for(i = len_b-1 ; i >= 0 ; i--){
c = 0 ; j = len_b-i-1;
for(k = len_a-1 ; k >= 0 ; k--){
int s = (a[k]-48)*(b[i]-48)+c;
c = (s+sum[j])/10;
sum[j++] = (sum[j]+s)%10;
}
if(c > 0){
sum[j] = (sum[j]+c)%10;
}
}
PrintNum(sum , len);
}
int main()///-----main()
{
Str a , b;
while(~scanf("%s %s",a,b)){
if(a[0] == '0' || b[0] == '0'){
printf("0\n");
continue;
}
if(strlen(a) >= strlen(b))
Mul(a , strlen(a) , b , strlen(b));
else{
Mul(b , strlen(b) , a , strlen(a));
}
}
return 0;
}
大数除法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char Str[1000];
void PrintNum(char sum[]){///---print
int i;
for(i = 0 ; i < strlen(sum) ; i++){
if(i == 0 && sum[i] == '0')
while(sum[i] == '0') i++;
printf("%c",sum[i]);
}
printf("\n");
}
void Div(Str a , Str b)///-------div a/b
{
Str sum;
int len_a = strlen(a);
int len_b = strlen(b);
char str[len_b+2];
int i , j = 0 , k = 0 , tem_i , num , temp = 0;
for(i = 0 ; i < len_a ; i++){
str[j++] = a[i] ; num = 0;
if((temp == 0 || temp == 1 && j == len_b) || (j > len_b)){
str[j] = '\0' ; temp = 1;
if(strcmp(str , b) < 0 && strlen(str) == len_b){
sum[k++] = num+'0';
continue;
}
while((strcmp(str , b) >= 0 && strlen(str) == len_b) || strlen(str) > len_b){
int c = 0;
for( tem_i = len_b-1 , j -= 1; tem_i >= 0 ; tem_i-- , j--){///-------str-b
int m = str[j]-48 ; int n = b[tem_i]-48;
str[j] = (m-n-c) < 0 ? (10+m-n-c)+'0' : (m-n-c)+'0';
c = m >= (n+c) ? 0 : 1;
}
str[j] = str[j]-48-c+'0';
num++ ; j = 0;
for(tem_i = 0 ; tem_i < strlen(str) ; tem_i++){
if(tem_i == 0 && str[tem_i] == '0')
while(str[tem_i] == '0') tem_i++;
if(tem_i < strlen(str))
str[j++] = str[tem_i];
}
str[j] = '\0';
}
sum[k++] = num+'0';
}
else if(j < len_b && temp == 1){
sum[k++] = '0';
if(str[0] == '0') j = 0;
}
}
sum[k] = '\0';
PrintNum(sum);
}
int main()///--main()
{
int m;
Str a , b;
scanf("%d",&m);
while(m--){
scanf("%s %s",a,b);
if(a[0] == '0' || b[0] == '0' || strlen(a) < strlen(b)){
printf("0\n");
continue;
}
if(strcmp(a , b) < 0 && strlen(a) == strlen(b)){
printf("0\n");
continue;
}
if(strcmp(a , b) == 0){
printf("1\n");
continue;
}
Div(a,b);
}
return 0;
}