Mars A+B HDU-1230

Link: Mars A+B

Read in two Martian positive integers A and B with no more than 25 digits, and calculate A+B. It should be noted that on Mars, integers are not in a single base, and the n-th base is the n-th prime number. For example: the decimal number 2 on the earth is recorded as "1,0" on Mars, because the single digit of Mars is binary; the decimal number 38 on the earth is recorded as "1, 1,1,0", because the single digit of Mars is in binary, the tens digit is in hexadecimal, the hundreds digit is in hexadecimal, and the thousands digit is in hexadecimal...
Input
test input contains Several test cases, each test case occupies a line, contains two Martian positive integers A and B, the adjacent two digits of Martian integers are separated by commas, and there is a space between A and B. When A or B is 0, the input ends, and the corresponding result is not output.
Output
outputs 1 line for each test case, which is the value of A+B in Martian notation.
The Input the Sample
1,0 2,1
4,2,0 1,2,0
. 1 10,6,4,2,1
0 0
the Sample the Output
1,0,1
1,1,1,0
1,0,0, 0,0,0

Directly observing the input and output of the sample, you can find the law, and two Martian numbers can be added directly. The ones digit is full 2 ​​into 1, the tens digit is full 3 into 1, and the hundred digit full is 5 into 1……. Manual calculation is very simple, so it is not difficult to write the program

Because the Martian text is within 25 digits, a maximum of 25 prime numbers are required. We can define an array to store the first 25 prime numbers directly.

Then use two arrays to store two Martian texts respectively, because I store them in positive order, such as 10,6,4,2,1 and store them in the array is a[0] = 10,a[1] = 6,a [2] = 4, a[3] = 2, a[4] = 1 like this, so if you need to start from the ones digit and add upwards, you have to compare the digits of the two digits first, starting with the larger digit At the beginning, such as 1 and 10, 6, 4, 2, 1; first get b[4] and a[0] to calculate, just discuss the length of ab and it will come out

The basic operation is to add two numbers at corresponding positions. If it is greater than the prime number corresponding to the digit base, then subtract the prime number and add 1 to the previous digit.

The problem itself is not difficult. It can be done by looking at the sample. It takes a little effort when entering data, and the foundation is still not solid... Then remember to reset several arrays and corresponding variables to 0 at the beginning of each calculation. All right

#include<bits/stdc++.h>
using namespace std;//除了麻烦点其他没啥,简单的加法 
int f(int a)
{
    
    
	int i,l=sqrt(a);
	for(i=2;i<=l;i++)
	if(a%i==0)
	return 0;
	return 1;
}
int main()
{
    
    
	int i,j=0,k,n,x,y,m,s[100];
	char a[101],b[101];
	for(i=2;i<200;i++)//素数表 
	{
    
    
		if(f(i))
		s[j++]=i;//存素数 
	}
     while(scanf("%s%s",a,b)!=EOF)
     {
    
    
     	if(a[0]=='0'&&b[0]=='0')
     	break;//标识结束符 
     	x=strlen(a),y=strlen(b);//字符串长度 
     	int s1[101],s11[1010],s22[1010],s2[101],q[101];//一堆数组,为了方便计算嘛。。。 
     	for(i=0;i<101;i++)
     	s1[i]=s2[i]=q[i]=s11[i]=s22[i]=0;//初始化数组 
     	for(i=0,j=0;i<x;i++)
     	{
    
    
     		if(a[i]==',')//看见逗号表示这个位置的数都已经计算了 
     		j++;
     		else
     		{
    
    
     			s1[j]=s1[j]*10+(a[i]-'0');//反复计算存入数组,注意数组下标没有变,依次*10就是为了变大数 
			 }
		 }
     	x=j;//标记这个j
		 //下面字符串b同理 
     	for(i=0,j=0;i<y;i++)
     	{
    
    
     		if(b[i]==',')
     		j++;
     		else
     		s2[j]=s2[j]*10+(b[i]-'0');
		 }
     	y=j;
     	//把两个字符串翻转一下,也可以直接用函数翻转,自己找去,,我也记不住 
     	for(j=0,i=y;j<=y;j++,i--)
     	s22[i]=s2[j];
     	for(j=0,i=x;j<=x;j++,i--)
     	s11[i]=s1[j];//翻转 就是把个位放到起始位置,方便计算素数进位 
     	m=max(x,y)+1;//因为初始化了所有数组,所以直接循环到最大,max是c++最大值函数 
     	for(i=0;i<m;i++)
     	{
    
    
     		q[i]+=(s11[i]+s22[i]);
     		if(q[i]>=s[i])//进位 
     		q[i+1]+=((q[i]/s[i])),q[i]=q[i]%s[i];
		 }
		 if(q[m]!=0)//特判最后一位是否进位了 
		 m=m+1;
		 printf("%d",q[m-1]);
		 for(i=m-2;i>=0;i--)
		 printf(",%d",q[i]);
		 printf("\n");
	 }
}

Guess you like

Origin blog.csdn.net/m0_46381590/article/details/112606140