Timus 1777. Anindilyakwa 奇怪的问题计数
2015-06-11 14:48
204 查看
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.
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 the number of aborigines that will have to answer a stupid question by professor.
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 符号问题。由于这里仅仅求差异就能够了。所以返回绝对值就够了。
这样做本题还是有一定难度。 并且能够锻炼到一些高级点的知识的运用。挺好。
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
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 thirdaborigine 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; }
相关文章推荐
- Android网络编程只局域网传输文件
- Xamarin iOS教程之编辑界面编写代码
- 闭环最短路径算法
- android 设备所支持的分辨率
- android之Context对各种服务的管理
- 在Mac OS X下搭建Latex编辑环境
- 面试题一
- 东莞理工学院2015新生交流群是多少呢
- Top 10 content management systems
- PostgreSQL备份恢复
- AES 加密方式
- 讨论oracle在rowid和rownum
- spring quartz使用多线程并发“陷阱”(转)
- intent-filter
- 数组中出现次数超过一半的数字
- jsp的<%%>
- 使用EXCEL设置“下拉菜单”选项功能
- AI教程!手把手教你绘制素朴的山谷小溪矢量图
- Leetcode[20]-Valid Parentheses
- Nginx 负载均衡