Mars A + B (string shaping conversion, hexadecimal), Mars hexadecimal
Description
Read two positive integers A and B of Mars at least 25 digits and calculate A + B. It should be noted that on Mars, integers are not in single hexadecimal notation, And the nth hexadecimal notation is the nth prime number. For example, the 10 hexadecimal number 2 on the earth is counted as "1, 0" on Mars, because the single digit of Mars is 2 hexadecimal; the 10 hexadecimal number on the earth is 38, on Mars, it is recorded as ",", because the number of single digits of Mars is in hexadecimal notation, the number of digits is in hexadecimal notation, and the number of digits is in hexadecimal notation, the number of thousands is in the 7-digit format ......
Input
The test input contains several test cases. Each test case occupies one row and contains two positive integers, A and B. The two adjacent double digits of the positive integers are separated by commas. There is A space interval between A and B. When expression A or expression B is 0, the input ends, and the corresponding results are not output.
Output
Output one line for each test case, that is, the value of A + B in the Mars notation.
Sample Input
, 0, 0 1, 6, 1 0
Sample Output
1, 0, 1, 1, 0, 0, 0, 0 I think this question has been tested many times. I forgot to open the equal sign and the character array is too small. This is a waste of time? 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]); }}