标签:timus 1777. anindilyakwa 奇怪的数数问题
input | output |
---|---|
11 5 9 |
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