U E D R , A S I H C RSS

Ugly Numbers/곽세환

2

O
  • . .
  • STL 편하
  • The 1500'th ugly number is <>..

2

~cpp 
//136
#include <iostream>
#include <list>
using namespace std;

void main()
{
	int cnt = 1500;
	list<unsigned int> numbers;
	numbers.push_back(1);
	unsigned int temp;

	while (cnt)
	{
		temp = numbers.front();
		numbers.pop_front();
		cnt--;
		numbers.push_back(temp * 2);
		numbers.push_back(temp * 3);
		numbers.push_back(temp * 5);
		numbers.sort();
		numbers.unique();
	}

	cout << "The 1500'th ugly number is " << temp << "." << endl;
}

1


X
1500 2 ㅡ.ㅡ
...

1

~cpp 
#include <iostream>
using namespace std;

void main()
{
	int cnt;
	cin >> cnt;
	
	int num = 1, temp;
	cnt--;

	while (cnt)
	{
		num++;

		temp = num;
		while (temp % 2 == 0)
			temp = temp / 2;
		while (temp % 3 == 0)
			temp = temp / 3;
		while (temp % 5 == 0)
			temp = temp / 5;
		
		if (temp == 1)
			cnt--;
	}

	cout << num << endl;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:19
Processing time 0.0146 sec