Python练习题 032:Project Euler 004:最大的回文积
本题来自 Project Euler 第4题:https://projecteuler.net/problem=4
# Project Euler: Problem 4: Largest palindrome product
# A palindromic number reads the same both ways.
# The largest palindrome made from the product
# of two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
# Answer: 906609 lst = []
for i in range(100, 999):
for j in range(100, 999):
if str(i*j) == str(i*j)[::-1]:
lst.append(i*j)
print(max(lst))
所谓的“回文积”(Palindrome Product),指的是某个数字,正着念、倒着念都一样,而且这个数字是另外两个数字之乘积。比如:9009 = 91 × 99,9009就是个回文数,而且是91和99的乘积,暂且就称之为回文积。本题就是求解两个三位数之积中的最大回文数。
因为此前在 Python练习题 025:判断回文数 里已做过类似的题目,所以也算是轻车熟路了。思路也很简单:用两个 for 循环轮番找出所有“两个三位数之积中的所有回文数”,用 max() 找出最大的那个即可。而判断回文数,最简单的就是用 str() 把数字转化为字符,如果 str == str[::-1],就判断为是回文,因为 str[::-1] 的作用就是把 str 倒着写。
Python练习题 032:Project Euler 004:最大的回文积的更多相关文章
- 【Python】【demo实验32】【回文数的确认】
原题: 我的代码: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- #判断一个数字是否为回文数 即 12345654321 x = ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 033:Project Euler 005:最小公倍数
本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
随机推荐
- 23种设计模式 - 领域问题(Interpreter)
其他设计模式 23种设计模式(C++) 每一种都有对应理解的相关代码示例 → Git原码 ⌨ 领域问题 Interpreter 动机(Motivation) 在软件构建过程中,如果某一特定领域的问题比 ...
- ACwing 你能回答这些问题吗(线段树求最大连续字段和)
给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“1 x y”,查询区间 [x,y] 中的最大连续子段和,即 maxx≤l≤r≤ymaxx≤l≤r≤y{∑ri=lA[i]∑i=l ...
- MySQL索引凭什么能让查询效率提高这么多?
点赞再看,养成习惯,微信搜一搜[三太子敖丙]关注这个喜欢写情怀的程序员. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系 ...
- 【Gin-API系列】守护进程和平滑重启(八)
生产环境的API服务我们都会部署在Linux服务器上,为了不受终端状态的影响,启动服务的时候会让服务在后台运行.那么如何让服务在后台运行呢,目前有2种常见的方法. 1.nohub 运行 表示忽略SIG ...
- cdq分治——bzoj2683简单题
https://www.lydsy.com/JudgeOnline/problem.php?id=2683 知识点:1.以操作的顺序进行分治 2.cdq分治维护矩阵 3.计算比mid小的给比mid大 ...
- 执行Python程序出现“SyntaxError: Non-UTF-8 code starting with '\xb6'...”错误怎么办?
如果文件中有中文,直接执行python xx.py会出现以下错误: SyntaxError: Non-UTF- code starting with '\xb6' in file XX.py on l ...
- Brup sute
暴力破解
- Java实现随机生成由字母、数字组合的N位随机数
通过Math.random函数生成所需的字符所有序列,通过判断当前字符是否属于大小写.数字,符合者加入数组内,直到数组存储N位为止,最后把当前数组转为字符串返回调用处. /** * 随机生成由数字.字 ...
- 设计模式也可以这么简单(7年开发老鸟PS注释总结)
设计模式是对大家实际工作中写的各种代码进行高层次抽象的总结,其中最出名的当属 Gang of Four (GoF) 的分类了,他们将设计模式分类为 23 种经典的模式,根据用途我们又可以分为三大类,分 ...
- swift基本体验
Swift初体验 1. 导入框架 2. 定义标识符: let/var 3. 语句结束时;可以省略 4. print() 5. let/var 6. 逻辑分支 7. 循环使用 // 1.Swift中如何 ...