小明寫了一個函式,可以用來玩剪刀石頭布,請你也寫一個函式,來跟他寫的函式來對決!
然而,他的程式碼不小心被你看到了!如下:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int opponent(){
int temp = rand() % 3;
if (temp == 0) return 5; // 出布
else if (temp == 1) return 2; // 出剪刀
else return 0; // 出石頭
}
int you(int seed);
// 請完成 you() 函式。你的程式碼會被插在這裡。
bool win(int a, int b){
if ((a == 5) and (b == 2)) return true; // 剪刀打贏布
if ((a == 2) and (b == 0)) return true; // 石頭打贏剪刀
if ((a == 0) and (b == 5)) return true; // 布打贏石頭
return false;
}
int main() {
int seed;
cin >> seed;
srand(seed);
int a = opponent();
int b = you(seed);
if (win(a,b)) std::cout << "You win!" << std::endl;
else std::cout << "You lose!" << std::endl;
return 0;
}
在上面的程式中,opponent() 是小明寫的函式, you() 是你寫的函式,5 代表出布,2 代表出剪刀,0 代表出石頭。win() 會判斷你有沒有贏,如果你贏了,函式會回傳 true,如果沒贏,則會回傳 false。
請觀察上面的程式碼,想辦法完成 you() 函式,來讓 seed 不論給多少,你的函式都能夠打贏 opponent()!
(你不需要處理輸入的問題)輸入會有一個整數,代表隨機種子。
$0\leq seed\leq 231 -1$
(你不需要處理輸出的問題)如果你的函式打贏了對手,程式就會輸出 "You win!" 並換行。
NEOJ Problem 982
No. | Testdata Range | Constraints | Score |
---|---|---|---|
1 | 0 | 10 | |
2 | 1 | 10 | |
3 | 2 | 10 | |
4 | 3 | 10 | |
5 | 4 | 10 | |
6 | 5 | 10 | |
7 | 6 | 10 | |
8 | 7 | 10 | |
9 | 8 | 10 | |
10 | 9 | 10 |