Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #define MAX_CHAR 10
- string sortString(string str, bool SortType);
- int main(void)
- {
- string a, max, min,b;
- cin >> a;
- b=a;
- int pv = 0;
- int val = 1;
- int counter=0;
- while (true)
- {
- max = sortString(a, true);
- min = sortString(a, false);
- counter++;
- int maxi = stoi(max);
- int mini = stoi(min);
- int val = maxi - mini;
- if (pv == val){
- cout<<"Original number is: "<<b<<" and chain length is "<<counter;
- break;
- }
- if(counter>1000){
- cout<<"Orignal number is: "<<b<<" and chain length is > 1000";
- break;
- }
- // cout << maxi << " - " << mini << " = " << val << "\n";
- a = to_string(val);
- pv = val;
- }
- }
- string sortString(string str, bool SortType)
- {
- string Ret;
- int len = str.length();
- int charCount[MAX_CHAR] = { 0 };
- for (int i = 0; i < len; i++)
- charCount[i] = (int)(str[i]);
- int Min = 0;
- for (int i = 0; i < len; i++)
- {
- for (int j = 0; j <= i; j++)
- {
- if (SortType)
- {
- if (charCount[j] < charCount[i])
- {
- Min = charCount[j];
- charCount[j] = charCount[i];
- charCount[i] = Min;
- }
- }
- else
- {
- if (charCount[j] > charCount[i])
- {
- Min = charCount[j];
- charCount[j] = charCount[i];
- charCount[i] = Min;
- }
- }
- }
- }
- for (int i = 0; i < len; i++)
- Ret += charCount[i];
- return Ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement