[hackerrank]Closest Number
https://www.hackerrank.com/contests/w5/challenges/closest-number
简单题。
#include <iostream>
#include <cmath>
using namespace std; int main() {
int T;
cin >> T;
while (T--) {
int a, b, x;
cin >> a >> b >> x;
if (b < 0 && a == 1) {
if (x - 1 < 1) {
cout << x << endl;
} else {
cout << 0 << endl;
}
} else if (b < 0) {
cout << 0 << endl;
} else {
int req = pow(a, b);
int k = req / x;
if ((k + 1) * x - req < req - k * x) {
cout << (k + 1) * x << endl;
} else {
cout << k * x << endl;
}
}
}
return 0;
}
[hackerrank]Closest Number的更多相关文章
- Closest Number in Sorted Array
Given a target number and an integer array A sorted in ascending order, find the index i in A such t ...
- K Closest Numbers In Sorted Array
Given a target number, a non-negative integer k and an integer array A sorted in ascending order, fi ...
- NSString 转换 float 的精度问题, 换double类型可以解决
@"0.01" 转换成float时, 经常会变成 0.009999799 这种形式, 因为float类型无法精准保存, 系统会选一个接近的值来代替. 而double类型则可以有更 ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- Float精度丢失
BigDecimal _0_1 = new BigDecimal(0.1); BigDecimal x = _0_1; for(int i = 1; i <= 10; i ++) { Syste ...
- Outlier Detection
1)正态分布数据,飘出95%的可能是异常值.变量var正态标准化,|var|<=1.96的可能是异常值,further chk needed!large sample better. 对于偏态分 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- 【HackerRank】Closest Numbers
Sorting is often useful as the first step in many different tasks. The most common task is to make f ...
随机推荐
- js中substring和substr的用法比较
推荐使用substring 方法 stringObject.substring(start,stop) stringObject.substr(start,length) 定义和用法 提取 ...
- php + apache + mysql环境搭建
别人写的很好,若是不改变php默认访问路径的话,能够成功搭建是没问题的 http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html
- How to check Windows 7 OS is permanently activated?[Windows 7]
Press Windows + R, then you can enter : slmgr.vbs -xpr
- 在Windows 上安装SQL Server的一些注意事项
基本来说安装SQL Server 单节点数据库并不是很困难的事情,大多可以通过Next来安装完成.其中要注意以下几点 安装.net3.5 可以参考本Blog的一些安装须知. Windows Serve ...
- 单例模式(Singleton)的6种实现
1.1.1 摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就 ...
- 【F#】 WebSharper框架
WebSharper,它是一个基于F#构建的Web开发平台,使用F#构造从前到后的一整套内容.其中利用到F#中许多高级的开发特性,并可以将F#代码直接转化JavaScript,这样服务器端和客户端的通 ...
- 关于sql语句in的使用注意规则
想必大家都用过sql中的in语句吧,我这里描述下我遇到的一种in语句问题,并总结一些给大家分享下,不对的地方还希望大虾指点下. 问题描述:IN子查询时,子查询中字段在表中不存在时语句却不报错 平常工作 ...
- 标签跳转break和continue
标签是后面跟有冒号的标识符,例如 label1: 在java中,标签起作用的唯一的地方刚好是在迭代语句之前. “刚好之前”的意思表明,在标签和迭代之间置入热和语句都不好. 而在迭代之前设置标签的唯一 ...
- List<T>中Exists 和Contains的区别
.net编码中,使用泛型List<>时,经常遇到这样的需求:新来一个Model对象,如果已有的List中没有这条数据,则把新对象Add到List中,否则不处理 判断已有的List中是否包含 ...
- 视频学习_css基础学习
块状元素 block element 容器元素 设置高宽 width height 可以容纳 文本 内脸 和其他块状 霸道 独占一行 特例:form 只容纳 块状元素 常见元素 http:// ...