51nod Bash游戏V1 Bash游戏V2

来源:互联网 发布:gz解压命令 linux 编辑:程序博客网 时间:2024/05/22 13:15

1066 Bash游戏
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N和K,问最后谁能赢得比赛。
例如N = 3,K = 2。无论A如何拿,B都可以拿到最后1颗石子。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行2个数N,K。中间用空格分隔。(1 <= N,K <= 10^9)
Output
共T行,如果A获胜输出A,如果B获胜输出B。
Input示例
4
3 2
4 2
7 3
8 3
Output示例
B
A
A
B
题解:
如果n%(k+1)等于0,先手拿1–k内的任何一个数m,后手都会拿剩下(k+1-m)个石子,所以说n%(k+1)==0先手是必败态,如果n%(k+1)!=0,先手可以先拿走n%(k+1)个,此后,后手是必败态

#include <stdio.h>#include <iostream>#include <cmath>using namespace std;int main(){    int T;    scanf("%d",&T);    while(T--){        int n,k;        scanf("%d %d",&n,&k);        if(n%(k+1)){            puts("A");        } else {            puts("B");        }    }    return 0;}

1067 Bash游戏 V2
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次只能拿1,3,4颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 2。A只能拿1颗,所以B可以拿到最后1颗石子。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^9)
Output
共T行,如果A获胜输出A,如果B获胜输出B。
Input示例
3
2
3
4
Output示例
B
A
A

用sg函数打表

#include <stdio.h>#include <iostream>#include <string.h>using namespace std;const int N=1000;const int MAXN=1000;int f[N];//这道题N=3,f[3]={1,3,4};int SG[MAXN];bool vis[MAXN];void sg(int n){    memset(SG,0,sizeof(SG));    for(int i=1;i<=n;i++){        memset(vis,false,sizeof(vis));        for(int j=0;j<N;j++){            if(i-f[j]>=0){                vis[SG[i-f[j]]]=true;            }        }        for(int j=0;j<=n;j++){            if(!vis[j]){                SG[i]=j;                break;            }        }    }}int main(){    int n;    scanf("%d",&n);    sg(n);    for(int i=0;i<=n;i++){        printf("i==%d sg==%d\n",i,SG[i]);    }    return 0;}

发现n%7=0或n%7=2时后手赢,其他先手赢

#include <stdio.h>#include <iostream>#include <string.h>using namespace std;int main(){    int T;    scanf("%d",&T);    for(int i=0;i<T;i++){        int n;        scanf("%d",&n);        if(n%7==0||n%7==2){            printf("B\n");        } else {            printf("A\n");        }    }    return 0;}
原创粉丝点击