codewars习题(1),python3,playing with digits,6级,算法
2018-01-20 13:22
435 查看
描述:
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.
算法思想:给定两个参数,n,p,将n中每一个数字提取出来,并加入一个列表里面,再求出总和计算k值
但最终k值会是浮点数,于是加一个限定,不是整数则返回-1,是整数则返回k值
函数代码;def dig_pow(n, p):
a = []
b = n
c = p
s = 0
d = 1
while d > 0:#d判断执行次数
d = b // 10
b = b % 10
a.append(b)
b = d
i = -1
q = len(a)
while q > 0:
h = a [q - 1]
s = s + (h ** c)
c +=1
q -=1
l = s / n
v = l - int(l)#v判断浮点数值是不是整数
if s < n or v > 0:
return -1
else:
return l
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.
算法思想:给定两个参数,n,p,将n中每一个数字提取出来,并加入一个列表里面,再求出总和计算k值
但最终k值会是浮点数,于是加一个限定,不是整数则返回-1,是整数则返回k值
函数代码;def dig_pow(n, p):
a = []
b = n
c = p
s = 0
d = 1
while d > 0:#d判断执行次数
d = b // 10
b = b % 10
a.append(b)
b = d
i = -1
q = len(a)
while q > 0:
h = a [q - 1]
s = s + (h ** c)
c +=1
q -=1
l = s / n
v = l - int(l)#v判断浮点数值是不是整数
if s < n or v > 0:
return -1
else:
return l
相关文章推荐
- codewars习题(2) python3 算法 Delete occurrences of an element if it occurs more than n times 6级
- codewars习题3 python3 Reverse words 算法 6级
- 算法练习3:Playing with digits
- python算法习题(四): 添加运算符
- Rapid GUI Programming with Python and Qt第二章习题答案加注释
- python写算法题:leetcode: 30. Substring with Concatenation of All Words
- coursera 吴恩达 -- 第一课 神经网络和深度学习 :第二周课后习题 Python Basics with numpy (optional)
- (算法分析Week12)Count Numbers with Unique Digits[Medium]
- 《编程珠玑》习题练习In Python——第二章 啊哈!算法
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法
- python codewars每日一刷-记第一个自力更生的算法问题
- Playing Atari with Deep Reinforcement Learning算法解读
- python算法习题(一): 排列组合
- Tartu大学计算机科学系计算神经学小组的学者在robohub网站发表文章,阐述了他们对DeepMind人工智能算法的复现。Playing Atari with Deep Reinforcement.
- Python习题:Python 判断是不是等差数列,要求算法时间复杂度为O(NlogN)
- Python程序设计基础 数据结构与算法习题
- python算法习题(三): 链表反置
- 【LEETCODE】357- Count Numbers with Unique Digits [Python]
- Leedcode 算法习题 第七周
- Python学习日志 - 6.字典(习题解答)