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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Eureka集群原理

    Eureka集群原理问题:微服务RPC远程服务调用最核心的是什么?高可用,试想你的注册中心只有一个onlyone,它出故障了那就呵呵o( ̄︶ ̄)o了,会导致整个微服务环境不可用。解决办法:搭建Eureka注册中心集群,实现负载均衡+故障容错Eureka集群的原理:互相注册,相互守望。一个Eureka集群包含7001和7002等许多服务,在这个集群中,7001指向其他所有服务…

    2022年5月28日
    38
  • DTU连接自建MQTT服务器

    DTU连接自建MQTT服务器DTU连接自建MQTT服务器DTU串口助手连接电脑,图片中485端口被变送器占用,飞线用来测试配置参数如图:重启DTU网络连接正常。启动java服务端启动连接成功发送透传测试数据查看串口助手:收到透传数据DTU发送透传数据查看Java服务端收到透传数据…

    2022年5月28日
    62
  • Fetch failed: unable to access http://dev.123.cn:123/git/123/213.git/

    Fetch failed: unable to access http://dev.123.cn:123/git/123/213.git/

    2020年11月9日
    208
  • Git提交代码步骤

    Git提交代码步骤目录1.Git提交代码步骤1.1第1步:查看当前状态:gitstatus1.2第2步:提交代码到本地git缓存区:gitadd1.3第3步:推送代码到本地git库:gitcommit1.4第4步:合并远程与本地代码:gitpull1.5第5步:提交本地代码到远程仓库:gitpush1.Git提交代码步骤1.1第1步:查看当前状态:gitstatus提交代码第1步:gitstatus查看当前状态当你忘记修改了哪些文件的时候可以使用git..

    2022年6月23日
    42
  • android studio与eclipse_androidstudio源码网

    android studio与eclipse_androidstudio源码网 以前公司的老项目,是使用eclipse进行开发的,虽然androidstudio出来了很久,但为了避免迁移会有一些问题,一直忍着没改,但最近谷歌公司上架有要求,要求android的项目要用android8.0来编译,然后就发现eclipse+ADT已经不支持jdk1.8还有android8.0,运行就会有问题,有类似unsported52.0,还有各种莫名其妙的错误,比如无法识别27,…

    2022年10月4日
    0
  • idea插件开发指南_idea get set插件

    idea插件开发指南_idea get set插件gitee地址:https://gitee.com/jyq_18792721831/studyplugin.gitidea插件开发入门idea插件开发–配置idea插件开发–服务-翻译插件idea插件开发–组件–编程久坐提醒介绍组件应用程序启动项目打开模块打开应用程序/项目关闭监听程序代码中注册监听器声明注册监听器项目级的监听器声明注册的其他配置自定义监听器接口消息系统设计主题消息总线连接广播嵌套消息组件定义应用程序级别项目级别监听器定义Java计时器实例需求分解项目创建配置界面存储服务配置和

    2022年10月1日
    1

发表回复

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

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