Given 2 positive integers a, b,a and B can have up to 40 bits, and a + B is calculated and (C language implementation)

Source: Internet
Author: User

Recently participated in a programming competition, there is a few question bank, this topic is one of them.

Of course, if you want to exercise your programming level, you can go to Lintcode or other websites to brush the problem.

I was so confident that it took me one hours to write it down, and I was ashamed.

It was implemented using string, after all, it was programmed with C + +. Without thinking how to write, there is no use.

Title Description:

Given 2 positive integers a, b,a and B can have up to 40 bits, and a + B is calculated.

Input Description:

Two positive integers a, b,a and B can have up to 40 bits. A row represents a number.

Output Description:

A + B's and.

Sample input:
11111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222222
Sample output:
3333333333333333333333333333333333333333333333333333333333333333333

The principle of implementation is simple, that is, alignment. Aligns the bits and bits of the string.

Add the aligned parts and then handle the parts that are not aligned

#include <iostream> #include <stdlib.h> #include <string.h>using namespace Std;char * Addchar (char *   Add1,char *add2,char *sum,int * carry); int main () {char num[40] = {};  Char num2[40] = {};   Char sum[40] = {}; CIN >> Num;   Cin >> num2;   The number is aligned if (strlen (num) = = strlen (num2)) {int carry = 0; Addchar (num,num2,num,&carry); cout << num; return 0;} if (strlen (num) > strlen (num2)) {//num Large int sub = strlen (num)-strlen (num2); int carry = 0;char *p = num + Sub;addcha R (Num2,p,p,&carry); cout << num << endl; } else {int sub = strlen (num2)-strlen (num), int carry = 0;char *p = num2 + Sub;addchar (num,p,p,&carry); cout << N Um2 << Endl; }}//Add char * Addchar (char *add1,char *add2,char *sum,int * carry) {if (strlen (add1) = = strlen (ADD2)) {for (int i = strlen (a DD1)-1;i>=0;i--) {//48-57sum[i] = Add1[i] + add2[i]-+ *carry;if (Sum[i] >) {sum[i]-= 10;*carry = 1;} Else{*carry = 0;}} return sum;} Else{cout <&Lt "Erro, cannot be added" <<endl;return NULL; } }

  

Given 2 positive integers a, b,a and B can have up to 40 bits, and a + B is calculated and (C language implementation)

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.