#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<vector> #include<cmath> #include<set> #include<map> #define db(a) \ cout << #a << " = " << a << endl #define db2(a, b) \ cout << #a << " = " << a << " " << #b << " = " << b << endl #define inf (1<<30) #define foreach(m, it) \ for (typeof(m.begin()) it = m.begin(); it != m.end(); it++) using namespace std; int n; bool espali(string linea) { int n = linea.size(); for (int i = 0; i < n / 2; i++) { if (linea[i] != linea[n - i - 1]) return false; } return true; } int main() { #ifdef dennisbot freopen("in.in", "r", stdin); freopen("ou.out", "w", stdout); #endif string s; char buf[10]; int mayor, menor, prev, len; set<int> nums; while (cin >> s) { if (s == "0") break; prev = -1; len = 0; nums.clear(); printf("Original number was %s\n", s.c_str()); while (true) { sort(s.rbegin(), s.rend()); mayor = atoi(s.c_str()); reverse(s.begin(), s.end()); menor = atoi(s.c_str()); printf("%d - %d = %d\n", mayor, menor, mayor - menor); len++; prev = mayor - menor; if (!nums.count(prev)) nums.insert(prev); else break; sprintf(buf, "%d", prev); s = buf; } printf("Chain length %d\n\n", len); } return 0; }Here is the Statement
viernes, 13 de julio de 2012
263 - Number Chains, uva
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario