火星A+B

來源:互聯網
上載者:User

標籤:

Description

讀入兩個不超過25位的火星正整數A和B,計算A+B。需要注意的是:在火星上,整數不是單一進位的,第n位的進位就是第n個素數。例如:地球上的10進位數2,在火星上記為“1,0”,因為火星個位元是2進位的;地球上的10進位數38,在火星上記為“1,1,1,0”,因為火星個位元是2進位的,十位元是3進位的,百位元是5進位的,千位元是7進位的……  

Input

測試輸入包含若干測試案例,每個測試案例佔一行,包含兩個火星正整數A和B,火星整數的相鄰兩位元用逗號分隔,A和B之間有一個空格間隔。當A或B為0時輸入結束,相應的結果不要輸出。  

Output

對每個測試案例輸出1行,即火星標記法的A+B的值。  

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 個人感想這道題測試了很多次,忘掉等號開以及字元數組太小等問題浪費了時間 sourcehttp://acm.hust.edu.cn/vjudge/contest/view.action?cid=89340#problem/D 
#include <stdio.h>#include <math.h>#include <string.h>int prime(int x){    int flag = 1;    for(int i = 2; i <= sqrt(x); i++)    {        if(x % i == 0)        {            flag = 0;            break;        }    }    return flag;}int main(){    int p[30], top = 0, len1, len2, num1[30], num2[30], ans[30];    char str1[1000], str2[1000];    for(int i = 2; top < 25; i++)    {        if(prime(i))        {            p[top++] = i;            //printf("%d\n\n", p[top-1]);        }    }    while(~scanf("%s%s", str1, str2))    {        if(strcmp(str1, "0") == 0 || strcmp(str2, "0") == 0)            break;        memset(num1, 0, sizeof(num1));        memset(num2, 0, sizeof(num2));        memset(ans, 0, sizeof(ans));        top = 0;        int k = 1, cnt;        len1 = strlen(str1);        len2 = strlen(str2);        /*for(int i = 0; i < len1; i++)        {            printf("%c:%c\n", str1[i], str2[i]);        }        printf("\n\n"); */        //printf("len1 = %d    len2 = %d\n", len1, len2);        for(int i = len1 - 1; i >= 0; i--)        {            if(str1[i] == ‘,‘)            {                top++;                k = 1;                continue;            }            num1[top] += (str1[i] - ‘0‘) * k;            //printf("i = %d : num1[%d] = %d\n\n", i, top, num1[top]);            k = k*10;        }        cnt = top+1;        //printf("cnt = %d\n", cnt);        top = 0;        k = 1;        for(int i = len2 - 1; i >= 0; i--)        {            if(str2[i] == ‘,‘)            {                top++;                k = 1;                continue;            }            num2[top] += (str2[i] - ‘0‘) * k;            //printf("i = %d : num2[top] = %d\n\n", i, top, num2[top]);            k = k*10;        }        if(top+1 > cnt)            cnt = top+1;        //printf("cnt = %d\n", cnt);        for(int i = 0; i < cnt; i++)        {            ans[i] += num1[i] + num2[i];            while(ans[i] >= p[i] && p[i] != 0)            {                ans[i+1] += ans[i] / p[i];                ans[i] %= p[i];            }            //printf("ans[%d] = %d, num1[%d] = %d, num2[%d] = %d\n", i, ans[i], i, num1[i], i, num2[i]);            k = i+1;        }        if(ans[k])                printf("%d,", ans[k]);        for(int i = cnt-1; i > 0; i--)            printf("%d,", ans[i]);        printf("%d\n", ans[0]);    }}

 

火星A+B

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.