URAL 1180. Stone Game (博弈 + 规律)[通俗易懂]

URAL 1180. Stone Game (博弈 + 规律)

大家好,又见面了,我是全栈君。

1180. Stone Game

Time limit: 1.0 second

Memory limit: 64 MB

Two Nikifors play a funny game. There is a heap of
N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only condition that this number is a nonnegative integer power of 2 (e.g. 1, 2, 4, 8 etc.). Nikifor who takes the last stone wins.You are to write a program that determines winner assuming each Nikifor does its best.

Input

An input contains the only positive integer number
N (condition
N ≤ 10
250 holds).

Output

The first line should contain 1 in the case the first Nikifor wins and 2 in case the second one does. If the first Nikifor wins the second line should contain the minimal number of stones he should take at the first move in order to guarantee his victory.

Sample

input output
8
1
2

Problem Author: Dmitry Filimonenkov


Problem Source: Third USU personal programming contest, Ekaterinburg, Russia, February 16, 2002

解析:找规律。

考虑例如以下样例:
剩余石子的数量    first Nikifor
1                              win
2                              win
—- 依次选择1,2. 而且1 和 2是能够选择的。

3                              lose
—- 由于你选1或2的时候,另外一个人总能一次把剩下的取完。

4, 5                          win

—- 依次选择1,2,可以获胜。

6                              lose

7, 8                          win

…. 从上面的规律我们能够看出当 N mod 3 == 0 我们能确认first Nikifor一定能失败;否则的话。我们能选择的最小石子的数量 = N mod 3,此时first Nikifor获胜。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(){
    int ans = 0;
    char c;
    while(scanf("%c", &c) && c != '\n') ans += c - '0';     //求个位数字之和以推断数字能否被3整除,数太大。直接存不下!

if(ans % 3 == 0) puts("2"); else printf("%d\n%d", 1, ans % 3); return 0;}

.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116181.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • C语言根据经纬度计算距离[通俗易懂]

    C语言根据经纬度计算距离[通俗易懂]#include#defineEARTH_RADIUS6378.137//地球半径#definePI3.14159265358979323846//Ô²ÖÜÂÊ//½Ç¶Èת»¯Îª»¡¶Èstaticdoublerad(doubled){  returnd*PI/180.0;}//µ±Äϱ±

    2022年9月23日
  • 新手必备webstorm安装教程

    新手必备webstorm安装教程接下来我们看一下webstorm的安装1.首先下载webstrom安装包这个安装包在网上很好找点击.exe文件激活成功教程为长期有效的webstrom/IDEAlicenseserver20181.复制下方activationcode,直接粘贴在webstorm的activationcode栏上,切记先不要点ok,切记先不要点ok,切记先不要点ok,等第二步执行完…

    2022年6月15日
  • Java 逻辑运算符(&、|、^、!、&&、||)

    Java 逻辑运算符(&、|、^、!、&&、||)一、逻辑运算符短路逻辑运算符与逻辑运算符的区别是:逻辑运算符:先走一遍,在判断短路逻辑运算符:一但符合条件,后面的判断不走二、逻辑运算符逻辑运算符:&、|、^、!案例代码publicclasstest{publicstaticvoidmain(String[]args){//定义变量inti=10;intj=20;intk=30;//&:有false

    2022年10月29日
  • group by 与 where, having以及顺序

    group by 与 where, having以及顺序1.GROUPBY子句必须出现在WHERE子句之后,ORDERBY子句之前.HAVING语句必须在ORDERBY子句之后。(where先执行,再groupby分组;groupby先分组,having在执行。)2.除聚集计算语句外,SELECT语句中的每个列都必须在GROUPBY子句中给出。count()为聚集函数,vend_id在后面groupby中有,所以select后面有。sel…

    2022年5月25日
  • 优秀的程序员该如何更上一层楼

    优秀的程序员该如何更上一层楼

    2022年3月5日
  • 数据的四大特性_质量具有的四种特性

    数据的四大特性_质量具有的四种特性事务具有四个特征:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)和持续性(Durability)。这四个特性简称为ACID特性。1

    2022年8月1日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号