P1604 B into the planet

Source: Internet
Author: User

Topic links


Topic Background

The problem of the system, but also a calculator ~ ~

Title Description

One day, Little Z took a spaceship and flew to a beautiful planet. For historical reasons, science and technology are not very developed on this beautiful planet, and there is a widespread use of B (2<=b<=36) counting in the planets. The people on the planet entertained little Z with delicious food, and in return, Little Z wanted to send them a calculator that could complete the B-Add. Now Little Z wants you to help him, write a program that implements the addition of the B binary.

input/output format Input Format:

Total 3 Rows Line 1th: A decimal integer representing the binary B. 第2-3 line: A positive integer of B-binary numbers per line. Each digit of the number belongs to {0,1,2,3,4,5,6,7,8,9,a,b ...}, each digit length <=2000 bit.

output Format:

A B-binary number that represents the two number of inputs of the and.

input/Output sample Input Sample # #:
Sample # # of output:

Binary Calculator

Problem analysis

It's obvious that you see the subject.

This is titled High precision + binary conversion

Convert b into decimal and sum

And then convert the decimal into a B-binary.

But I don't have this water problem again.

PS: In the judgment of the preamble 0 o'clock should be judged with an array of int, because the array of char "0" corresponds to the ASCLL code is not 0! can also be compared with the array of char and ' 0 ' (that is, 0 ASCLL)

Reference Code

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cctype>5 6 using namespacestd;7 //#define DEBUG (x) cerr << #x << "=" << x << Endl8 Const intMAXN = 1e6 +Ten;9 Ten int Base, I, Len1, Len2, Len; One intCur =0, first =1, JW; A intA[MAXN], B[MAXN], SUM[MAXN]; - CharS1[MAXN], S2[MAXN], SUM_S[MAXN]; -  theInlineintRead () - { -     Charch, c; -     intRes; +      while(ch = getchar (), ch <'0'|| CH >'9') C =ch; -res = CH- -; +      while(ch = getchar (), ch >='0'&& CH <='9') Ares = res *Ten+ CH- -; at     if(c = ='-') Res =-Res; -     returnRes; - } -  - voidWriteintx) - { in     if(X <0) Putchar ('-'), x =-x; -     if(X >9) Write (X/Ten); toPutchar (x%Ten+'0'); +     return; - } the  * intMaxintXinty) $ {Panax Notoginseng     returnx > y?x:y; - } the  + intMain () A { theIos::sync_with_stdio (false); +Cin.tie (0); -CIN >>Base; $CIN >>S1; $CIN >>S2; -Len1 =strlen (S1); -Len2 =strlen (S2); the      for(inti = len1-1; I >=0; i--) -     {Wuyi         if(Isalpha (S1[i])) a[++cur] = S1[i]-'A'+Ten; the         ElseA[++cur] = s1[i]-'0'; -     } WuCur =0; -      for(inti = len2-1; I >=0; i--) About     { $         if(Isalpha (S2[i])) b[++cur] = S2[i]-'A'+Ten; -         ElseB[++cur] = s2[i]-'0'; -     } -Len = max (len1, len2) +1; A      for(inti =1; I <= Len; i++) +     { theSum[i] = (A[i] + b[i] + JW)%Base; -JW = (A[i] + b[i] + JW)/Base; $     } the      for(inti =1; I <= Len; i++) the         if(Sum[i] >9) Sum_s[i] = Sum[i]-Ten+'A'; the         ElseSum_s[i] = Sum[i] +'0'; the      for(inti = len; I >=1; i--) -         if(First &&!sum[i])Continue; in         Else the         { theFirst =0; Aboutcout <<Sum_s[i]; the         } thePuts""); the     return 0; +}

P1604 B into the planet

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.