找出由5、6、2、9、4、1的3个数字组成的最大的3位数的程序

//找出由5、6、2、9、4、1的3个数字组成的最大的3位数的程序


/*解决问题的3个阶段是通过外层的for循环控制。
第一个循环周期digit=100,表示找百位数;第二个循环周期digit=10,表示找十位数;第三个循环周期digit=1,表示找个位数。
没词都是找剩余元素中的最大值。这通过里层的for循环实现的。程序用max表示上一次找到的数字,本次要找的是小于max的最大数字。
这个最大的数字存储变量curent中。


*/
#include <iostream>
using namespace std;


int main(){
	int num = 0, max = 10, current;


	for (int digit = 100; digit > 0; digit = digit / 10){
		current = 0;
		for (int n : {5, 6, 2, 4, 9, 1})
		if (n > current && n < max) current = n;
		num += digit * current;
		max = current;
	}

	cout << num << '\t';

	system("pause");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39026129/article/details/79661911