Modified Kaprekar Numbers
Link:
https://www.hackerrank.com/challenges/kaprekar-numbers
from __future__ import print_function
def find_kaprekar(num):
num_square = str(num ** 2)
if len(num_square) == 1:
if num == 1:
print (num, end = ' ')
return True
elif len(num_square) % 2 == 0:
d = len(num_square) / 2
if num == int(num_square[0:d]) + int(num_square[d:2*d]):
print (num, end = ' ')
return True
else:
d = len(num_square) // 2
if num == int(num_square[0:d]) + int(num_square[d:(2*d+1)]):
print (num, end = ' ')
return True
def main():
p = int(raw_input())
q = int(raw_input())
have_kaprekar_num = False
for i in xrange(p, q+1):
if find_kaprekar(i):
have_kaprekar_num = True
if have_kaprekar_num == False:
print("INVALID RANGE")
else:
print()
main()
//其他1
def is_kaprekar(n):
squared = str(n ** 2)
mid = len(squared) - len(str(n))
a = int(squared[mid:]) # 这种写法更简便
b = int(squared[:mid]) if len(squared) > 1 else 0
return a + b == n # 直接返回一个判断式子 p = int(raw_input())
q = int(raw_input()) kaprekars = [str(x) for x in xrange(p, q + 1) if is_kaprekar(x)]
print ' '.join(kaprekars) if kaprekars else 'INVALID RANGE' # join函数的使用
//其他2
import sys p = int(sys.stdin.readline())
q = int(sys.stdin.readline()) kaprekar = [1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4950, 5050, 7272, 7777, 9999, 17344, 22222, 77778, 82656, 95121, 99999] # 更省资源,因为数量不多,所以干脆一次性算出
ans = [str(k) for k in kaprekar if k>=p and k<=q]
if ans:
print ' '.join(ans)
else:
print 'INVALID RANGE'
Modified Kaprekar Numbers的更多相关文章
- 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- Self Numbers[HDU1128]
Self Numbers Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- (转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates
Setting Display Formats Using Microsoft Excel: Right-click on any desired cell and select Format Cel ...
- Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏
Self Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22101 Accepted: 12429 De ...
- HDUoj-------(1128)Self Numbers
Self Numbers Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- The Black Hole of Numbers (strtoint+inttostr+sort)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- OpenJudge/Poj 1316 Self Numbers
1.链接地址: http://poj.org/problem?id=1316 http://bailian.openjudge.cn/practice/1316 2.题目: 总时间限制: 1000ms ...
- PAT 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
随机推荐
- iOS学习之导航条NavigationControl的一些属性设置
/** * 配置公共的属性,该属性作用于所有的导航条界面; */ - (void)configureConmmonPropety { //1.设置导航条的颜色 self.navigationContr ...
- webpack ,react
一小时包教会 —— webpack 入门指南 http://***/Article/50764 ------------------ 轻松入门React和Webpack http://www.in ...
- 用linux mail命令发送邮件[Linux]
mail [-s 邮件标题] <<邮件地址1> [邮件地址2] [邮件地址3]> [ < 包含邮件内容的文件路径 ] <-- -f 发送邮件地址> [-F 显 ...
- Ubuntu下配置使用maven
下载界面: http://maven.apache.org/download.cgi 当前下载:apache-maven-3.2.5-bin.zip 解压到:/usr/lib/jvm/apache-m ...
- LeetCode_Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- TranslateAnimation详解
TranslateAnimation详解 Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, Tr ...
- Linux企业级项目实践之网络爬虫(4)——主程序流程
当我们设计好程序框架之后就要开始实现它了.第一步当然是要实现主程序的流程框架.之后我们逐渐填充每个流程的细节和其需要调用的模块. 主程序的流程如下: 1. 解析命令行参数,并根据参数跳转到相应的处理 ...
- VMware Workstation 精致汉化系列 使用方法
http://kuai.xunlei.com/d/QqGABAKChQBwMzxR983 迅雷快传 XP系统之家-温馨提示: VMware Workstation 精致汉化系列 使用方法:1.安装 ...
- Linux CentOS 7 YouCompleteMe相关配置。
CentOS 6.5上面弄了2天,以失败告终!!!当作练手了.在网上看到一篇CentOS7.0上安装YouCompleteMe插件的文章,就重新在虚拟机上安装了一个CentOS7,按那个文章执行了一下 ...
- python网络请求简洁之道--python requests简介
#requests中文文档:http://cn.python-requests.org/en/latest/#学习出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0 ...