牛牛和羊羊都很喜欢青草。今天他们决定玩青草游戏。
最初有一个装有n份青草的箱子,牛牛和羊羊依次进行,牛牛先开始。在每个回合中,每个玩家必须吃一些箱子中的青草,所吃的青草份数必须是4的x次幂,比如1,4,16,64等等。不能在箱子中吃到有效份数青草的玩家落败。假定牛牛和羊羊都是按照最佳方法进行游戏,请输出胜利者的名字。
输入包括t+1行。 第一行包括一个整数t(1 ≤ t ≤ 100),表示情况数. 接下来t行每行一个n(1 ≤ n ≤ 10^9),表示青草份数
对于每一个n,如果牛牛胜利输出"niu",如果羊羊胜利输出"yang"。
3 1 2 3
niu yang niu
#include<iostream>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
int f = n%5;
if (f == 0 || f == 2) cout << "yang" << endl;
else cout << "niu" << endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t-- > 0) {
int n;
scanf("%d", &n);
if(n % 5 == 0 || n % 5 == 2)
cout << "yang" << endl;
else
cout << "niu" << endl;
}
return 0;
}
本题可以先使用暴力算法求解n较小时的基础情况,然后观察规律,可以发现序列以0,1,0,1,1,0,1,0,1,1...的规律延续,其中0表示先手必败,1表示先手必胜。因此该问题转化为由n对5取模,余0或2时必败,其他情况必胜。
import java.util.*; public class Main { private static final int MAX = (int)1e9; private static boolean[] dp = new boolean[MAX + 5]; private static final int strategyN = (int) (Math.log(MAX) / Math.log(4)) + 1; private static final int[] strategies = new int[strategyN];; static { for (int i= 0; i<strategyN; i++) { strategies[i] = (int)Math.pow(4, i); Arrays.fill(dp, false); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 1; i <= MAX; i++) { for (int j = 0; j < strategyN; j++) { if (strategies[j] <= i) { dp[i] |= !dp[i - strategies[j]]; } } } int T = sc.nextInt(); while (T-- != 0) { for (int i = 0; i != MAX; i++) { if (dp[sc.nextInt()]) { System.out.println("niu"); } else { System.out.println("yang"); } } return; } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T-- != 0) { int in = sc.nextInt(); System.out.println( (in % 5 == 0|| in % 5 == 2) ? "yang" : "niu"); } return; } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int i = 0; // 这是一道规律题 // 0 -> yang // 1 -> niu // 2 -> yang // 3 -> niu // 4 -> niu // 5 -> yang // 6 -> niu // 7 -> yang // 8 -> niu // 9 -> niu while(i < t) { long n = sc.nextLong(); if(n % 5 == 0 || n % 5 == 2) System.out.println("yang"); else System.out.println("niu"); i++; } sc.close(); } }算法还没想好,规律就找出来了,emmmmm,对比上面大佬的解法,自己还是菜
参考:
https://blog.csdn.net/huyahuioo/article/details/80081031
一通秀的飞起的分析之后,模5为1,3,4时牛牛赢,否则输。***神奇
for _ in range(int(input())):
print("niu" if int(input())%5 in [1,3,4] else 'yang')
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strN; while((strN = br.readLine()) != null){ int n = Integer.parseInt(strN); for(int i = 0; i < n; i++) solve(Integer.parseInt(br.readLine())); } } private static void solve(int num) { if(num % 5 == 0 || num % 5 == 2) System.out.println("yang"); else System.out.println("niu"); } }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); int n = cin.nextInt();// 总的情况 String str[] = {"yang","niu","yang","niu","niu"}; for (int i = 0; i < n; i++) { long a=cin.nextLong(); System.out.println(str[(int) (a%5)]); } } }
#include <iostream>using namespace std;
n = int(input()) for i in range(n): if int(input())%5 in [1, 3, 4]: print('niu') else: print('yang') # 4的幂次结果%5只会是1或4 # r%5 == 2, x%5 == 1 or 4 , (r-x)%5 == 1 or 3 # r%5 == 2, x%5 == 4, (r-x)%5 == 3 """ for i in range(int(input())): print('niu' if int(input())%5 in [1, 3, 4] else 'yang') """