题目简述
给你 个 位数 ,如果 的位数不足 位,则填充前导 。每次设 为 按数位值由大到小排列所得到的数字,设 为 按数位值由小到大排列所得到的数字,让 。
在进行若干次循环后一定会得到一个之前已经得到的数,求 第一次出现的之前已经得到的数。
主要思路
由于 ,所以每次直接模拟相减的过程是可以的。
在每次进行相减前,先将目前的 存放到一个 set 中,set 中存储的都是已经出现过的数,所以可以用一个 do-while 循环,do 中存储与操作,while 判断刚操作完还未存放进 set 的 是否在 set 中出现过。
相减的过程中,由于输入的 的位数可能不等于 ,所以用整数操作就不太方便了,可以用 string 与数组进行一个类似于高精减法来操作。其中有几个点需要注意:
-
可能输入的 的位数不等于 ,所以需要加前导 。注意:这个操作必须在存放进
set前完成,因为不管在do的哪里都是不能去掉这个前导 的,如果后面操作时有前导 ,但刚开始的 没有,同时这两个数相等,set不能存放数组,所以只能存字符串,这样就会导致这两个数相等却不会被判重复。 -
在输出时,应该输出目前的 ,但这个 可能还存在前导 ,需要去掉。
AC Code
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
namespace IO {
#ifdef ONLINE_JUDGE
#define getchar getchar_unlocked
#endif
#define pc putchar
#define gc getchar
template<typename T> void read(T &x) { int f = 1; x = 0; char ch = gc(); while (!isdigit(ch)) { if (ch == '-')f = -1; ch = gc(); }while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = gc(); }x *= f; }
template<typename T, typename ...Args> void read(T &x, Args &...args) { read(x); read(args...); }
template<typename T> void print(T x) { if (x < 0) { pc('-'); x = -x; }if (x > 9) { print(x / 10); }pc(char(x % 10 + 48)); }
template<typename T, typename ...Args> void print(T &x, Args &...args) { print(x); pc(' '); print(args...); }
inline void readstr(string& x) { x.clear(); char ch = gc(); while (isspace(ch)) ch = gc(); while (!isspace(ch) && ch != EOF) { x.push_back(ch); ch = gc(); } }
inline void printstr(char* x) { for (int i = 0; i < (int)strlen(x); i++) pc(x[i]); }
inline void printstr(string& x) { for (auto i = x.begin(); i != x.end(); i++) pc(*i); }
};
using namespace IO;
#define OUT 0
#define MAMBA return
typedef long long ll;
const int D = 10 + 10;
typedef long double db;
const int INF = 0x3f3f3f3f;
int man();int main(){MAMBA man();}
inline int _abs(int a) { if (a < 0) return -a; return a; }
inline int _pow(int a, int b) { int x = 1, y = a; while(b > 0) {if (b & 1) x *= y; y *= y; b >>= 1; } return x; }
// ----------------------------
// ----------------------------
set<string> st;
int a[D], b[D];
// ----------------------------
int man() {
int n, d;
read(n, d);
// ----------------------------
string s;
while (n--) {
readstr(s);
st.clear();
do {
reverse(s.begin(), s.end());
while ((int)s.length() < d) s.push_back('0');
reverse(s.begin(), s.end());
st.insert(s);
for (int i = 0; i < d; i++) a[i + 1] = s[i] - '0';
sort(a + 1, a + d + 1);
copy(a + 1, a + d + 1, b + 1);
reverse(a + 1, a + d + 1);
for (int i = d; i >= 1; i--) {
if (a[i] < b[i]) {
a[i - 1]--;
a[i] += 10;
}
a[i] -= b[i];
}
s.clear();
for (int i = 1; i <= d; i++) s += char(a[i] + '0');
}
while (st.find(s) == st.end());
reverse(s.begin(), s.end());
while (s.length() > 1 && s.back() == '0') s.pop_back();
reverse(s.begin(), s.end());
printstr(s);
pc('\n');
}
MAMBA OUT;
}
/*
.-~~~~~~~~~-._ _.-~~~~~~~~~-.
__.' ~. .~ `.__
.'// A C 之 \./ 之 真 理 \`.
.'// | \`.
.'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \`.
.'//.-" `-. | .-' "-.\`.
.'//______.============-.. \ | / ..-============.______\`.
.'______________________________\|/______________________________`.
*/