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 ...
随机推荐
- Struts2学习笔记--Struts例子及开发流程
参考资料:http://blog.csdn.net/hntyzgn2010/article/details/5547753 http://chenlh.iteye.com/blog/464341 入门 ...
- AFNetworking使用总结
AFNetworking使用总结 关于AFNetworking使用总结 以及一些错误的解决办法. AD:WOT2015 互联网运维与开发者大会 热销抢票 AFNetworking使用总结 分享类型 ...
- Mac实用技巧
1. 程序员Mac新装机必备 Mac很玄这个大家都承认,但是鄙人觉得程序员用Mac才能真正发挥它的功效.下面就说说我的Mac使用: 基本编程软件:xcode,这个东西不仅仅是对mac的界面程序开发有用 ...
- Flume配置
http://my.oschina.net/leejun2005/blog/288136#OSC_h1_1 http://blog.cloudera.com/blog/2012/09/analyzin ...
- MongoDB再实测
不用安装,直接解压.. 这些都不是最主要的,,,倒是TOMCAT和NGINX还需要更深入的了解... http://jingyan.baidu.com/article/acf728fd3d398bf8 ...
- 为什么memset的第二个参数不把int替换成char
memset是一个经常被用来初始化数组的函数,其定义如下: 1 void * memset ( void * ptr, int value, size_t num ); 它的效果大致是把以ptr为起始 ...
- About Undefined Behavior[译文]
原文:blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 人们偶尔会问为什么LLVM的汇编代码有时会在优化器打开时产生SIGT ...
- dictionary (key-value) (map容器)
#dictionary可以保存不同类型的值 menu = {'fish0':14.50} list = [] menu['fish1'] = 10.1 # Adding new key-value p ...
- Web开发之RSET API
REST介绍 如果要说什么是REST的话,那最好先从Web(万维网)说起. 什么是Web呢?读者可以查看维基百科的词条(http://zh.wikipedia.org/zh-cn/Web),具体的我就 ...
- 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道 使用python/casperjs编写终极爬虫-客户端App的抓取