一天的努力就是两道题··································分别是加法和减法·····&am

#include<stdio.h>
#include<string.h>
int a[1000]={0},b[1000]={0},c[1000]={0};
int main()
{
char *s1;
int l1=0,l2=0,l,i,x;
freopen("hp-.in","r",stdin);
freopen("hp-.out","w",stdout);
scanf("%s",s1);
l1=strlen(s1);
for(i=0;i<=l1-1;i++)
{
a[l1-1-i]=s1[i]-'0';
}
scanf("%s",s1);
l2=strlen(s1);
for(i=0;i<=l2-1;i++)
{
b[l2-1-i]=s1[i]-'0';
}
if(l1>l2)
l=l1;
else
l=l2;
for(i=0;i<=l-1;i++)
{

if(a[i]<b[i])
{
c[i]=a[i]+10-b[i];
c[i+1]=a[i+1]-1;
}
else
c[i]=a[i]-b[i];
}
for(i=0;i<=l-1;i++)
{
if(a[i]<b[i])
{
x=1;
}
}
if(l1<l2||x==1)
{
printf("-");
for(i=0;i<=l-1;i++)
{

if(b[i]<a[i])
{
c[i]=b[i]+10-a[i];
c[i+1]=b[i+1]-1;
}
else
c[i]=b[i]-a[i];
}
}
for(i=l-1;i>=0;i--)
{
if(c[i]==0)
l--;
else
break;
}
for(i=l-1;i>=0;i--)
printf("%d",c[i]);
return 0;
}

#include<stdio.h>
#include<string.h>
int a[1000]={0},b[1000]={0};
int main()
{
char *s1;
int l1=0,l2=0,l,i;
scanf("%s",s1);
l1=strlen(s1);
for(i=0;i<=l1-1;i++)
{
a[l1-1-i]=s1[i]-'0';
}
scanf("%s",s1);
l2=strlen(s1);
for(i=0;i<=l2-1;i++)
{
b[l2-1-i]=s1[i]-'0';
}
if(l1>l2)
l=l1;
else
l=l2;
for(i=0;i<=l-i;i++)
{
a[i]=a[i]+b[i];
if(a[i]>=10)
{
a[i]=a[i]-10;
a[i+1]=a[i+1]+1;
}
}
if(a[l]!=0)
l++;
for(i=l-1;i>=0;i--)
printf("%d",a[i]);
return 0;
}

posted @ 2016-07-06 19:19  刘cy  阅读(143)  评论(0编辑  收藏  举报