题目
输入两个正整数(20位以上),计算两个数的差
例如:
输入:56262555558558536665 54212125121252222521
输出:2050430437306314144
代码
#include<stdio.h>
#include<string.h>
int main(){
char a[100],b[100],t[100];
char flg[]="+";
int ans[100]={0};
int num_a,num_b,i,n,lim,tmp;
char *p1,*p2;
scanf("%s %s",a,b);
for(num_a=0;a[num_a]!='\0';num_a++);
for(num_b=0;b[num_b]!='\0';num_b++);
if(strcmp(b,a) == 0){
printf("0");
return 0;
}
if(strlen(a)<strlen(b) || strlen(a)==strlen(b) && strcmp(b,a) > 0
){
strcpy(t,a);
strcpy(a,b);
strcpy(b,t);
flg[0] = '-';
}
n = --num_a;
lim = --num_b;
p1 = &a[num_a];
p2 = &b[num_b];
for(i=0;i<n-lim;i++)ans[i] = a[i] - '0';
tmp = n;
for(;n>0;n--){
ans[n] += (10 + *p1 - *p2) %10;
ans[n-1] += (*p1<*p2)?-1:0;
if(0 == lim--)break;
p1--;
p2--;
}
if(flg[0]=='-')
printf("%c",flg[0]);
for(i=0;ans[i]==0;i++)
for(i++;i<=tmp;i++)printf("%d",ans[i]);
return 0;
}
标签:p1,大数,int,C语言,char,num,ans,100,减法
From: https://www.cnblogs.com/Shin404/p/16902794.html