[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 ...
随机推荐
- RHEL7 Ansible
[root@promote tt]# rpm -iUvh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch ...
- VC Dimension -衡量模型与样本的复杂度
(1)定义VC Dimension: dichotomies数量的上限是成长函数,成长函数的上限是边界函数: 所以VC Bound可以改写成: 下面我们定义VC Dimension: 对于某个备选函数 ...
- 深入理解ThreadLocal(一)
Android里,在不同的线程(假设子线程已经创建了Looper)中创建Handler时,并不需要显式指定Looper,系统能自动找到该线程自己的Looper.不同线程的Looper相互独立,之所以能 ...
- cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient
1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的 2.CCLayerGradient是用来显示 ...
- android 设置半透明
对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...
- cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)
ORCAD网表输出时 ERROR (ORCAP-5004):Error initializing COM property pages 之前遇到过这个问题,解决后忘了记录下来了.依稀记得问题答 ...
- c++ 关于换行符
windows: \r\n linux: \n mac: \r http://blog.chinaunix.net/uid-12706763-id-10830.html 不同的OS有不同的换行符: O ...
- IntelliJ IDEA 14 利用JRebel实现热部署 二
前言:今天下午和一个qq群里讨论JRebel时,忽然得到“自动部署”的奥秘--真有听君一席话,胜读十年书的感悟. 这是此群友的热部署博客:http://blog.csdn.net/martinkey/ ...
- 【HDOJ】【3709】Balanced Bumber
数位DP 题解:http://www.cnblogs.com/algorithms/archive/2012/09/02/2667637.html dfs的地方没太看懂……(也就那里是重点吧喂!)挖个 ...
- TesserOCR训练
1.CMD命令行进入 图片目录.运行: tesseract.exe testcode.tif testcode batch.nochop makebox 注意:上面的 testcode 名称 必须保持 ...