High-precision addition and subtraction. Addition is OK, subtraction took a lot of time.

Source: Internet
Author: User

Addition:

#include <stdio.h>
#include <string.h>
int a[1000]={0},b[1000]={0};
int main ()
{
Char *s1;
int l1=0,l2=0,l,i;
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&GT;L2)
L=L1;
Else
L=L2;
for (i=0;i<=l-1;i++)
{
A[i]=a[i]+b[i];
if (a[i]>=10)
{
a[i+1]=a[i+1]+1;
a[i]=a[i]-10;
}
}
if (a[l]!=0)
l++;
for (i=l-1;i>=0;i--)
printf ("%d", a[i]);
return 0;
}

Subtraction:

#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=l-1;i>=0;i++)     {     if (A[i] <b[i])      {        x=1;    }   else & nbsp;   break;  }     if (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; }

High-precision addition and subtraction. Addition is OK, subtraction took a lot of time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.