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

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

  2. Self Numbers[HDU1128]

    Self Numbers Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. (转)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 ...

  4. Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏

    Self Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22101   Accepted: 12429 De ...

  5. HDUoj-------(1128)Self Numbers

    Self Numbers Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

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

  7. OpenJudge/Poj 1316 Self Numbers

    1.链接地址: http://poj.org/problem?id=1316 http://bailian.openjudge.cn/practice/1316 2.题目: 总时间限制: 1000ms ...

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

  9. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

随机推荐

  1. Factorial Solved Problem code: FCTRL

    import sys #import psyco #很奇怪,这题用psyco就runtime error #psyco.full() def z(n): #这个应该是技巧的一种算法 r = 0 whi ...

  2. C语言初学 if-else语句判别在ASCII值中小于32的可控制符的类型

    #include<stdio.h> main() { char c; printf("输入一个符号\n"); c=getchar(); if(c<32) prin ...

  3. jQuery的Autocomplete插件的远程url取json数据的问题

    关于远程返回的json数据的展示,以前一样的代码,如果是本地写好的json串数据,插件显示就没有问题,一旦换成ulr方式读取一样的数据,插件就不能正常显示问题了. 今天偶然搜索资料找到一篇csdn上有 ...

  4. angularjs学习笔记—事件指令

    ngClick 适用标签:所有触发条件:单击 #html <div ng-controller="LearnCtrl"> <div ng-click=" ...

  5. iOS开发 自定义navigationleftItem 之后手势失效的问题

    @property (nonatomic, strong) UIViewController *currentShowVC; //设置代理 self.navigationController.inte ...

  6. ios打包ipa的四种实用方法(.app转.ipa)-备

    感谢大神分享这个博客 总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Arc ...

  7. git新手碰到的各种奇葩问题之一

    git  操作错误: <1>.情景描述:当在git commit --amend 更新上一次提交时,而此时提交日志会跳转到别人的日志中.,会出现错误:如下 弥补操作: 1.git fetc ...

  8. 关于Windows高DPI的一些简单总结(Window上一般默认是96 dpi 作为100% 的缩放比率)

    我们知道,关于高DPI的支持, Windows XP时代就开始有了, 那时关于高DPI的支持比较简单, 但是从Vista/Win7 到现在Win8 /Win8.1, Windows关于高DPI的支持已 ...

  9. Linux 硬连接和软连接的原理 (in使用)

    引子 目前,UNIX的文件系统有很多种实现,例如UFS(基于BSD的UNIX文件系统).ext3.ext4.ZFS和Reiserfs等等. 不论哪一种文件系统,总是需要存储数据.硬盘的最小存储单位是扇 ...

  10. inux xsel 拷贝复制命令行输出放在系统剪贴板上

    转载自:http://oldratlee.com/post/2012-12-23/command-output-to-clip 为什么要这么做?直接把命令的输出(比如 grep/awk/sed/fin ...