解题思路,用数字记录每一位的数字,然后从高位还原,遇见的第一个6改为9,再还原;
#include <stdio.h>
#include <stdlib.h>
int changemax(int x, int count);
int maximum69Number(int num);
int main(int argc, char const *argv[])
{
int num = 99666;
int max = maximum69Number(num);
printf("%d\n", max);
return 0;
}
int maximum69Number(int num)
{
int num1 = num;
int count = 0;
int max = 0;
int flag = 0;
while (num1)
{
num1 = num1 / 10;
count++;
}
int *data = (int *)malloc(sizeof(int) * count);
int i = 0;
while (num)
{
data[i] = num % 10;
num = num / 10; //
i++;
}
while (count)
{
if (data[count - 1] == 9)
{
max += changemax(data[count - 1], count - 1);
count--;
continue;
}
if (data[count - 1] == 6 && flag == 0)
{
data[count - 1] = 9;
max += changemax(data[count - 1], count - 1);
flag = 1;
count--;
continue;
}
max += changemax(data[count - 1], count - 1);
count--;
}
free(data);
return max;
}
int changemax(int x, int count)
{
int max = x;
while (count)
{
max = max * 10;
count--;
}
return max;
}