https://www.hackerrank.com/contests/w8/challenges/counter-game

关键是二分查找等于或最接近的小于target的数。可以使用和mid+1判断来比较~ 另有一种做法如下:

http://stackoverflow.com/questions/6553970/find-the-first-element-in-an-array-that-is-greater-than-the-target

#include <iostream>
#include <vector>
using namespace std; uint64_t nextN(uint64_t N, vector<uint64_t> &vec) {
int left = 0;
int right = vec.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (vec[mid] == N) {
return N / 2;
} else if (vec[mid] > N) {
right = mid - 1;
} else {
if (N < vec[mid + 1]) {
return (N - vec[mid]);
}
left = mid + 1;
}
}
return (N - vec[(left + right) / 2]);
} int main() {
int T;
cin >> T;
vector<uint64_t> vec;
uint64_t p = 1;
for (int i = 0; i < 64; i++) {
vec.push_back(p);
p *= 2;
}
while (T--) {
uint64_t N;
cin >> N;
int move = 0;
while (N != 1) {
move++;
N = nextN(N, vec);
}
if (move % 2 == 0) {
cout << "Richard" << endl;
} else {
cout << "Louise" << endl;
}
}
return 0;
}

  

[hankerrank]Counter game的更多相关文章

  1. UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER

    UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?

  2. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  3. collections 模块(namedtuple, deque, Counter )

    基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型 ...

  4. Performance Monitor2:Peformance Counter

    Performance Counter 是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performance Co ...

  5. Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...

  6. 利用CSS计数函数counter()实现计数

    要实现li列表计数比较简单,直接设置list-style-type即可,但是要实现非li列表计数该怎么办呢,counter()可以轻松实现 body{counter-reset:section 0 s ...

  7. 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuq ...

  8. LeetCode Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...

  9. CacheManager COUNTER

    CacheClient.AddOrUpdate("COUNTER", 0, v => Convert.ToInt32(v) + 1);

随机推荐

  1. js设计模式(3)---桥接模式

    0.前言 看设计模式比较痛苦,一则是自己经验尚浅,不能体会到使用这些设计模式的益处:二则是不能很好把握使用这些设计模式的时机.所以这一部分看得断断续续,拖拖拉拉,为了了却这快心病,决定最近一口气看完几 ...

  2. 怎样按字母顺序(ABCDEF)动态添加控件

    考试系统中题库设计时,我想动态添加选项,顺序按ABCDEF这样,点击一下按钮添加A(radiobutton),再点击添加B,如此依次添加.本人比较菜,求达人写一个方法. private void bu ...

  3. jQuery基础选择器

    attr()方法的功能是设置或获取元素的某项属性值. attr("disabled", "true”)表示使该功能不可用. #id 选择器 $("#my_id& ...

  4. 51nod 1021 石头归并

    1021 石子归并 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合 ...

  5. JavaScript移除数组元素减少长度的方法

    JavaScript移除数组元素减少长度的方法,代码如下: //数组移除长度方法 var array=[];  array[0]="张三";  array[1]="李四& ...

  6. JS获取图片实际宽高及根据图片大小进行自适应

    JS获取图片实际宽高,以及根据图片大小进行自适应  <img src="http://xxx.jpg" id="imgs" onload="ad ...

  7. phpExcel导出excel的类,每步都有说明

    require_once WEB_PATH . '/lib/PHPExcel/PHPExcel.php'; require_once WEB_PATH . '/lib/PHPExcel/PHPExce ...

  8. EWOULDBLOCK = EAGAIN

    #define EAGAIN 11 /* Try again */ #define EINTR 4 /* Interrupted system call */ #define EWOULDBLOCK ...

  9. 【转】char*,const char*和string的相互转换

    1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...

  10. mapreduce 实现pagerank

    输入格式: A 1 B,C,D B 1 C,Dmap: B A 1/3 C A 1/3 D A 1/3 A |B,C,D C B 1/2 D B 1/2 B |C,Dreduce: B (1-0.85 ...