现在的位置: 首页 > 综合 > 正文

POJ 1503 Integer Inquiry

2019年04月08日 ⁄ 综合 ⁄ 共 828字 ⁄ 字号 评论关闭

练习高精度。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

const int MAXN = 10010;

struct bign
{
	int len, s[MAXN];
	bign ()
	{
		memset(s, 0, sizeof(s));
		len = 1;
	}
	void clean()
	{
		while(len > 1 && !s[len-1]) len--;
	}
	bign (int num) { *this = num;}
	bign (const char *num) {*this = num;}
	bign operator = (const char *num)
	{
		len = strlen(num);
		for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
		return *this;
	}
	bign operator + (const bign &b) const
	{
		bign c;
		c.len = 0;
		for(int i = 0, g = 0; g || i < max(len, b.len); i++)
		{
			int x = g;
			if(i < len) x += s[i];
			if(i < b.len) x += b.s[i];
			c.s[c.len++] = x%10;
			g = x / 10;
		}
		c.clean();
		return c;
	}
	bign operator += (const bign &b)
	{
		*this = *this + b;
		return *this;
	}
	void print()
	{
		for(int i = len-1; i >= 0; i--) printf("%d", s[i]);
		printf("\n");
	}
};

int main()
{
	char str[MAXN];
	bign ans;
	while(~scanf("%s", str))
	{
		if(!strcmp(str, "0"))
		{
			ans.print();
			ans = "0";
		}
		else ans += str;
	}
	return 0;
}

抱歉!评论已关闭.