A. Game with Integers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vanya and Vova are playing a game. Players are given an integer 𝑛�. On their turn, the player can add 11 to the current integer or subtract 11. The players take turns; Vanya starts. If after Vanya's move the integer is divisible by 33, then he wins. If 1010 moves have passed and Vanya has not won, then Vova wins.
Write a program that, based on the integer 𝑛�, determines who will win if both players play optimally.
Input
The first line contains the integer 𝑡� (1≤𝑡≤1001≤�≤100) — the number of test cases.
The single line of each test case contains the integer 𝑛� (1≤𝑛≤10001≤�≤1000).
Output
For each test case, print "First" without quotes if Vanya wins, and "Second" without quotes if Vova wins.
Example
input
Copy
6
1
3
5
100
999
1000
output
Copy
First Second First First Second First
- #include
- using namespace std;
-
- int main()
- {
- int n;
- scanf("%d",&n);
- while(n--)
- {
- int num;
- scanf("%d",&num);
- bool flag=false;
- if((num+1)%3==0||(num-1)%3==0) flag=true;
- if(flag==true) printf("First\n");
- else printf("Second\n");
- }
- return 0;
- }
题目的意思是说给定一个数字,有两个参赛者,a和b,每个参赛者轮流对该数字进行操作,每一次操作可以加一或者减一,假设a操作之后该数字可以被3整除,就输出First,十次操作之后还不能被3整除就输出Second。
假设该数字原本可以被3整除,经过一次加一或者减一的操作就一定不可以被3整除,假设a进行加一的操作,b进行加一的操作,原本数字可以被3整除,增加2是不可以被3整除的,此时a再增加一,该数字就可以被3整除了,所以说a增加一,b就一定要减小一,每一次b都使用与a相反的操作,就可以保证a一定无法获得胜利。
假设该数字不可以被3整除,是什么情况呢?该数字不可以被3整除,那么该数字对3取余的结果是1或2,假设该数字对3取余的结果是1,那么a把该数字减小1就可以使得该数字可以被3整除,a获得胜利,假设该数字对3取余的结果是2,a把该数字增加1就可以使得该数字可以被3整除,a获得胜利。
综上所述,只需要判断给定的数字能否被3整除即可,可以被3整除就输出Second,不可以被3整除就输出First
- #include
- using namespace std;
-
- int main()
- {
- int t;
- scanf("%d",&t);
- while(t--)
- {
- int n;
- scanf("%d",&n);
- if(n%3==0) printf("Second\n");
- else printf("First\n");
- }
- return 0;
- }
第一份代码是赛时写的,第二份代码明显思路更加清晰
- #include
-
- using namespace std;
-
- void solve() {
- int n;
- cin >> n;
- if (n % 3) {
- cout << "First\n";
- } else {
- cout << "Second\n";
- }
- }
-
- int main() {
- int t;
- cin >> t;
- while (t--) {
- solve();
- }
- }
第三份代码是官方题解,非常简洁明了