Program to find the largest 3-digit number consisting of 3 numbers 5, 6, 2, 9, 4, 1

//Program to find the largest 3-digit number consisting of 3 numbers of 5, 6, 2, 9, 4, 1


/* The 3 stages of problem solving are controlled by the outer for loop.
The first cycle digit=100 means to find the hundreds digit; the second cycle digit=10 means to find the tens digit; the third cycle digit=1 means to find the one digit.
No word is to find the maximum value of the remaining elements. This is achieved through the inner for loop. The program uses max to represent the number found last time, and this time it is looking for the largest number less than max.
This largest number is stored in the variable 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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326502059&siteId=291194637