首页 > 其他 > 详细

1069 Nim游戏(51NOD基础)

时间:2017-10-06 12:06:24      阅读:191      评论:0      收藏:0      [点我收藏+]

1069 Nim游戏技术分享(51NOD基础)

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 
有N堆石子。A B两个人轮流拿,A先拿。每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N及每堆石子的数量,问最后谁能赢得比赛。
例如:3堆石子,每堆1颗。A拿1颗,B拿1颗,此时还剩1堆,所以A可以拿到最后1颗石子。
 
Input
第1行:一个数N,表示有N堆石子。(1 <= N <= 1000)
第2 - N + 1行:N堆石子的数量。(1 <= A[i] <= 10^9)
Output
如果A获胜输出A,如果B获胜输出B。
Input示例
3
1
1
1
Output示例
A
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std ; 

#define maxn 1005 
#define LL long long
LL num[maxn] ;  

int main(){
    int n ; 
    while(~scanf("%d" , &n)){
        for(int i= 0 ; i<n ; i++){
            scanf("%lld" , &num[i]) ; 
        }
        LL k = num[0] ; 
        for(int i=1 ; i<=n ; i++){
            k = k ^ num[i] ; // 异或运算 (按位模2加法) 
        }
        if(k ==0 ){//A先取 面对 奇异局势  
            printf("B\n") ; 
        }else printf("A\n") ; 
    }
    return 0 ; 
} 

 

1069 Nim游戏(51NOD基础)

原文:http://www.cnblogs.com/yi-ye-zhi-qiu/p/7631333.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!