【JAVA:於航】
很多關於日期的求解問題可以歸結為:求兩個日期之間的差值(但不建議使用Date類計算日期/日歷)
// 日期差問題// 日期表示法?? 距離基點的天數
基點為1年1月1日 A-B=(A-基准)】-(B-基准)
public class A}
【JAVA:於航】
public class A //不超過三個字母的解法
{
// n 轉換為excel列地址
static String f(int n){
for(int i=0; i<=26; i++)
for(int j=(i==0)?0:1; j<=26; j++)//若i=0,則j=0 否則:i=1,j=1
for(int k=(j==0)?0:1; k<=26; k++){ //若j=0,則k=0 否則:j=1,k=1
int x = i*26*26 + j*26 + k;
if(x==n){ String s ="";
if(i>0) s += (char)(i-1+'A');
if(j>0) s += (char)(j-1+'A');另一種方法:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] iA = new int[5000];
int n = in.nextInt();
int i = 1;
while (n != 0) {
if (n % 26 == 0) {
// +64轉大寫字母,+96轉小寫字母
iA[i] = 26 + 64;
n -= 1;
} else {
iA[i] = n % 26 + 64;
}
n /= 26;
i++;
}
for (int j = i - 1; j > 0; j--) {
System.out.print((char)iA[j]);
}
}
}
}
編程確定出在雙方都不判斷失誤的情況下,對於特定的初始球數,A是否能贏?
思路:
f(局面x){
for(可能的所有走法){
試走x->y;
if(f(y)==輸)
then return 贏;
回溯;
}
return 輸;
}
//當調用的雙方共用了某個數據結構,則需要回溯,否則,當另一方下次調用時,會發生錯誤
【源代碼】【JAVA:於航】
public class A //數據量比較大時,程序會癱瘓(跑不動了)
{
static boolean f(int n){//n:球數
if(n==0) return true;//必贏
if(n>=1 && f(n-1)==false) return true;
if(n>=3 && f(n-3)==false) return true;
if(n>=7 && f(n-7)==false) return true;
if(n>=8 && f(n-8)==false) return true;
return false;
}
public static void main(String[] args){
for(int i=1; i<=50; i++){
System.out.println(i + ": " + f(i));
}
}
}
【JAVA:於航】
import java.util.*;
public class A
{
// -1: 必輸,0: 平局, 1: 必贏
static int f(char[] x)
{
String s = new String(x);
if(s.contains("LOL")) return -1;
if(s.contains("*")==false) return 0;
boolean ping = false; // 假設無法平局
for(int i=0; i<x.length; i++){
if(x[i]=='*'){
try{
x[i]='L'; //試着填L
switch(f(x)){
case -1: return 1;
case 0: ping = true;
}
x[i]='O'; //試着填O
switch(f(x)){
case -1: return 1;
case 0: ping = true;
}
}
finally{
x[i]='*';//回溯
}
}
}
if(ping) return 0;
return -1;
}
static int game(String s)
{
return f(s.toCharArray());
}
public static void main(String[] args)
{
System.out.println(game("***"));
System.out.println(game("L**L"));
System.out.println(game("L**L***L"));
System.out.println(game("L*****L"));
}
}
import java.util.*;
public class LOL
{
static Map<String, Integer> map = new HashMap<String, Integer>();
// -1: 必輸,0: 平局, 1: 必贏
static int f(char[] x)
{
String s = new String(x);
if(map.get(s) != null) return map.get(s);
if(s.contains("LOL")) {
map.put(s,-1);
return -1;
}
if(s.contains("*")==false) {
map.put(s,0);
return 0;
}
boolean ping = false;
for(int i=0; i<x.length; i++){
if(x[i]=='*'){
try{
x[i]='L';
{
int t = f(x);
if(t<0) {
map.put(s,1);
return 1;
}
if(t==0) ping = true;
}
x[i]='O';
{
int t = f(x);
if(t<0) {
map.put(s,1);
return 1;
}
if(t==0) ping = true;
}
}
finally{
x[i]='*';
}
}
}
if(ping){
map.put(s,0);
return 0;
}
map.put(s,-1);
return -1;
}
static int game(String s)
{
map.clear();
return f(s.toCharArray());
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine().trim());
for(int i=0; i<n; i++){
System.out.println(game(scan.nextLine().trim()));
}
}
}
尼姆定理:
斯普萊格-格隆第定理:任何一個無偏游戲都可以等價到尼姆堆
【JAVA:於航】
// 組合博弈論....轉化為尼姆堆:兩個人之間的空台階數,組成一個尼姆堆
import java.util.*;
public class A
{
static boolean f(int[] x){//尼姆堆中的元素即是數組中兩兩元素的差
int sum = 0;
for(int i=0; i<x.length-1; i+=2){ //最后一個元素不能使用
sum ^= x[i+1] - x[i] - 1; // 相鄰兩人差的空台階數===>尼姆堆
}for(int k=x[i]+1; k<x[i+1]; k++){
// x [i] +1:當前這一階的下級 k<x[i+1]:不能撞上/越過下一個小和尚的位置
int old = x[i]; //試着走}
【JAVA:於航】
public class DuJu
{
public static double f()
{
int a = (int)(Math.random() * 6) + 1;
int b = (int)(Math.random() * 6) + 1;
int c = (int)(Math.random() * 6) + 1;
int w = (int)(Math.random() * 6) + 1;
int n = 0;
if(a==w) n++;
if(b==w) n++;
if(c==w) n++;
if(n==3) return -10;
if(n==2) return -2;
if(n==1) return -1;
return 1;
}
public static void main(String[] args)
{
int N = 500*1000;
double sum = 0;
for(int i=0; i<N; i++)
{
sum += f();
}
System.out.println(sum/N);//即贏率 由於押注總是1元
}
}
【JAVA:於航】
import java.util.*;
public class A
{
static void show(char[][] data){
System.out.println();
for(int i=0; i<data.length; i++){
System.out.println(new String(data[i]));
}
}
static boolean f(char[][] data){ //遞歸:判斷是否贏
for(int i=0; i<data.length; i++){//檢查是否橫着連通
String s = new String(data[i]).replaceAll("0","");//把空格壓縮掉,看是否連着
if(s.contains("--")) return true;
}
for(int i=0; i<data[0].length; i++){//檢查是否豎着連通
String s = ("" + data[0][i] + data[1][i] + data[2][i]).replaceAll("0","");
if(s.contains("11")) return true;
}
for(int i=0; i<data.length; i++){//典型的博弈框架
for(int j=0; j<data[i].length; j++){
if(data[i][j]=='0'){
try{
data[i][j] = '1';
if(f(data)==false) return true;
data[i][j] = '-';
if(f(data)==false) return true;
}
finally{
data[i][j] = '0';
}
}
}
}
return false; //所有的試走都為true
}
static void solve(char[][] data){
for(int i=0; i<data.length; i++){
for(int j=0; j<data[i].length; j++){
if(data[i][j]=='0'){
try{
data[i][j] = '1';
if(f(data)==false) {
show(data);
//return
}
data[i][j] = '-';
if(f(data)==false){
show(data);//找到了一個解
//return;
}
}
finally{
data[i][j] = '0';
}
}
}
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
char[][] data = new char[3][];
data[0] = scan.nextLine().trim().toCharArray();
data[1] = scan.nextLine().trim().toCharArray();
data[2] = scan.nextLine().trim().toCharArray();
solve(data);
//System.out.println(f(data));
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。