给定1~100整数的乱序列表,查找并输出乘积是用户指定整数的两个整数下标对。
(本笔记适合熟练掌握Python列表的 coder 翻阅)
【学习的细节是欢悦的历程】
自学并不是什么神秘的东西 ,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。 —— 华罗庚
给定1~100的整数列表,打乱其次序
两 数 乘 积
(输出乘积是用户指定整数的两个整数下标)
本文质量分:
【 96 】
本文地址:
https://blog.csdn.net/m0_57158496/article/details/132815038
CSDN质量分查询入口:http://www.csdn.net/qc
目 录
◆ 两数乘积
1、题目描述
回页目录
2、算法解析
2.1 运行效果截图
代码运行效果截屏图片 一、默认目标整数88 输入非数字字符串或者不输入直接回车,默认目标整数88。 二、目标整数45 三、目标整数99
由于列表数多,一般不只一对下标满足题意。题目有要求,只能输出字典序较小的一对下标。我采用的是for循环遍历,且是从小到大,找到的第一对符题的下标即是字典序最小,退出两层for循环,打印下标即可。
2.2 优化后的for循环代码
Python代码:优化后的for循环
for i in range ( 100 ) :
for j in range ( i+ 1 , 100 ) :
mybool = lis[ i] * lis[ j] == target
if mybool:
break
if mybool:
break
print ( f"\n{lis[i]} × {lis[j]} = {target},({i}, {j})" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
2.3 有bug的代码
算法解析 : 最初代码,没有考虑到外层for循环的退出,造成了“最终的i、j不是达成目标时的i、j”的困扰。
for i in range ( 100 ) :
for j in range ( i+ 1 , 100 ) :
if lis[ i] * lis[ j] == target:
a, b = i, j
print ( i, j)
break
print ( i, j)
print ( f"\n{lis[a]} × {lis[b]} = {target},({a}, {b})" )
代码运行效果截屏图片 第一行是优化后的代码输出,最小字典序下标对。 最初代码只要找到符题下标对,就退出内层循环,继续遍历,找出了所有的符题组合(40, 44)、(62, 98)、(74, 97)、(80, 83),最后打印了最末一组下标对,是字典序最大的一组。最终,i、j变量都遍历到了下标99。
回页目录
3、完整源码
(源码较长,点此 跳过源码)
from random import shuffle
lis = list ( range ( 1 , 101 ) )
shuffle( lis)
print ( '\n100个整数列表:\n' , lis)
target = input ( '\n输入目标整数(如88):' )
target = 88 if not target. isdigit( ) else int ( target)
for i in range ( 100 ) :
for j in range ( i+ 1 , 100 ) :
mybool = lis[ i] * lis[ j] == target
if mybool:
break
if mybool:
break
print ( f"\n{lis[i]} × {lis[j]} = {target},({i}, {j})" )
for i in range ( 100 ) :
for j in range ( i+ 1 , 100 ) :
if lis[ i] * lis[ j] == target:
a, b = i, j
print ( i, j)
break
print ( i, j)
print ( f"\n{lis[a]} × {lis[b]} = {target},({a}, {b})" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
回页首
上一篇: 打印剪刀手“耶”(V形) (用给定单个字符和首行宽度(奇数),打印首行宽度为给定奇数“V”字形状)) 下一篇:
我的HOT 博:
本次共计收集 228 篇博文笔记信息,总阅读量 39.10w,平均阅读量 1714。已生成 26 篇阅读量不小于 3000 的博文笔记索引链接。数据采集于 2023-09-11 05:52:46 完成,用时 4 分 47.95 秒。
ChatGPT国内镜像站初体验:聊天、Python代码生成等 ( 58196 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/129035387 点赞:125 踩 :0 收藏:795 打赏:0 评论:75 本篇博文笔记于 2023-02-14 23:46:33 首发,最晚于 2023-07-03 05:50:55 修改。 让QQ群昵称色变的神奇代码 ( 57819 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500 点赞:24 踩 :0 收藏:83 打赏:0 评论:17 本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。 pandas 数据类型之 DataFrame ( 9078 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814 点赞:6 踩 :0 收藏:31 打赏:0 评论:0 本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。 个人信息提取(字符串) ( 7101 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618 点赞:1 踩 :0 收藏:13 打赏:0 评论:0 本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。 罗马数字转换器|罗马数字生成器 ( 6941 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047 点赞:0 踩 :0 收藏:1 打赏:0 评论:0 本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。 Python字符串居中显示 ( 6798 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023 点赞:1 踩 :0 收藏:6 打赏:0 评论:1 本篇博文笔记于 2021-12-26 23:35:29 发布。 Python列表(list)反序(降序)的7种实现方式 ( 6336 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700 点赞:5 踩 :0 收藏:19 打赏:0 评论:8 本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。 斐波那契数列的递归实现和for实现 ( 5486 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295 点赞:4 踩 :0 收藏:2 打赏:0 评论:8 本篇博文笔记于 2022-01-06 23:27:40 发布。 练习:字符串统计(坑:f‘string‘报错) ( 5058 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096 点赞:0 踩 :0 收藏:1 打赏:0 评论:0 本篇博文笔记于 2021-12-04 22:54:29 发布。 python清屏 ( 4963 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101 点赞:0 踩 :0 收藏:7 打赏:0 评论:0 本篇博文笔记于 2021-10-14 13:47:21 发布。 回车符、换行符和回车换行符 ( 4936 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488 点赞:1 踩 :0 收藏:2 打赏:0 评论:0 本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。 练习:尼姆游戏(聪明版/傻瓜式•人机对战) ( 4887 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399 点赞:14 踩 :0 收藏:42 打赏:0 评论:0 本篇博文笔记于 2021-11-30 23:43:17 发布。 密码强度检测器 ( 4253 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694 点赞:1 踩 :0 收藏:4 打赏:0 评论:0 本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。 练习:生成100个随机正整数 ( 4190 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220 点赞:1 踩 :0 收藏:6 打赏:0 评论:0 本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。 罗马数字转换器(用罗马数字构造元素的值取模实现) ( 4099 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。 我的 Python.color() (Python 色彩打印控制) ( 3982 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259 点赞:2 踩 :0 收藏:7 打赏:0 评论:0 本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。 练习:班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法) ( 3825 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124424935 点赞:1 踩 :0 收藏:4 打赏:0 评论:0 本篇博文笔记于 2022-04-26 12:46:25 首发,最晚于 2022-04-27 21:22:07 修改。 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。 ( 3622 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/125415626 点赞:3 踩 :0 收藏:6 打赏:0 评论:3 本篇博文笔记于 2022-06-22 19:54:20 首发,最晚于 2022-06-23 22:41:33 修改。 random.sample()将在python 3.9x后续版本中被弃用 ( 3539 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/120657230 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记 聊天消息敏感词屏蔽系统(字符串替换 str.replace(str1, *) ) ( 3407 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124539589 点赞:4 踩 :0 收藏:2 打赏:0 评论:3 本篇博文笔记于 2022-05-02 13:02:39 首发,最晚于 2022-05-21 06:10:42 修改。 Linux 脚本文件第一行的特殊注释符(井号和感叹号组合)的含义 ( 3377 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123087606 点赞:0 踩 :0 收藏:4 打赏:0 评论:3 本篇博文笔记于 2022-02-23 13:08:07 首发,最晚于 2022-04-04 23:52:38 修改。 练习:小炼二维数组 ( 3234 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/125175592 点赞:9 踩 :0 收藏:5 打赏:0 评论:9 本篇博文笔记于 2022-06-07 23:54:43 首发,最晚于 2022-06-08 00:31:49 修改。 练习:求列表(整数列表)平衡点 ( 3201 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121737612 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记 练习:银行复利计算(用 for 循环解一道初中小题) ( 3105 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123854548 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2022-03-30 20:06:37 首发,最晚于 2022-04-06 18:15:16 修改。 练习:柱状图中最大矩形 ( 3089 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122032365 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2021-12-19 23:47:07 发布。 练习:电话拨号键盘的字母组合(一个缩进给我惹了麻烦) ( 3009 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121887995 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2021-12-12 15:25:58 发布。
推荐条件
阅读量突破三千
(更多热博 ,请点击蓝色文字跳转翻阅)
回页首
精品文章:
来源:老齐教室
回页首
◆ Python 入门指南 【Python 3.6.3】
好文力荐:
CSDN实用技巧博文: