Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. #define MAX_CHAR 10
  6.  
  7. string sortString(string str, bool SortType);
  8.  
  9. int main(void)
  10. {
  11.     string a, max, min,b;
  12.     cin >> a;
  13.     b=a;
  14.     int pv = 0;
  15.     int val = 1;
  16.     int counter=0;
  17.     while (true)
  18.     {
  19.         max = sortString(a, true);
  20.         min = sortString(a, false);
  21.         counter++;
  22.         int maxi = stoi(max);
  23.         int mini = stoi(min);
  24.  
  25.         int val = maxi - mini;
  26.         if (pv == val){
  27.             cout<<"Original number is: "<<b<<" and chain length is "<<counter;
  28.             break;
  29.         }  
  30.         if(counter>1000){
  31.             cout<<"Orignal number is: "<<b<<" and chain length is > 1000";
  32.             break;
  33.         }
  34.        
  35.     //  cout << maxi << " - " << mini << " = " << val << "\n";
  36.         a = to_string(val);
  37.         pv = val;
  38.     }
  39. }
  40.  
  41. string sortString(string str, bool SortType)
  42. {
  43.     string Ret;
  44.  
  45.     int len = str.length();
  46.  
  47.     int charCount[MAX_CHAR] = { 0 };
  48.  
  49.     for (int i = 0; i < len; i++)
  50.         charCount[i] = (int)(str[i]);
  51.  
  52.     int Min = 0;
  53.  
  54.     for (int i = 0; i < len; i++)
  55.     {
  56.         for (int j = 0; j <= i; j++)
  57.         {
  58.  
  59.             if (SortType)
  60.             {
  61.                 if (charCount[j] < charCount[i])
  62.                 {
  63.                     Min = charCount[j];
  64.                     charCount[j] = charCount[i];
  65.                     charCount[i] = Min;
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 if (charCount[j] > charCount[i])
  71.                 {
  72.                     Min = charCount[j];
  73.                     charCount[j] = charCount[i];
  74.                     charCount[i] = Min;
  75.                 }
  76.  
  77.             }
  78.         }
  79.     }
  80.  
  81.     for (int i = 0; i < len; i++)
  82.         Ret += charCount[i];
  83.  
  84.     return Ret;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement