要求:给定一个正整数(十进制形式),输出消除重复数字后的最大整数
例如: 输入12355996, 输出965321
基本思路:使用数组下标实现
1. 定义一个长度为10的数组(因为十进制正整数每一位数字范围为0~9),并且将所有元素赋值为无效值
2. 遍历输入整数的每一个数字,将数字看做数组下标,将对应下标的元素值设置为有效。遍历后,即完成了去重
3. 数组中有效值的下标可以组成去重后的最大整数
ps:程序存在scanf溢出的情况,可能之后不能使用scanf来进行输入动作。
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
unsigned int get_max_val(unsigned long int val)
{
bool table[10];
bool *ptable = table;
unsigned long int res = 0;
signed char i;
memset(ptable, false, sizeof(table) / sizeof(table[0]));
while(val)
{
table[val % 10] = true;
val = val / 10;
}
for(i = 9; i >= 0; i--)
{
if(table[i])
{
res = res * 10 + i;
}
}
return res;
}
int main()
{
unsigned long int val;
while(1)
{
printf("please input value: ");
scanf("%lu", &val);
printf("The result is: %d\n", get_max_val(val));
}
}