zoukankan      html  css  js  c++  java
  • 博弈论

    1. Vijos 萌萌的糖果博弈 && 吃糖果游戏

    每次可以任意拿走其中一堆糖果;如果这时候另一堆糖果数目多于1粒,就把它任意分成两堆,否则就把剩下的一粒糖果取走并获得这次博弈的胜利。

    萌萌的糖果博弈
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 using namespace std;
     5 int main()
     6 {
     7     string a,b;
     8     while(cin>>a)
     9     {
    10         if(a[0]=='-')break;
    11         cin>>b;
    12         int a1= a[a.length()-1]-'0';
    13         int b1= b[b.length()-1]-'0';
    14         if((a1%5==2||a1%5==3)&&(b1%5==2||b1%5==3))
    15             printf("SheepDaddy
    ");
    16         else
    17             printf("MengMeng
    ");
    18     }
    19     return 0;
    20 }

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 using namespace std;
     5 int main()
     6 {
     7     string a,b;
     8     while(cin>>a>>b)
     9     {
    10         int a1= a[a.length()-1]-'0';
    11         int b1= b[b.length()-1]-'0';
    12         if((a1%5==2||a1%5==3)&&(b1%5==2||b1%5==3))
    13             printf("Shadow
    ");
    14         else
    15             printf("Matrix67
    ");
    16     }
    17     return 0;
    18 }
    吃糖果游戏

     2. 巴什博奕

      只有一堆n个物品,两个人轮流从中取物,规定每次最少取一个,最多取m个,最后取光者为胜。

     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int n,m;
     6     while(cin>>n>>m)
     7       if(n%(m+1)==0)  cout<<"后手必胜"<<endl;
     8       else cout<<"先手必胜"<<endl;
     9     return 0;
    10 }
    待检验

    3. 威佐夫博弈(Wythoff Game)(*黄金分割)

      有两堆各若干的物品,两人轮流从其中一堆取至少一件物品,至多不限,或从两堆中同时取相同件物品,规定最后取完者胜利。

    POJ 1067 

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 int main()
     4 {
     5     int a,b;
     6     while(~scanf("%d%d",&a,&b))
     7     {
     8         putchar( ((a<b?a:b)==(int)(abs(a-b)*1.618033988749895)?'0':'1') );
     9         putchar('
    ');
    10     }
    11     return 0;
    12 }
    恶心的AC代码

    4. 尼姆博弈(Nimm Game):

      有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取部分或全部物品,最少取一件,取到最后一件物品的人获胜。

    结论就是:把每堆物品数全部异或起来,如果得到的值为0,那么先手必败,否则先手必胜。

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <iostream>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,ans,temp;
     8     while(cin>>n)
     9     {
    10         temp=0;
    11         for(int i=0;i<n;i++)
    12         {
    13             cin>>ans;
    14             temp^=ans;
    15         }
    16         if(temp==0)  cout<<"后手必胜"<<endl;
    17         else cout<<"先手必胜"<<endl;
    18     }
    19     return 0;
    20 }
    View Code

    5. 斐波那契博弈:

      有一堆物品,两人轮流取物品,先手最少取一个,至多无上限,但不能把物品取完,之后每次取的物品数不能超过上一次取的物品数的二倍且至少为一件,取走最后一件物品的人获胜。

    结论是:先手胜,当且仅当n不是斐波那契数(n为物品总数)

     1 #include <iostream>  
     2 #include <string.h>  
     3 #include <stdio.h>  
     4 using namespace std;  
     5 const int N = 55;    
     6 int f[N];   
     7 void Init()  
     8 {  
     9     f[0] = f[1] = 1;  
    10     for(int i=2;i<N;i++)  
    11         f[i] = f[i-1] + f[i-2];  
    12 }    
    13 int main()  
    14 {  
    15     Init();  
    16     int n;  
    17     while(cin>>n)  
    18     {  
    19         if(n == 0) break;  
    20         bool flag = 0;  
    21         for(int i=0;i<N;i++)  
    22         {  
    23             if(f[i] == n)  
    24             {  
    25                 flag = 1;  
    26                 break;  
    27             }  
    28         }  
    29         if(flag) puts("Second win");  
    30         else     puts("First win");  
    31     }  
    32     return 0;  
    33 } 
    View Code

    博文参考:http://blog.csdn.net/ac_gibson/article/details/41624623

  • 相关阅读:
    这个Pandas函数可以自动爬取Web图表
    /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/local/python3.6/lib/python3.6/site-packages/paddle/fluid/core_avx.so)
    kubernetes yaml详解
    centos 安装kubernetes
    初识kafka-connect
    Docker1:本地连接Docker中的mysql,实现数据同步
    git分支管理规范
    cat 配置告警规则
    Java的即时编译(Just In Time, JIT)及其优化
    java自定义实现一个缓存器
  • 原文地址:https://www.cnblogs.com/A--Q/p/6805662.html
Copyright © 2011-2022 走看看