Hangzhou Electric ACM 1230 Mars a+b (binary)

Source: Internet
Author: User

Mars a+b

                               time limit:2000/1000 MS (java/others)      memory limit:65536/32768 K (java/others)
                                            Total Submission (s): 13903     accepted Submission (s): 4810

Problem Description

Read two non-25-bit Mars positive integers a and B, calculate a+b. It is important to note that on Mars, integers are not single-binary, and nth-bit is the nth prime. For example: 10 binary number 2 on Earth, on Mars as "1,0", because the number of Mars single-digit is 2 binary, the Earth's 10 number of 38, on Mars as "1,1,1,0", because the Mars single-digit is 2 binary, 10-digit is 3, the Hundred is 5 binary, thousand is 7 binary ... The input test inputs contain several test cases, one row for each test case, two Martian positive integers a and B, a comma-separated number of adjacent two-digit numbers for the Mars Integer, and a space interval between a and B.  When a or B is 0 o'clock input end, the corresponding result does not output. Output outputs 1 rows for each test case, which is the value of the a+b of the Mars notation. Sample input1,0 2,14,2,0 1,2,01 10,6,4,2,10 0 Sample output1,0,11,1,1,01,0,0,0,0,0 The most important thing is to deal with the numbers, that is, how to accept every digit, The binary conversion is actually very simple. Although there is a lot of numbers, but not strings, because there will be two digits or even multiple digits on the high, so the good way is to use an array, one is a number, from low to high, analog conversion.
#include <iostream>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;Const intmaxn= Max;//because the maximum number of two reads is 25 bits.intP[MAXN];voidPrime () {//find the first 150 primesp[0]=2; p[1]=3; intk=2;  for(intI=4; k<maxn;i++){        intflag=0;  for(intj=2; j<=sqrt (i); j + +) {//If a number cannot be divisible by 2 of all books to his square root, he is prime.            if(i%j==0) {flag=1;  Break; }        }        if(!flag) p[k++]=i; }}intMain () {intn,m,i,j,k,t;    Prime (); intA[MAXN],B[MAXN]; intLena,lenb; intx, y; Charop;  while(1) {memset (A,0,sizeof(a));//Initialize A and Bmemset (b,0,sizeof(b)); scanf ("%d", &x);//enter the first digit of aI=0; A[i++]=x; scanf ("%c", &op);//characters after receiving a number         while(op==','){//If the character is a space, it indicates that the number after is B.scanf"%d",&x); A[i++]=x; scanf ("%c",&op); } Lena=i; scanf ("%d",&x); I=0; B[i++]=x; scanf ("%c",&op);  while(op==',') {scanf ("%d",&x); B[i++]=x; scanf ("%c",&op); } LenB=i; Std::reverse (A,a+lena);//The number of flashbacks is received, so the order in turn is calculatedStd::reverse (b,b+LenB); if(a[0]==0&&b[0]==0&&lena==1&&lenb==1) Break;//if both A and B are 0, end the program        intmaxnlen=Max (LENA,LENB); intSUM[MAXN]; memset (SUM,0,sizeof(sum));  for(i=0, k=0; i<maxnlen;i++) {//Add A and Bsum[k++]=a[i]+B[i]; }        intlensum=K;  for(i=0; i<lensum+Ten; i++) {//converting A and B calculated numbers into a binary conversion            intCur=sum[i]%p[i];//Current Value            intMod=sum[i]/p[i];//Roundingsum[i]=cur; Sum[i+1]+=MoD; }         for(i=maxn-1; i>=0;-I.)if(sum[i]!=0) Break;//from the very end, that is, the highest bit value starts to output, so to remove the excess 0;j=i; printf ("%d", Sum[j]);  for(i=j-1; i>=0;--i) {printf (",%d", Sum[i]); } printf ("\ n"); }}

Hangzhou Electric ACM 1230 Mars a+b (binary)

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.