HDOJ1230 Mars a+b__ string

Source: Internet
Author: User
Mars a+b time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u Problem Description read two 25-digit Mars full Number A and B, calculate a+b. It should be noted that on Mars, the integer is not a single system, the nth bit of the system is the nth prime number. For example: 10 of the Earth's number of 2, on Mars recorded as "1,0", because the Mars single digit is 2; 10 of the Earth's number 38, on Mars, recorded as "1,1,1,0", because the Mars Single-digit is 2, 10 digits is 3, the Hundred is 5, the thousand is 7 into the system ....

Input test entry contains a number of test cases, one row for each test case, contains two Mars positive integers a and B, and the adjacent two digits of the Mars integer are separated by commas, with a space interval between a and B. When a or B enters the end of 0 o'clock, the corresponding result is not output.

Output 1 rows For each test case, that is, the a+b value of the Mars notation.

Sample Input
1,0 2,1 4,2,0 1,2,0 1 10,6,4,2,1 0 0 Sample Output
1,0,1 1,1,1,0 1,0,0,0,0,0

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype
> #include <cmath> using namespace std;
    #define M int main () {int p[30],pn,n,i,b[2][30],bn[2],k;
        For (pn=1,p[0]=2,n=3, pn<27; n+=2) {for (i=0; i<pn&&n%p[i]; ++i);
    if (I==PN) p[pn++]=n;
    } string a[2]; while (cin>>a[0]>>a[1]&& (a[0]!= "0") | |
        a[1]!= "0")) {memset (b,0,sizeof (b)); For (i=0 i<2; ++i) {for (Bn[i]=0,k=a[i].size ()-1; k>=0; ++bn[i],--k) {for (b[i][bn[i]]=0,n = 1; K>=0&&a[i][k]!= ', ';
            --k,n*=10) b[i][bn[i]]+=n* (a[i][k]-' 0 '); for (i=0; i<bn[0]| | i<bn[1];
        ++i) {if ((B[0][i]+=b[1][i]) >=p[i]) b[0][i+1]+=b[0][i]/p[i],b[0][i]%=p[i];
        BOOL Fi=true;
        if (b[0][i]>0) cout<<b[0][i],fi=false; for (i; i>=0;-i) {if (FI) Fi=false;
            Else cout<< ",";
        cout<<b[0][i];
    } cout<<endl;
return 0; }


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.