Acceleration and deceleration calculation (arithmetic)

Source: Internet
Author: User

Title DescriptionDescription addition and subtraction is the basic operation in the calculation, although the rules are simple, but the number of bits is too many, it will inevitably make mistakes. The question now is: given any number of digits (no more than 1000 bits) of the addition and subtraction formula, give the correct result. To increase the speed, the result of the given operation is guaranteed to be a positive integer. input/output format input/output Input Format:
Line: An expression of addition or subtraction
output Format:
A positive integer that represents the result of the calculation (there can be no leading 0) input and Output sample sample Input/output sample Test point # # Input Sample:12345678+111 Sample output:

123456789

idea: The data volume of this problem is very large, there are 1000 bits, if the direct calculation with int or longlong type is not advisable, must use high precision algorithm. here can be divided into steps to calculate: ① reading into an array with a string ② scans the array once, storing two paragraphs before and after the operator into two arrays ③ using high-precision addition and subtraction to calculate the sum or difference of two arrays into a array or b array (you can do whatever you want) ④for Loop output (reverse) add: The high-precision algorithm refers to the number of participating operations (Addend, meiosis, factors ...). ) range significantly beyond the range of the standard data type (integer, real) can be represented by the operationThe code is as follows:
1#include <stdio.h>2#include <string.h>3 inta[1001]={0},b[1001]={0};4 inti,ka,kb,k,c,temp;5 Chars[2002],ch;6 voidPlus ()//High-precision additive function7 {8      for(i=0; i<k;i++)9     {TenA[i]=a[i]+b[i]+c;//bitwise plus One         if(a[i]>=Ten)//Handling Carry (full 10 in one) A         { -a[i]=a[i]%Ten; -C=1;  the         } -         ElseC=0; -     } -     if(c>0) +     { -a[k]=C; +k++; A     } at } - voidMinus ()//High precision subtraction function - { -      for(i=0; i<ka;i++) -     { -         if(A[i]<b[i])//determine if this calculation is to be borrow in         { -C=1; toa[i+1]--; +         } -     Else theC=0; *a[i]=a[i]+c*Ten-b[i];//calculate the result of this bit $k=Ka;Panax Notoginseng      while(a[k-1]==0&&k>0) -k--;  the     } + }  A intMain () the { +scanf"%s", s); -k=0; $      while((s[k]!='+') && (s[k]!='-'))//The operator is bounded, separating two operands, the first part of the period is counted in the array a $     { -a[k]=s[k]-'0'; -k++; the     } -CH=S[K];//Record OperatorsWuyiKa=k;//record operator Current position thekb=0; -k++;//Skip Continue Wu      while(K<strlen (s))//The operator is later counted in array b -     { Aboutb[kb]=s[k]-'0';  $k++; -kb++;//record the number of bits in the post-operator section -     }  -     if(ka>=kb) K=ka;//maximum length of a and B for bitwise calculation A     Elsek=kb; +      for(i=0; i<ka/2; i++)//read-in is from high to low order, should be reversed before calculation the     { -temp=A[i]; $a[i]=a[ka-i-1]; thea[ka-1-i]=temp; the     }  the      for(i=0; i<kb/2; i++)//read-in is from high to low order, should be reversed before calculation the     { -temp=B[i]; inb[i]=b[ka-i-1]; theb[ka-1-i]=temp; the     } AboutC=0;//Rounding Initialization the     if(ch=='+')//Incoming addition function the Plus (); the     Else //Incoming subtraction function + minus (); -      for(i=k-1; i>=0; i--)//output results in reverse order the     {Bayiprintf"%d", A[i]); the     }  theprintf"\ n");  -     return 0; -}

Acceleration and deceleration calculation (arithmetic)

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.