[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 ...
随机推荐
- nginx作反向代理,实现负载均衡
nginx作反向代理,实现负载均衡按正常的方法安装好 ngixn,方法可参考http://www.cnblogs.com/lin3615/p/4376224.html其中作了反向代理的服务器的配置如下 ...
- 安装ipvsadm时出现下面所示错误,MARK
[root@localhost ipvsadm-1.26]# makemake -C libipvsmake[1]: Entering directory `/usr/local/soft/ipvsa ...
- 动态创建MySQL数据库
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- SQL中char,varchar,nvarchar等的异同
比较这几个数据类型,总是忘记,可能比较细节的原因.先做个记号,回头完善.
- eclipse 安装插件不生效
操作: 下载了一个插件,插件中只有一个jar包,解压放到plugins目录,插件没生效. 解决方法: 把jar包直接扔到dropins目录下,并且删除eclipse\configuration\org ...
- EWOULDBLOCK = EAGAIN
#define EAGAIN 11 /* Try again */ #define EINTR 4 /* Interrupted system call */ #define EWOULDBLOCK ...
- mootools和jquery冲突的解决
mootools-jquery 今天在做EcStore前台的做效果时,由于Jquery的插件比较多,于是就使用了Jquery的插件,但是发现会引起Mootools的冲突. 于是猛找资料,终于找到了,现 ...
- VSC 使用Git进行版本控制
Visual Studio Code 使用Git进行版本控制 请确保你安装了最新的VS Code.http://code.visualstudio.com/ 请确保安装了最新版的Git.https:/ ...
- HTML+CSS入门
<strong>加粗</strong> <em>斜体</em> <p>段落</p> <span>设置单独样式< ...
- UVA 11149 Power of Matrix 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...