Codewars解题Playing with digits
来源:互联网 发布:淘宝短网址生成 编辑:程序博客网 时间:2024/06/03 04:59
题目
Some numbers have funny properties. For example:
89 –> 8¹ + 9² = 89 * 1
695 –> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 –> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51 Given a
positive integer n written as abcd… (a, b, c, d… being digits) and
a positive integer p we want to find a positive integer k, if it
exists, such as the sum of the digits of n taken to the successive
powers of p is equal to k * n. In other words:Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^
(p+3) + …) = n * k If it is the case we will return k, if not return
-1.Note: n, p will always be given as strictly positive integers.
digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1 digPow(92,
1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ =
2360688 = 46288 * 51
谷歌翻译:
给定一个正整数n写成abcd …(a,b,c,d
…是数字)和一个正整数p,我们想要找到一个正整数k,如果存在的话,比如数字的总和对于p的连续幂的n等于k * n。换一种说法:是否有整数k,例如:(a ^ p + b ^(p + 1)+ c ^(p + 2)+ d ^(p + 3)+ …)= n * k
如果是这种情况,我们将返回k,如果不返回-1。注:n,p将始终作为严格正整数给出。
我的理解
参数:给一个正整数n=abcde; 与p
abcde带表各个位上的数字
如果a^p+b^(p+1)+c^(p+2)+d^(p+3)+e^(p+4)暂且把值写为sum
如果sum%n==0;就返回sum/0;
否则返回-1;
以下为我的代码
public class DigPow { public static long digPow(int n, int p) { // your code //定义一个标记 long tem = -1; //计算总和 int sum = 0 ; //将初始值记录 int ss = n; //计算最大次幂 int p1 = p+(n+"").length()-1; //开始循环,每次循环n都除以10,直到除不尽 while(n!=0){ //得到个位上的数 int m = n%10; //记录该数 int m1 = m; for(int i = 0 ;i<p1-1;i++){ //循环算出该位上应得的值 m*=m1; } //将最大次幂递减 p1--; //将算出来的值累加 sum+=m; //去掉最后一位数 n = n/10; } //判断是否符合条件 if(sum%ss==0){ //修改标记 tem=sum/ss; } return tem; }}
该方法以通过测验
以下为评分最高的解决方案
public class DigPow { public static long digPow(int n, int p) { String intString = String.valueOf(n); long sum = 0; for (int i = 0; i < intString.length(); ++i, ++p) sum += Math.pow(Character.getNumericValue(intString.charAt(i)), p); return (sum % n == 0) ? sum / n : -1; }}
很高兴可以看得懂
目前的水平在6k徘徊,有时连7k的题都解决不了
- Codewars解题Playing with digits
- 算法练习3:Playing with digits
- CodeWars:Most digits
- [Haskell] CodeWars|Sum of Digits
- [leetcode] 357. Count Numbers with Unique Digits 解题报告
- leetcode 357. Count Numbers with Unique Digits 解题报告
- LeetCode---Count Numbers with Unique Digits解题分析
- LeetCode 357. Count Numbers with Unique Digits 解题报告
- LeetCode解题报告 357. Count Numbers with Unique Digits [medium]
- leecode 解题总结:357. Count Numbers with Unique Digits
- [Leetcode] 357. Count Numbers with Unique Digits 解题报告
- 357. Count Numbers with Unique Digits解题报告
- Playing with DataGrid Control
- Playing with ptrace
- Playing with the Stack
- Playing with the Stack
- Playing with the stack
- playing with ptrace
- mysql启动
- C#获取系统文件、文件夹和磁盘驱动器图标
- Android界面编程之实现改变图片透明度并提供细节查看
- cocos2dx DrawNode和ClippingRectangleNode 使用详解
- 利用Sobel算子,求梯度图像
- Codewars解题Playing with digits
- GitHub Branch / SSH clone URL / Clone in Desktop / Download ZIP
- jzoj P2249 蒸发学水
- 访问wamp的localhost出现“没有权限访问”解决方法
- ubuntu14.04小技巧
- 直方图均衡化(Histogram equalization)与直方图规定化
- 小程序app.json文件浅析
- 【AndroidStudio】分析性能工具(AndroidDeviceMonitor)
- 415错误及解决方法