Skip to content

Instantly share code, notes, and snippets.

@illustris
Created February 22, 2020 18:08
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 illustris/a1091e93ce529a4828ac149a4af303fc to your computer and use it in GitHub Desktop.
Save illustris/a1091e93ce529a4828ac149a4af303fc to your computer and use it in GitHub Desktop.
#define N 4
int *sort(int *arr, int len)
{
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
int tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
}
}
int main()
{
int in,last;
int ar[N];
scanf("%d",&in);
do
{
last=in;
int big=0;
int small=0;
for(int i=0;i<N;i++)
{
ar[i]=in%10;
in/=10;
}
sort(ar,N);
for(int i=0;i<N;i++)
{
big*=10;
big+=ar[N-1-i];
small*=10;
small+=ar[i];
}
in=big-small;
printf("%d\n",in);
} while(in != last);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment