加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

POJ 3982 序列 大数题解

发布时间:2020-12-14 03:31:06 所属栏目:大数据 来源:网络整理
导读:又是一道大数相加的题目,直接模板或者Java都可以水过了。 循环相加33次就可以了,计算出A99是第几个,准确输出答案。 #include stdio.h#include string#include algorithmusing std::string;const int MAX_B = 5120;char buf[MAX_B];int id = 0,len = 0;inl

又是一道大数相加的题目,直接模板或者Java都可以水过了。

循环相加33次就可以了,计算出A99是第几个,准确输出答案。

#include <stdio.h>
#include <string>
#include <algorithm>
using std::string;

const int MAX_B = 5120;
char buf[MAX_B];
int id = 0,len = 0;

inline char getFromBuf()
{
	if (id >= len)
	{
		len = fread(buf,1,MAX_B,stdin);
		id = 0;
	}
	return buf[id++];
}

void getIntFromBuf(string &n)
{
	char a = getFromBuf();
	while ((a == ' ' || a == 'n') && len) a = getFromBuf();

	n.clear();
	while ((a != ' ' && a != 'n') && len)//老是写&&,错成||
	{
		n.push_back(a);
		a = getFromBuf();
	}
}

string operator+(string &a,string &b)
{
	string c;
	int N1 = (int)a.size(),N2 = (int)b.size();
	int carry = 0;
	for (int i = N1-1,j = N2-1; i>=0 || j>=0 || carry; i--,j--)
	{
		int an = i>=0? a[i]-'0' : 0;
		int bn = j>=0? b[j]-'0' : 0;
		int sum = an + bn + carry;
		carry = sum / 10;
		c.push_back(sum % 10 + '0');
	}
	reverse(c.begin(),c.end());
	return c;
}

int main()
{
	string a1,a2,a3;
	while (true)
	{
		getIntFromBuf(a1);
		if (len == 0) break;
		getIntFromBuf(a2);
		getIntFromBuf(a3);

		for (int i = 0; i < 33; i++)
		{
			string a = a2 + a3;
			a1 = a1 + a;
			if (i == 32) break;
			a2 = a1 + a;
			a3 = a3 + a1;
			a3 = a3 + a2;
		}
		puts(a1.c_str());
	}
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读