给定一组由不重复的数,例如5、6、2 、9 、4、 1,找出这几个数字所能组成的最大三位数
三个阶段:
- 找百位数:所有数中最大的
- 找十位数:剩余所有数中最大的
- 找个位数
#include <iostream>
using namespace std;
int main()
{
int num = 0, max = 10, current;
for (int digit = 100; digit > 0; digit /= 10)
{
current = 0;
for (int n : {5, 6, 2, 4, 9, 1})// 范围for循环
{
if (current < n && n < max) current = n;
}
num += current * digit;
max = current;//将每次循环已用的数设为最大值
}
cout << num << endl;
return 0;
}