码迷,mamicode.com
首页 > 其他好文 > 详细

Timus 1777. Anindilyakwa 奇怪的数数问题

时间:2014-04-29 13:46:22      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:timus   1777. anindilyakwa   奇怪的数数问题   

The language of Australian aborigines anindilyakwa has no numerals. No anindilyakwa can say: “I‘ve hooked eight fishes”. Instead, he says: “I‘ve hooked as many fishes as many stones are in this pile”.
Professor Brian Butterworth found a meadow with three piles of stones. He decided to determine whether aborigines can count. Professor asked one of the aborigines to point at two piles with the minimal difference of numbers of stones in them and tell what this difference is. The aborigine pointed correctly! He was unable to express the difference with words, so he went to a shore and returned with a pile of the corresponding number of stones.
Professor decided to continue his experiments with other aborigines, until one of them points at two piles with equal number of stones. All piles that aborigines bring from the shore are left at the meadow. So, the second aborigine will have to deal with one more pile, the one brought by the first aborigine.

Input

The only input line contains space-separated pairwise distinct integers x1x2 and x3(1 ≤ x1x2x3 ≤ 1018), which are the numbers of stones in piles that were lying on the meadow at the moment professor Butterworth asked the first aborigine.

Output

Output the number of aborigines that will have to answer a stupid question by professor.

Sample

input output
11 5 9
3

Hint

The first aborigine will point at piles of 11 and 9 stones and will bring a pile of two stones. The second aborigine will point at the same piles and will bring another pile of two stones. The third aborigine will point at two piles of two stones, and the experiments will be over.

本题可以使用长整形来记录数据的,因为最长不过10^8,但是如果把这题当做是无穷大数来做的话,难度指数就直线上升了。
这里给出使用string来做无穷大减法的解法。
要处理的问题:
1 string大小比较问题,不能使用原始的<号
2 如何进位的问题
3 符号问题,因为这里只求差异就可以了,所以返回绝对值就够了。
这样做本题还是有一定难度, 而且可以锻炼到一些高级点的知识的运用,挺好。

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;

namespace
{

bool sLarger(const string s, const string t)
{
	if (s.size() < t.size()) return false;
	else if (s.size() > t.size()) return true;
	return s > t;
}

string operator-(string s, string t)
{
	if (s == t) return "0";
	string x;
	//bool sign = true;不用sign,求其绝对值即可
	if (sLarger(t, s)) 
	{
		s.swap(t);
	}

	bool carry = 0;
	for (int i = s.size()-1, j = t.size()-1; i>=0 || j>=0 ; i--, j--)
	{
		int a = i>=0? s[i] - ‘0‘ : 0;
		int b = j>=0? t[j] - ‘0‘ : 0;
		int sub = a - b - carry;
		carry = 0;
		if (sub < 0) 
		{
			sub += 10;
			carry = 1;
		}
		x.push_back(sub+‘0‘);
	}
	while (x.size() && ‘0‘ == x.back()) x.pop_back();
	reverse(x.begin(), x.end());
	return x;
}

}//empty namespace end

void Anindilyakwa1777()
{
	vector<string> piles(3);
	cin>>piles[0]>>piles[1]>>piles[2];
	sort(piles.begin(), piles.end(), sLarger);

	int c = 0;
	while (true)
	{
		c++;
		string minSub = piles[1]-piles[0];
		for (int i = 2; i < (int)piles.size(); i++)
		{
			string sub = piles[i]-piles[i-1];
			if (sLarger(minSub, sub)) minSub = sub;
		}
		if ("0" == minSub) break;
		piles.push_back(minSub);
		sort(piles.begin(), piles.end(), sLarger);
	}
	cout<<c;
}



Timus 1777. Anindilyakwa 奇怪的数数问题

标签:timus   1777. anindilyakwa   奇怪的数数问题   

原文地址:http://blog.csdn.net/kenden23/article/details/24688979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!