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. List容器

    List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...

  2. C语言(按键获取与函数)

    举一个简单的例子,如果有按键,就输出相关按键.否则,输出“.”.每隔 100 毫秒输出一次.按 ESC 退出.注:ESC 的 ASCII 码是 27. #include <stdio.h> ...

  3. 解决android studio 创建新项目后假死

    概况:升级sdk编译api后,创建新的android项目后,在构建过程中假死:原有创建的项目 均运行正常:但是新建的项目只要build,电脑除了鼠标之外的,什么都动不了. 通过一系列的折腾,并重启了N ...

  4. Android学习笔记--Broadcast, BroadcastReceiver(广播)

    参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...

  5. AFNnetworking快速教程,官方入门教程译

    AFNnetworking快速教程,官方入门教程译 分类: IOS2013-12-15 20:29 12489人阅读 评论(5) 收藏 举报 afnetworkingjsonios入门教程快速教程 A ...

  6. Caffe : Layer Catalogue(2)

    TanH / Hyperbolic Tangent 类型(type):TanH CPU 实现: ./src/caffe/layers/tanh_layer.cpp CUDA.GPU实现: ./src/ ...

  7. 再转一篇gtest1.6安装

    http://www.cppblog.com/izualzhy/archive/2012/07/31/185772.html googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用 ...

  8. 【转】sublime text 2 中文乱码解决办法

    sublime text 2是一款非常优秀的跨平台文本及源代码编辑器,本人非常喜欢,但是不支持GB2312和GBK编码在某些时候比较麻烦.可以通过向sublime text 中添加编码类型转换包(比如 ...

  9. 抗忙,,建个MAVEN的私服仓库-NEXUS

    公司最近需求越来越上轨道,MAVEN的私服仓库-NEXUS构架起来哟.. 参考文档URL: http://www.linuxidc.com/Linux/2011-07/39578p3.htm http ...

  10. [转]ubuntu14.04安装好用的google拼音输入法

    原文网址:http://jingyan.baidu.com/article/219f4bf7d4a183de442d38f2.html 装了ubuntu14.04后感觉自带的拼音输入法不好用的有没有, ...