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.0034 sec