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 ...
随机推荐
- python construct文档
The Basics Fields Fields are the most fundamental unit of construction: they parse (read data from t ...
- Activiti工作流学习-----基于5.19.0版本(2)
二.activiti.cfg.xml的其他bean节点配置 2.1 新特性:Job Executor和Async Executor 从5.17.0版本的activiti开始提供作业执行者(Job Ex ...
- 性能测试 - some
1.日常业务中测试过的最大并发数,其QPS为多少? 答: 从服务端开发处得知线上某台机器单接口访问量为63万 因该接口为视频类访问接口,大多数接口集中于晚间时段.可计算QPS = 63万/8/3600 ...
- Rufus-Create bootable USB drives the easy way
Rufus Create bootable USB drives the easy way Rufus is a utility that helps format and create bootab ...
- cf D. Xenia and Hamming
http://codeforces.com/contest/357/problem/D 题意:给你两个数n和m,表示两个字符串的循环次数,然后给出两个字符串,求出其相同位置字符不同的个数. 先求出两个 ...
- 自制单片机之四……LCD1602的驱动
LCD1602已很普遍了,具体介绍我就不多说了,市面上字符液晶绝大多数是基于HD44780液晶芯片的,控制原理是完全相同的,因此HD44780写的控制程序可以很方便地应用于市面上大部分的字符型液晶.字 ...
- Qt浅谈之二十App自动重启及关闭子窗口
一.简介 最近因项目需求,Qt程序一旦检测到错误,要重新启动,自己是每次关闭主窗口的所有子窗口但有些模态框会出现问题,因此从网上总结了一些知识点,以备以后的应用. 二.详解 1.Qt结构 int ma ...
- POJ1061青蛙的约会(扩展欧几里德算法)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 102239 Accepted: 19781 Descript ...
- Java单元测试工具:JUnit4(一)(二)(三)(四)
Java单元测试工具:JUnit4(一)--概述及简单例子 Java单元测试工具:JUnit4(二)--JUnit使用详解 Java单元测试工具:JUnit4(三)--JUnit详解之运行流程及常用注 ...
- git hub 资料汇总
tobecrazy Selenium automation test framework https://github.com/tobecrazy/Demo Smartphone Test F ...