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. documentElement vs body区别

    documentElement.scrollTop------>0因为,他包含head, body body.scrollTop------------------>才是正确的 scrol ...

  2. 超实用--删除MYSQL中指定的数据的全部表

    作过的人都知道,重复测试数据库的苦恼. 用法:# Usage: ./script user password dbnane mysql.nixcraft.in ~~~~~~~~~~~~~ #!/bin ...

  3. Unity欢迎窗口的信息

    1.Video Tutorials提供Unity相关的教程,包括用户手册.组件手册以及脚本手册等内容.2.Unity Basics提供Unity的基础知识,例如操作界面.工作流程.发布设置等内容.3. ...

  4. QDialog 模态对话框与事件循环(exec其实就是调用了show和eventLoop.exec)

    起源 qtcn中文论坛中有网友问到: 假设程序正常运行时,只有一个简单的窗体A,此时只有一个GUI主线程,在这个主线程中有一个事件循环处理窗体上的事件.当此程序运行到某阶段时,弹出一个模态窗体B(书上 ...

  5. VS2015 C#6.0 中的那些新特性(转载)

    自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...

  6. WCF的执行过程

    既然是实现互通信.那么肯定会有概念意义上的服务端Server  和概念意义上的客户端 Client,在这里,我所说明的都是概念意义上的,单独强调此,是因为,基于WCF的通信没有物理上的划分,但是概念上 ...

  7. 【数位DP】 HDU 4734 F(x)

    原题直通车:HDU 4734 F(x) 题意:F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1, 求0.....B中F[x]<=F[A ...

  8. 减少leftJoin的使用 (转)

    作为开发,你是否经常碰到下面需要转换用户ID成用户名称的情况: 可惜你的这些业务表出于最少冗余设计要求,只有UserId,而没有UserName,这时你不得不破坏你一个类封装一个表的美好想法, 在你的 ...

  9. IDF实验室解题学习笔记1

    1.图片里的英文 图片可以有很多种打开方式,破解该题,需将图片下载下来. 对于图片,我们可以使用图片编辑软件,进行各种调明暗,变色调等操作. 我们还可以使用2进制或者16进制的文件打开方式打开.该图使 ...

  10. 程序员必备基础知识:通信协议——Http、TCP、UDP

    CP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系: TCP/IP是个协议组,可分为四个层次: ...