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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. NSString 转换 float 的精度问题, 换double类型可以解决

    @"0.01" 转换成float时, 经常会变成  0.009999799 这种形式, 因为float类型无法精准保存, 系统会选一个接近的值来代替. 而double类型则可以有更 ...

  4. 九章lintcode作业题

    1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...

  5. Float精度丢失

    BigDecimal _0_1 = new BigDecimal(0.1); BigDecimal x = _0_1; for(int i = 1; i <= 10; i ++) { Syste ...

  6. Outlier Detection

    1)正态分布数据,飘出95%的可能是异常值.变量var正态标准化,|var|<=1.96的可能是异常值,further chk needed!large sample better. 对于偏态分 ...

  7. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  8. 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 ...

  9. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

随机推荐

  1. mongodb学习之路1

    第一节 MongoDB介绍及下载与安装 引言 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似 json的b ...

  2. Android设置背景图像重复【整理自网络】

    1:在res/drawable文件下新建一个xml文件tool_bar_bg_repeat.xml比如为: <?xml version="1.0" encoding=&quo ...

  3. js各类共用方法

    function GetParameterValueByName(parametername) { var reg = new RegExp("(^|&)" + param ...

  4. 你的数据根本不够大,别老扯什么Hadoop了

    本文原名"Don't use Hadoop when your data isn't that big ",出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯 ...

  5. Struts 2简单配置分析

    要配置Struts 2,首先先要有Struts 2的Jar包,可以去Struts的官网下载(http://struts.apache.org/),这里有3个GA版本可以选择下载,我选择的是最新的2.2 ...

  6. STL算法

    STL算法部分主要由头文 件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorit ...

  7. 转字符驱动实例gpio

    概述: 字符设备驱动程序: 是按照字符设备要求完成的由操作系统调用的代码. 重点理解以下内容:  1. 驱动是写给操作系统的代码,它不是直接给用户层程序调用的,而是给系统调用的  2. 所以驱动要向系 ...

  8. z470 装黑苹果 10.92

    1.分两个区,一个是mac安装区,一个是镜像拷贝区. 2.把镜像压进去. 3.安装好系统. 4.把镜像区的 extent拷贝到安装好的系统盘里去. 5.安装驱动,网盘里有.还有系统也在网盘里. 6.声 ...

  9. mysql中常用的公式及个人演示

    学生,院系表 -- phpMyAdmin SQL Dump-- version 4.1.9-- http://www.phpmyadmin.net---- Host: localhost-- Gene ...

  10. python学习小结4:类

    虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 类和对象是面向对象编程的两个主要方面.类:创建一个新类型,而对象是这个类的实例,类使用class关键字创建.类的域和方法被列在一个 ...