Find n'th Digit of a Number -- 8 kyu
来源:互联网 发布:淘宝八十字评论 编辑:程序博客网 时间:2024/06/08 01:02
原题
http://www.codewars.com/kata/find-nth-digit-of-a-number/train/cpp
题目
The function findDigit takes two numbers as input, num and nth. It outputs the nth digit of num (counting from right to left).
- If num is negative, ignore its sign and treat it as a positive value.
- If nth is not positive, return -1.
- Keep in mind that 42 = 00042. This means that findDigit(42, 5) would return 0.
Example:
findDigit(5673, 4)
returns 5
findDigit(129, 2)
returns 2
findDigit(-2825, 3)
returns 8
findDigit(-456, 4)
returns 0
findDigit(0, 20)
returns 0
findDigit(65, 0)
returns -1
findDigit(24, -8)
分析
取出第n位上的数值的绝对值,可以将数据除以10(n-1)次方后和10取余得到该值
代码
#include <cmath>int findDigit(int num, int nth){ return nth <=0?-1:int(abs(num)/pow(10,nth-1))%10;}
阅读全文
0 0
- Find n'th Digit of a Number -- 8 kyu
- Find n‘th number in a number system with only 3 and 4
- Parts of a list -- 7 kyu
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Find all prime factors of a number
- org.springframework.beans.factory.BeanCreationException
- List 接口常用子类及其特点
- Android so库Hook技术
- <31>——Next Permutation
- The Rotation Game UVA
- Find n'th Digit of a Number -- 8 kyu
- 搜索------(一)
- 在一个字符串中,找出最长回文子串
- Kinect sdk 2.0 + Opencv 获取深度图像并保存
- java线程死锁
- ImportError: No module named arcpy
- windows窗体程序的创建
- 用python做有趣的事儿——模拟登陆知乎
- Java多线程--让主线程等待子线程执行完毕