[伯俊]1436号映画監督


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

質問する


末日数字とは、ある数字のうち6「少なくとも3つ以上連続」の数字を指す.最小の末日数字は666で、次いで1666266663666です.に等しい
そのため、ʤは第1部映画の名前を世界末日666、第2部映画の名前を世界末日1666と命名した.一般的に、N本目の映画のタイトルは世界の終わり(N番目の小さな終わりの数字)に等しい.
プログラムを作成して、N本目の映画タイトルの数字を印刷してください.ʤはいつもこのシリーズを順番に作って、他の映画を作らない.

入力


最初の行には、数字Nが与えられる.Nは10000以下の自然数である.

しゅつりょく


第1行目には、第N本目の映画タイトルの数字が出力される.

プール1:入力値を数値として認識


https://cocoon1787.tistory.com/155
#include<iostream>
using namespace std;

int main()
{
	// 몇번째 종말의 수를 구할지 입력 받기
	int N;
	cin >> N;

	int num = 665;
	int cnt = 0;
	int temp; 

	// N번째 종말의 수를 구할 때까지 반복
	while (cnt != N) { 
		num++; // 666부터 1씩 증가시키면서 종말의 수 찾기

		// 종말의 수를 찾아보자!
		temp = num;
		while (temp != 0) {
			// 종말의 수일 경우 cnt++
			if (temp % 1000 == 666) {
				cnt++; 
				break;
			}
			else {
				temp /= 10;
			}
		}

		/*  내부 루프 탈출
		Case1. 종말의 수 발견해서 break한 경우
		1) cnt == N이라면, 외부 루프도 탈출하여 종말의 수 출력
		2) cnt != N이라면 'N번째에 맞는' 종말의 수를 구하기 위해 다시 num 증가시키기

		Case2. temp == 0이 된 경우
		cnt != N인 상태에서 종말의 수를 구할 때까지 num 증가시키기
		*/
	}

	// N번째에 해당하는 종말의 수 출력하기
	cout << num;

	return 0;
}

プール2:入力値を文字列として認識する


https://j3sung.tistory.com/224

stringのfind関数を理解してください!


https://www.cplusplus.com/reference/string/string/find/
string (1)
size_t find (const string& str, size_t pos = 0) const noexcept;
c-string (2)
size_t find (const char s, size_t pos = 0) const;
buffer (3)
size_t find (const char s, size_t pos, size_type n) const;
character (4)
size_t find (char c, size_t pos = 0) const noexcept;
文字列内の特定の部分文字列を検索するために使用します.posを指定すると、posまたはその後のインデックスのみが検索されます.もちろん、1文字だけを一致させることはできません.パラメータとして渡される文字列を完全に一致させる必要があります.

1.パラメータ

  • str:検索する部分文字列
  • pos:検索を開始する場所(文字列の長さより大きい値の場合、絶対一致する文字列は見つかりません.デフォルトでは、インデックス0から検索を開始します).
  • s:文字配列へのポインタ
    (3)番号のようにnを指定すると,一致するシーケンスは配列の最初のn文字である.逆に,(2)番号のようにnを指定しないとnullで終わるシーケンスが現れる.マッチングシーケンスの長さはnull文字が最初に現れる位置によって決まります.
  • n:一致文字列の長さ
  • 2.戻り値


    検索する文字列の最初の文字の位置を返します.文字列が見つからない場合はstd::string::nposを返します.
    static const size_t npos = -1;
    nposはsize t型の最値の定数である.size tは符号なし整数タイプであり、−1はそのタイプの最大値を表す.この値がlen関数のパラメータとして使用される場合、「文字列の末尾」を表します.逆に、戻り値として使用すると、「一致する値がない」ことを意味します.

    3.例

    // string::find
    #include <iostream> // std::cout
    #include <string>   // std::string
    
    int main()
    {
        std::string str("There are two needles in this haystack with needles.");
        std::string str2("needle");
    
        // different member versions of find in the same order as above:
        std::size_t found = str.find(str2);
        if (found != std::string::npos)
            std::cout << "first 'needle' found at: " << found << '\n';
    
        found = str.find("needles are small", found + 1, 6);
        if (found != std::string::npos)
            std::cout << "second 'needle' found at: " << found << '\n';
    
        found = str.find("haystack");
        if (found != std::string::npos)
            std::cout << "'haystack' also found at: " << found << '\n';
    
        found = str.find('.');
        if (found != std::string::npos)
            std::cout << "Period found at: " << found << '\n';
    
        // let's replace the first needle:
        str.replace(str.find(str2), str2.length(), "preposition");
        std::cout << str << '\n';
    
        return 0;
    }

    find関数を用いて1436回解く

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	// 몇번째 종말의 수를 구할지 입력 받기
    	int N;
    	cin >> N;
    
    	int num = 665;
    	int cnt = 0;
    
    	// N번째 종말의 수를 구할 때까지 반복
    	while (cnt != N) {
    		num++; // 666부터 검색 시작
    
    		// 666이 포함된 종말의 수를 발견하면
    		if (to_string(num).find("666") != string::npos) {
    			cnt++; // cnt == N이 되면 루프 탈출해서 결과 출력
    		}
    		// 종말의 수가 아닐 경우, num++하면서 검색 반복하기
    	}
    
    	// N번째 종말의 수 출력하기
    	cout << num;
    
    	return 0;
    }