Careercup - Facebook面试题 - 5110993575215104
2014-04-30 16:12
原题:
The beauty of a number X is the number of 1s in the binary representation of X. Two players are plaing a game. There is number n written on a black board. The game is played as following: Each time a player chooses an integer number ( <= k) so that ^k is less than n and (n-^k) has as beautiful as n.
Next he removes n from blackboard and writes n-^k instead.
The player that can not continue the game (there is no such k that satisfies the constrains) looses the game. The First player starts the game and they play in turns alternatively. Knowing that both two players play optimally you have to specify the winner. Input: First line of the Input contains an integer T, the number of testcase. <= T <= . Then follow T lines, each containing an integer n. n more than and less than ^ +. Output For each testcase print "First Player" if first player can win the game and "Second Player" otherwise. Sample Input Sample Output Second Player
First Player
First Player
Second Player
Second Player
First Player
Second Player Explanation In the first example n is and first player can't change it so the winner is the second player. In the second example n is , so the first player subtracts (^) from n and the second player looses the game.
题目:两人玩一个数字游戏,开始在黑板上写一个正整数。两人轮流对该数执行一种操作:将这个数换成一个更小的正整数,但得保证变化之后的数在二进制表示里具有相同个数的‘1’。比如5变到3就满足条件,两者的二进制里都有两个‘1’。如果轮到某人时,无法找到更小且满足条件的数,则此人输掉游戏。给定一个初始的数字n,请判断在双方都最优策略的情况下,谁一定会赢?有必胜策略吗?
解法:从题目里看这就像个博弈的题,于是我在错误的思路下进行了第一次尝试,最终明白这题似乎不用博弈。用一个例子来观察这种游戏规则:“100110”,这个数可以变成“10110”、“100101”。可以发现都是在“10”的地方变成了“01”,也就是把其中一个“1”向右移动一位。那么游戏何时结束?到了“111”就结束了,因为没有多余的“0”可供移动了。起点和终点都清楚了,并且每次只能移动一位,所以游戏的回合数其实是固定的,根据这个回合数的奇偶就知道谁输谁赢了。算法可以在O(log(n))时间内完成。
代码:
// http://www.careercup.com/question?id=5110993575215104
#include <cstdio>
using namespace std; int main()
{
int nzero;
int sum;
int n; while (scanf("%d", &n) == && n > ) {
sum = ;
nzero = ;
while (n > ) {
if (n & ) {
sum += nzero;
} else {
++nzero;
}
n >>= ;
}
printf((sum & ) ? "First player wins.\n" : "Second player wins.\n");
} return ;
}
Careercup - Facebook面试题 - 5110993575215104的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- .NET DLL 保护措施应用实例(百度云分享工具)
最近做了个小工具,将保护措施思路全部应用到了此工具中. 点我下载 “百度分享工具”介绍 大家都知道,在百度云盘中分享文件,只能手工一条条地点击“分享”,如果想分享很多文件,操作会非常辛苦.“百度云分享 ...
- C#用反射判断一个类型是否是Nullable同时获取它的根类型(转自网络)
在我们的应用程序中我们使用类描述我们的业务对象,为我们产生一些报表之类的,那就依赖大量不同的对象,我们创建一个帮助方法来转换我们的业务对象,或是一个List的业务对象到DataTables. 由于数据 ...
- PHP static静态局部变量和静态全局变量总结
1.不会随着函数的调用和退出而发生变化,不过,尽管该变量还继续存在,但不能使用它.倘若再次调用定义它的函数时,它又可继续使用,而且保存了前次被调用后留下的值 2.静态局部变量只会初始化一次 3.静态属 ...
- 第一个Cocos2d-JS游戏
我们的编写的第一个Cocos2d-JS程序,命名为HelloJS,从该工程开始学习其它的内容.创建工程我们创建Cocos2d-JS工程可以通过Cocos2d-x提供的命令工具cocos实现,但这种方式 ...
- (转)Yale CAS + .net Client 实现 SSO(6)
第一部分:安装配置 Tomcat 第二部分:安装配置 CAS 第三部分:实现 ASP.NET WebForm Client 第四部分:实现基于数据库的身份验证 第五部分:扩展基于数据库的身份验证 第六 ...
- C++将类的构造函数、析构函数声明为private或者protected的用途
如果将构造函数.析构函数声明为private或者protected,表示不能从类的外部正常调用构造和析构函数了. 这种用法的通常使用的场景如下: 1.如果不想让外面的用户直接构造一个类A的对象,而希望 ...
- Low-poly低面建模(低像素多边形)
概念 继拟物化.扁平化(Flat Design).长阴影(Long Shadow)之后,低多边形(Low Poly)又火速掀起了最新设计风潮.这种设计风格在早期计算机建模和动效中就被广泛采用,在快要被 ...
- cocos2d-x 节点操作 -->J2ME
cocos2d-x 的节点操作涉及到以下几点 1. 节点之间的关系 2. 节点的添加操作 3. 节点的删除操作 4. ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- “~/Views/Home/Text.aspx”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。
在MVC架构中使用aspx页面,需要在Text.aspx中开头加入如下代码: <%@ Page Language="C#" Inherits="System.Web ...