hdu1230 大数加法+进制转换

本文探讨了火星进制加法的算法实现,其中火星进制的位数由素数组成,同时介绍了刘老师的大数算法,用于处理大数值的加法运算。通过结合两种算法,解决了特殊进制下的加法问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 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

将两种算法结合起来求解

#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<cmath>
#include<algorithm>
#include<ctype.h>
using namespace std;
int prime[26];
char num1[1000];
char num2[1000];
void is_prime()
{
    int cnt=0;
    for(int i=2;; i++)
    {
        int ok=1;
        int m=int(sqrt(i)+0.5);
        for(int j=2; j<=m; j++)
            if(i%j==0)
            {
                ok=0;
                break;
            }
        if(ok)prime[cnt++]=i;
        if(cnt>=26)break;
    }
}
int main()
{
    is_prime();
    //freopen("datain.txt","r",stdin);
    while(~scanf("%s%s",num1,num2)){
            if(!strcmp(num1,"0")&&!strcmp(num2,"0"))break;
            int pos1=strlen(num1)-1;
            int pos2=strlen(num2)-1;
            stack<int>s;
            int r=0,digit=0;
            while(pos1>=0||pos2>=0){
                int weight=0,x=0;
                for(;pos1>=0;pos1--){
                    if(isdigit(num1[pos1]))x+=(num1[pos1]-'0')*pow(10,weight++);
                    else break;
                }
                weight=0;
                for(;pos2>=0;pos2--){
                    if(isdigit(num2[pos2]))x+=(num2[pos2]-'0')*pow(10,weight++);
                    else break;
                }
                s.push((x+r)%prime[digit]);
                r=(x+r)/prime[digit];
                pos1--;pos2--;digit++;
            }
            if(r) s.push(r);
            printf("%d",s.top());s.pop();
            while(!s.empty()) printf(",%d",s.top()),s.pop();
            printf("\n");
    }
    return 0;
}

顺便帖一下刘老师的大数算法

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include <algorithm>
#include<string>

using namespace std;

struct BigInteger {
	static const int BASE = 100000000;
	static const int WIDTH = 8;
	vector<int> s;

	BigInteger(long long num = 0) { *this = num; } // 构造函数  给构造函数的参数提供默认值
	BigInteger operator = (long long num) { // 赋值运算符
		s.clear();
		do {
			s.push_back(num % BASE);
			num /= BASE;
		} while (num > 0);
		return *this;
	}
	BigInteger operator = (const string& str) { // 赋值运算符
		s.clear();
		int x, len = (str.length() - 1) / WIDTH + 1;
		for (int i = 0; i < len; i++) {
			int end = str.length() - i * WIDTH;
			int start = max(0, end - WIDTH);
			sscanf(str.substr(start, end - start).c_str(), "%d", &x);
			s.push_back(x);
		}
		return *this;
	}
	BigInteger operator + (const BigInteger& b) const {
		BigInteger c;
		c.s.clear();
		for (int i = 0, g = 0; ; i++) {
			if (g == 0 && i >= s.size() && i >= b.s.size()) break;
			int x = g;
			if (i < s.size()) x += s[i];
			if (i < b.s.size()) x += b.s[i];
			c.s.push_back(x % BASE);
			g = x / BASE;
		}
		return c;
	}
};

ostream& operator << (ostream &out, const BigInteger& x) {
	out << x.s.back();
	for (int i = x.s.size() - 2; i >= 0; i--) {
		char buf[20];
		sprintf(buf, "%08d", x.s[i]);
		for (int j = 0; j < strlen(buf); j++) out << buf[j];
	}
	return out;
}

istream& operator >> (istream &in, BigInteger& x) {
	string s;
	if (!(in >> s)) return in;
	x = s;
	return in;
}

#include<set>
#include<map>
set<BigInteger> s;
map<BigInteger, int> m;

int main() {
	BigInteger y;
	BigInteger x = y;
	BigInteger z = 123;

	BigInteger a, b;
	cin >> a >> b;
	cout << a + b << "\n";
	//cout << BigInteger::BASE << "\n";
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值