[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 ...
随机推荐
- 万能的SQLHelper帮助类
/// <summary> /// 数据库帮助类 /// </summary> public class SQLHelper { private static string c ...
- php中json_encode中文编码问题分析
众所周知使用json_encode可以方便快捷地将对象进行json编码,但是如果对象的属性中存在着中文,问题也就随之而来了.json_encode会将中文转换为unicode编码例如:'胥'经过jso ...
- Crusher Django 学习笔记1 hello world
http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial1-hello-world.html 随便学习一下goagent ...
- Odoo 库存管理-库存移动(Stock Move)新玩法
库存移动(Stock Move)新玩法 Odoo的库存移动不仅仅是存货在两个“存货地点”之间的移动的基本概念了,他们可以被“串联”在一起,可以用来生成或改变其对应的拣货单 (Picking).链式库存 ...
- How to generate number Sequence[AX 2012]
Suppose we want create number sequence for Test field on form in General ledger module Consideratio ...
- SQL Server 数据库身份认证以及包含数据库
首先分为SQL Server 认证与Windows 身份认证. SQL Server 认证可以运行以下语句来查询 select * from sys.sql_logins 管理员可以直接修改密码,但无 ...
- 用Redis bitmap统计活跃用户、留存
Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...
- 非常实用的10个PHP高级应用技巧
PHP 独特的语法混合了 C.Java.Perl 以及 PHP 自创新的语法.它可以比 CGI或者Perl更快速的执行动态网页.用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML ...
- CentOS 6.5系统上安装MySQL数据库
1.查看系统是否安装了MySQL 使用命令: #rpm -qa | grep mysql 2.卸载已安装的MySQL 卸载mysql命令如下: #rpm ...
- Android应用市场提交入口
应用市场是整个移动生态系统的核心,然而对于中国用户来说,Google Play应用商店却因为种种原因,在中国一直无法长期稳定的运作,又加上Android系统的开源特性,从而在中国造就出大量的第三方应用 ...