• ZCMU--1431: Epic Game(C语言)


    Description

    Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).

    Your task is to determine by the given ab and n who wins the game.

    Input

    The only string contains space-separated integers ab and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.

    Output

    If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).

    Sample Input

    3  5  9
    1  1  100

    Sample Output

    0
    1
    解析:甲乙分别有个数a,b,石头有n个,两个人轮流来,甲每次取gcd(a,n),乙每次取gcd(b,n),如果某人某次取得时候,石头数量不够了,那个人输,我们开个while模拟每次,直到某个人不满足退出即可。
    注意点:求gcd(a(b),n)时候要先判断n>0,n=0得话进行这个函数就会除以0,发生错误。
    1. #include
    2. int gcd(int a,int b){//求最大公约数
    3. int t=1,max,min;
    4. if(a>=b) max=a,min=b;
    5. else max=b,min=a;
    6. while(t){
    7. t=max%min;
    8. max=min;
    9. min=t;
    10. }
    11. return max;
    12. }
    13. int main()
    14. {
    15. int a,b,n,s,f;
    16. while(~scanf("%d%d%d",&a,&b,&n)){
    17. s=1,f=1;//s=1表示Simon赢,0为输,f用来记录当前是谁进行游戏,f=1是Simon,2为Antisimon
    18. while(1){
    19. //当前是Simon操作
    20. if(f==1){
    21. if(n>0&&n>=gcd(a,n)) n-=gcd(a,n),f=2;//石子数量够
    22. else{
    23. s=1;//石子不够,Simon输
    24. break;
    25. }
    26. }else{
    27. //当前是Antisimon操作
    28. if(n>0&&n>=gcd(b,n)) n-=gcd(b,n),f=1;
    29. else{
    30. s=0;//石子不够,Antisimon输,也就是Simon赢
    31. break;
    32. }
    33. }
    34. }
    35. printf("%d\n",s);
    36. }
    37. return 0;
    38. }

  • 相关阅读:
    c# Enumerable<T>的GroupJoin方法和Join的用法和区别
    YonBuilder开发之后端函数
    持续集成部署 - 记一次构建Nuxt.js前端项目遇到的坑
    Sermant在异地多活场景下的实践
    仅作笔记用:Windows 11 通过 VBS 打开 IE 浏览器
    Seatunnel超高性能分布式数据集成平台使用体会
    分析2022年国内国际学校ib的分数
    LeetCode 203: Remove Linked List Elements 链表删除节点基本题
    Java代码基础算法练习-判断学生成绩等级-2024.06.28
    HCIP—BGP邻居关系建立实验
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/126458578