Skip to content

Instantly share code, notes, and snippets.

@1119-2916
Last active December 30, 2016 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1119-2916/18b39df0cd8f4f49670adb49b8cac9a3 to your computer and use it in GitHub Desktop.
Save 1119-2916/18b39df0cd8f4f49670adb49b8cac9a3 to your computer and use it in GitHub Desktop.
AOJ1180
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 2147483647
#define rep(i, n) for (int i = 0; i < n; i++)
int main()
{
int a, b;
char buf[32] = "";
while (cin >> a >> b, b) {
map<int, int> mp;
mp[a] = 0;
int j;
for (j = 1; ; j++) {
sprintf(buf, "%0*d", b, a);
sort(buf, buf+b);
int c = atoi(buf);
reverse(buf, buf+b);
int d = atoi(buf);
a = d - c;
if (!mp.insert(pair<int,int>(a, j)).second) {
break;
}
}
int r = mp[a];
printf("%d %d %d\n", r, a, j - r);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment