[BaekJun, C++] 1018: Movie Director

 

https://www.acmicpc.net/problem/1436

문제

It is said that 666 represents the end of the world. Therefore, many blockbuster movies often use titles containing the number 666. Director Sean is in charge of a movie series called "The End of the World." When George Lucas created Star Wars, he named the movies as Star Wars 1, Star Wars 2, Star Wars 3, Star Wars 4, Star Wars 5, and Star Wars 6, and similarly, Peter Jackson named his movies as The Lord of the Rings 1, The Lord of the Rings 2, The Lord of the Rings 3 when making "The Lord of the Rings." However, Sean decided to make his movie titles a bit different to demonstrate that he surpasses George Lucas and Peter Jackson.

The term "end of the world number" refers to a number with at least three consecutive 6s. The smallest end of the world number is 666, and the next larger numbers are 1666, 2666, 3666, and so on. Therefore, Sean's first movie's title will be "The End of the World 666," and the second movie's title will be "The End of the World 1666," and so on. In general, the title of the Nth movie made by Sean is "The End of the World" followed by the Nth smallest end of the world number.

Input

the first line is N, N is an integer less than or equal to 10,000

Output

It prints the Nth movie title

 

Code

#include <string> #include <vector> #include <algorithm> #include <iostream> using namespace std; int n; int main() { cin >> n; int cnt = 0; int i = 1; while (n != cnt) { int temp = i; while (temp) { if (temp % 1000 == 666) { // check if the last 3 digits are 666 cnt++;//if they are, break and add count break; } else { temp = temp / 10; //if not, divide to check the next 3 consecutive 3 digits } } i++;// check every positive integer starting from 1 } cout << i - 1; }

It was difficult to find if there are three consecutive 6's while it's easy to find just one


but just check if the nubmer mod 1000 is 666 and keep on dividing by 10

so it's a Brute Force Problem


 

Time Complexity

O(n^2)

 

Space Complexity
O(1)


Comments

Popular posts from this blog

[BaekJun, C++] 1018: Repainting a Chess Board

Binary Search: The Efficient Solution for Sorted Arrays

[BaekJun,C++]1929: finding primes