Timus 1777. Anindilyakwa 奇怪的数数问题
来源:互联网 发布:js 遍历tr td 编辑:程序博客网 时间:2024/04/28 05:42
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 x1, x2 and x3(1 ≤ x1, x2, x3 ≤ 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
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 endvoid 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;}
1 0
- Timus 1777. Anindilyakwa 奇怪的数数问题
- 一个奇怪的数数题
- 数数问题
- 数数问题
- Timus 1791 汽车过桥的问题
- 13个小朋友数数的问题
- n人围成一圈数数出列的问题
- 奇怪的malloc问题!
- 奇怪的编译问题
- 奇怪的问题。
- 又一奇怪的问题?
- 一个奇怪的问题
- 奇怪的JNI问题
- 奇怪的问题
- 奇怪的问题
- IMPLEMENT_DYNCREATE的奇怪问题
- DotMSN的奇怪问题
- 一些奇怪的问题
- 区分shapefile,coverage,geo…
- crossdomain.xml 配置说明
- GIS底层实现流程
- 解决mysql的1045错误,修改root密码
- C++primer plus第六版课后编程题答案17.2
- Timus 1777. Anindilyakwa 奇怪的数数问题
- VS2012快捷键
- spring加载properties文件
- 使用javascript开发2048
- 【博客分享】优秀的有趣的博客
- 记北邮创新展
- mybatis generator生成配置文件
- NYOJ 590 相同的和
- 中缀式转前缀式、中缀式转后缀式