6174问题
作者:互联网
#include<stdio.h>
#include<stdlib.h>
void select_sort(int data[], int len)
{
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
if (data[i] > data[j])
{
int temp = data[i];
data[i]=data[j];
data[j]= temp;
}
}
}
}
int kaprekar(int num)
{
while (num != 6174)
{
int bits[4] = { 0 };
int max_num = 0,min_num=0;
int k = 1000;
for (int i = 0; i < 4; i++)//获取4个位
{
bits[i] = num / k;
num %= k;
k /= 10;
}
select_sort(bits, 4);
for (int i = 0; i < 4; i++)
{
max_num = max_num * 10 + bits[4 - i - 1];
min_num = min_num * 10 + bits[i];
}
num = max_num - min_num;
}
return 1;
}
int main()
{
for (int i = 1000; i < 9999; i++)
{
kaprekar(i);
}
system("pause");
return 0;
}
标签:min,int,bits,问题,++,num,6174,data 来源: https://blog.csdn.net/qq_27182175/article/details/112915783