Huawei trial-subtraction of large numbers, Huawei large numbers

Source: Internet
Author: User

Huawei trial-subtraction of large numbers, Huawei large numbers

Question: subtraction of large numbers

Enter a positive integer of two strings. The first line is the subtrahend, the second line is the subtrahend, and the result of the first line minus the second line is output.

Note: 1. Both integers are positive integers. The subtrahend are greater than the subtrahend. Example:


Input: 1000000000000001 1

Output: 1000000000000000


# Include <stdio. h> # include <string. h> # define MAX 100 // subtract int bigNumSub (char a [], char B [], char sub []) {int I = 0; int c = 0; // determine whether the value is enough to reduce, 0 is enough to reduce, and 1 is not enough to reduce // clear 0 char m [MAX] = {0 }; // The subtrahend are greater than the subtrahend of char n [MAX] = {0}; // The subtrahend of memset (sub, 0,100 ); // reverse the string and change the string to the numeric int lenA = strlen (a); int lenB = strlen (B); for (I = 0; I <lenA; I ++) {m [I] = a [lenA-i-1]-'0'; // printf ("% d", m [I]);} for (I = 0; I <lenB; I ++) {n [I] = B [lenB-i-1]-'0'; // printf ("% d", N [I]);} // bitwise operation: the bitwise operation is not enough. The bitwise operation is not enough, and the last bitwise operation is reduced by one for (I = 0; I <lenA; I ++) {c = (m [I]-n [I]) <0? 1:0; sub [I] = m [I] + 10 * c-n [I] + '0 '; // Add '0' to convert if (I + 1 = lenA) break; m [I + 1]-= c;} return lenA ;} void print (char sub [], int lenA) {int I; int k; // the first digit not 0 for (I = lenA-1; I> = 0; I --) {if (sub [I]! = '0') {k = I; break ;}} for (I = k; I> = 0; I --) {printf ("% c ", sub [I]) ;}} int main () {char a [MAX]; char B [MAX]; char sub [MAX]; puts ("input :"); gets (a); puts ("input B:"); gets (B); print (sub, bigNumSub (a, B, sub )); printf ("\ n"); return 0 ;}

Test results may be incomplete. Check for missing items:



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.