No.304 Key (1)
タグ : / 解いたユーザー数 468
作問者 :
koyumeishi
Problem Statement
Yuki-kun has forgotten the PIN of his bicycle lock.
Tell Yuki-kun some numbers and help him somehow unlock it.
The PIN is a 3-digit number (000 to 999).
When you print a 3-digit number (including leading zeros) to standard output,
the judge returns one of the strings "locked" or "unlocked" to standard input.
When "locked" is returned, the lock has not been unlocked yet. Try another number.
When "unlocked" is returned, the lock has been unlocked. Terminate your program. Any extra output after the query that unlocked the lock results in WA.
You may try as many times as you like within the time limit.
Input and Output
This problem is a reactive problem. Perform input and output operations according to the problem statement.
In particular, do not forget to end each output with a newline and flush it.
For notes on input and output in reactive problems, see the summary of reactive problems on the yukicoder wiki, as well as
No.246,
No.253, and others.
Judge Code (for reference)
This is the code used for judging. (C++; it reads the answer string from the file given as the first command-line argument.)
2016/05/04: The judge code has been changed. Invalid formats (such as output other than digits), multiple outputs on one line, and extra output after unlocking result in WA.
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
string input(istream& is){
string ret;
getline(is, ret); //回答コードから受け取った文字列
assert(ret.size() == 3); //長さは 3
for(char c : ret){ //数字のみかチェック
assert('0' <= c && c <= '9');
}
return ret;
}
int main(int argc, char* argv[]){
ifstream ifs(argv[1]);
string ans = input(ifs); //第一引数のファイルから正解の文字列を取得
cerr << "Answer : " << ans << endl;
while(1){
string in = input(cin); //標準入力から回答の文字列を取得
if(in == ans){ //正解なら "unlocked" を出力して終了
cout << "unlocked" << endl;
if(getline(cin,in)){ //余計な出力がないか確認
cerr << "Wrong Answer : too many outputs, \"" << in << "\"" << endl;
abort();
}
return 0;
}else{ //不正解なら "locked" を出力
cout << "locked" << endl;
}
}
return 0;
}
Sample
Sample 1
PIN
000
This is an example of input and output when the PIN is "000".
Output
003 002 001 000
Input
locked locked locked unlocked
The lock was unlocked on the 4th attempt.
The first 3 outputs do not match the PIN, so "locked" is returned for them.
Print a 3-digit number, including leading zeros.
Sample 2
PIN
123
This is an example of input and output when the PIN is "123".
Output
120 121 122 123
Input
locked locked locked unlocked
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。