• 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. }

  • 相关阅读:
    类和对象(中)
    【MySQL】第01章_数据库概述
    自动化密码分析
    【Spring boot】静态资源、首页及Thymeleaf框架
    九、从0开始卷出一个新项目之瑞萨RZN2L生产烧录固件(jflash擦写读外挂flash)
    [C/C++]数据结构 栈和队列()
    力扣L12--- 125验证回文串(java版)-2024年3月15日
    高防DNS如何实现对DDoS攻击的流量清洗?
    CompletableFuture方法介绍及代码示例
    sql1(Leetcode1757可回收且低脂的产品)
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/126458578