Skip to content

Instantly share code, notes, and snippets.

@dd1994
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dd1994/e67bbd729f7644cfeff2 to your computer and use it in GitHub Desktop.

Select an option

Save dd1994/e67bbd729f7644cfeff2 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// big -> small
void BubbleSort(int a[]) {
int i, j, temp;
for (i = 0; i < 3; i++) {
for (j = i + 1; j < 4 ; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int Change(int a[4], bool b) {
if (b == 1) {
return a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
}
else
return a[0] + a[1] * 10+ a [2]*100+a[3]*1000;
}
void GetEnchNum(int a[], int n) {
int fac = 1000, i;
a[0] = n / fac;
n= n - a[0] * fac;
for (i = 1; i < 4; i++) {
fac = fac / 10;
a[i] = n / fac;
n = n - a[i] * fac;
}
}
int main(void) {
int n;
int a[4];
cout << "Enter a four-digit:";
cin >> n;
while (n >= 10000 || n < 999) {
cout << "Please enter it again:";
cin >> n;
}
while (n != 6174) {
GetEnchNum (a, n);
BubbleSort(a);
n = Change(a, 1) - Change(a,0);
cout << Change(a,1) << "-" << Change(a,0) << "=" << n <<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment