nyist-2384-无敌签到题
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int x,y,z,i,j,m,up=0,k,q,len1,len2;
char a[100],b[100],c[100],d[100];
while(~scanf("%s%s",a,b))
{
k=0;
len1=strlen(a)-1,len2=strlen(b)-1;
m=strcmp(a,b);
if(m==0)
{
cout<<0<<endl;
continue;
}
if(len1>len2||(len1==len2&&m>0))//如果后者小于前者,则交换位置
{
strcpy(d,b);
strcpy(b,a);
strcpy(a,d);
}
i=strlen(a)-1,j=strlen(b)-1;
while(i>=0||j>=0)
{
if(i<0) x=0;
else x=a[i]-'0';
if(j<0) y=0;
else y=b[j]-'0';
if(y<x)
{
q=j-1;
while(b[q]=='0') //向前面高位寻找非零数字借1
{q--;}
for(int s=q+1;s<j;s++)
b[s]='9';
b[q]=b[q]-1;
z=y+10-x;
}
else
z=y-x;
c[k++]=z+'0';
i--,j--;
}
c[k]='\0';
for(i=strlen(c)-1;i>=0;i--)
if(c[i]!='0')break;
if(len1>len2||(len1==len2&&m>0))
cout<<'-';
for(j=i;j>=0;j--)
cout<<c[j];
cout<<endl;
}
}
本文介绍了一种使用C++实现的字符串减法算法,通过比较两个字符串的大小并进行相应的位运算来得到两个字符串相减的结果。文章通过具体的代码实现了这一过程,并详细解释了如何处理字符串长度不一致的情况。
831

被折叠的 条评论
为什么被折叠?



