Python程序找到第一个和最后一个数字的和
在本文中,给定的任务是将整数的第一个数字和最后一个数字相加。现在整数可以非常小,也可以很大。因此,这些计划将分为两部分。首先,我们需要找到这个整数有多大,然后从中得到第一个数字。第二部分是从给定的整数中获取最后一个数字,这可以通过将数字除以十并找到余数来轻松完成。在这篇 Python 文章中,使用四个不同的示例,给出了将整数的第一位和最后一位相加的方法。
在第一个示例中,使用重复除以 10 的方法来获取整数的位数。在示例 2 中,math.log10() 用于获取整数的位数。在示例 3 中,将整数转换为字符串以查找其长度;在示例 4 中,首先将整数转换为字符串,然后使用索引值 0 和 -1 来获取第一个和最后一个数字。然后将第一个和最后一个数字相加即可得到结果。
Example 1: 通过使用重复除法来查找整数的首位和末位数字之和,以找到数字的数量。
算法
第 1 步 - 编写一个 countDigits 函数来计算整数的位数。
第二步 - 使用重复除法方法。
第三步 - 现在将整数除以10**count以获取第一个数字。
第四步 - 通过除以10并取余数来获取最后一个数字。
第 5 步 - 添加第一个和最后一个数字。
第 6 步 - 对数组中给出的不同长度的数字执行此操作。
第 7 步 - 打印输出。
Code
的中文翻译为:代码
listofnumbers =[881234,954321, 7178952, 20033, 459, 20069] import math #define function def countDigits(thenumber): count=0 while thenumber != 0: thenumber //= 10 count += 1 return count #Use for loop for item in listofnumbers: c=countDigits(item) firstnum=math.floor(item/10**(c-1)) lastnum=item%10 total=firstnum+lastnum print("\nThe Given number is: " , item) print("The first digit is ", firstnum) print("The last digit is ", lastnum) print("The sum of first and the last digit is " , total)
输出 - 示例 1
在命令窗口中运行 python 文件
打开cmd窗口。在cmd窗口中检查输出。
The Given number is: 881234 The first digit is 8 The last digit is 4 The sum of first and the last digit is 12 The Given number is: 954321 The first digit is 9 The last digit is 1 The sum of first and the last digit is 10 The Given number is: 7178952 The first digit is 7 The last digit is 2 The sum of first and the last digit is 9 The Given number is: 20033 The first digit is 2 The last digit is 3 The sum of first and the last digit is 5 The Given number is: 459 The first digit is 4 The last digit is 9 The sum of first and the last digit is 13 The Given number is: 20069 The first digit is 2 The last digit is 9 The sum of first and the last digit is 11
例子2:通过使用math.log10函数来找到整数的首位和末位数字的和,以找到数字的位数。
算法
第 1 步 - 要计算整数的位数,请编写 countDigits 函数。
第 2 步 - 在此函数中使用公式 math.floor(math.log10(thenumber) + 1)。
第 3 步 - 现在将整数除以 10**count 以获取第一个数字
第 4 步 - 除以 10 并得到余数,得到最后一个数字。
第五步 - 要得到总和,将第一个数和最后一个数相加。
第 6 步 - 使用具有不同整数的数组来对不同长度的数字执行此操作。
第 7 步 - 打印输出总和。
listofnumbers =[1234,54321, 678952, 200, 45, 10069] #Import the required module import math #define function def countDigits(thenumber): return math.floor(math.log10(thenumber) + 1) #Use for loop to iterate item for item in listofnumbers: c=countDigits(item) firstnum=math.floor(item/10**(c-1)) lastnum=item%10 total=firstnum+lastnum print("\nThe Given number is: " , item) print("The first digit is ", firstnum) print("The last digit is ", lastnum) print("The sum of first and the last digit is " , total)
输出 - 示例 2
在命令窗口中运行 python 文件
打开cmd窗口。在cmd窗口中检查输出。
The Given number is: 1234 The first digit is 1 The last digit is 4 The sum of first and the last digit is 5 The Given number is: 54321 The first digit is 5 The last digit is 1 The sum of first and the last digit is 6 The Given number is: 678952 The first digit is 6 The last digit is 2 The sum of first and the last digit is 8 The Given number is: 200 The first digit is 2 The last digit is 0 The sum of first and the last digit is 2 The Given number is: 45 The first digit is 4 The last digit is 5 The sum of first and the last digit is 9 The Given number is: 10069 The first digit is 1 The last digit is 9 The sum of first and the last digit is 10
示例 3:通过将 int 转换为 str 并使用 len 函数查找位数来查找整数的第一位和最后一位数字之和
算法
第 1 步 - 编写一个 countDigits 函数来计算整数的位数。
第二步 - 在这个函数内部,对于计数,首先将int转换为str,然后获取其长度。
第 3 步 - 现在将整数除以 10**count 以获得第一个数字。
第四步 - 通过除以十并获取余数来获取最后一个数字。
第 5 步 - 现在添加第一个和最后一个数字。
第 6 步 - 对数组中给出的所有数字执行此方法。
第 7 步 - 打印输出总和。
listofnumbers =[11234,554321, 6789521, 2004, 3455, 60069] import math def countDigits(thenumber): snum=str(thenumber) l=len(snum) return l for item in listofnumbers: c=countDigits(item) firstnum=math.floor(item/10**(c-1)) lastnum=item%10 total=firstnum+lastnum print("\nThe Given number is: " , item) print("The first digit is ", firstnum) print("The last digit is ", lastnum) print("The sum of first and the last digit is " , total)
输出 - 示例 3
在命令窗口中运行 python 文件
打开cmd窗口。检查cmd窗口中的输出。
The Given number is: 11234 The first digit is 1 The last digit is 4 The sum of first and the last digit is 5 The Given number is: 554321 The first digit is 5 The last digit is 1 The sum of first and the last digit is 6 The Given number is: 6789521 The first digit is 6 The last digit is 1 The sum of first and the last digit is 7 The Given number is: 2004 The first digit is 2 The last digit is 4 The sum of first and the last digit is 6 The Given number is: 3455 The first digit is 3 The last digit is 5 The sum of first and the last digit is 8 The Given number is: 60069 The first digit is 6 The last digit is 9 The sum of first and the last digit is 15
图3:示例3在CMD窗口中的输出
示例 4:通过使用字符串索引值查找第一个和最后一个数字来查找整数的第一个和最后一个数字之和
算法
第 1 步 - 首先将整数转换为字符串。
第 2 步 - 使用索引 0 获取第一个数字,然后将其转换回整数。
第 3 步 - 使用索引 -1 获取最后一位数字,然后将其转换回整数。
步骤 4 - 添加第一个和最后一个数字。
第 5 步 - 对数组中给出的不同长度的数字执行此操作。
第 6 步 - 打印计算出的总数。
listofnumbers =[12343,543210, 6789529, 9200, 45, 810069] #Use for loop for item in listofnumbers: snum=str(item) firstnum=int(snum[0]) lastnum=int(snum[-1]) total=firstnum+lastnum print("\nThe Given number is: " , item) print("The first digit is ", firstnum) print("The last digit is ", lastnum) print("The sum of first and the last digit is " , total)
输出 - 示例 4
在命令窗口中运行 python 文件
打开cmd窗口。在cmd窗口中检查输出。
The Given number is: 12343 The first digit is 1 The last digit is 3 The sum of first and the last digit is 4 The Given number is: 543210 The first digit is 5 The last digit is 0 The sum of first and the last digit is 5 The Given number is: 6789529 The first digit is 6 The last digit is 9 The sum of first and the last digit is 15 The Given number is: 9200 The first digit is 9 The last digit is 0 The sum of first and the last digit is 9 The Given number is: 45 The first digit is 4 The last digit is 5 The sum of first and the last digit is 9 The Given number is: 810069 The first digit is 8 The last digit is 9 The sum of first and the last digit is 17
这些数字是从一个数组中指定和提取的。
结论
我们在这里给出了各种方法来展示如何将整数的第一个数字和最后一个数字相加。不同长度的不同整数被写入一个数组中。然后对这些整数使用不同的方法。这些方法的区别主要在于查找整数中位数的方法或从中查找第一个和最后一个数字的方法。
以上是Python程序找到第一个和最后一个数字的和的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

待机是一种锁定屏幕模式,当iPhone插入充电器并以水平(或横向)方向定位时激活。它由三个不同的屏幕组成,其中一个是全屏时间显示。继续阅读以了解如何更改时钟的样式。StandBy的第三个屏幕显示各种主题的时间和日期,您可以垂直滑动。某些主题还会显示其他信息,例如温度或下一个闹钟。如果您按住任何时钟,则可以在不同的主题之间切换,包括数字、模拟、世界、太阳能和浮动。Float以可自定义的颜色以大气泡数字显示时间,Solar具有更多标准字体,具有不同颜色的太阳耀斑设计,而World则通过突出显示世界地

生成随机数或字母数字字符串的能力在许多情况下都会派上用场。您可以使用它在游戏中的不同位置生成敌人或食物。您还可以使用它向用户建议随机密码或创建文件名来保存文件。我写了一篇关于如何在PHP中生成随机字母数字字符串的教程。我在这篇文章的开头说,几乎没有事件是真正随机的,同样的情况也适用于随机数或字符串生成。在本教程中,我将向您展示如何在JavaScript中生成伪随机字母数字字符串。在JavaScript中生成随机数让我们从生成随机数开始。我想到的第一个方法是Math.random(),它返回一个浮

R55600搭配华硕哪个主板华硕ROGStrixB550-FGaming主板是一个非常出色的选择。它与Ryzen55600X处理器完美兼容,并提供出色的性能和功能。该主板具备可靠的供电系统,可支持超频,并提供丰富的扩展插槽和端口,满足日常使用和游戏需求。ROGStrixB550-FGaming还配备了高品质的音频解决方案、快速的网络连接和可靠的散热设计,确保系统保持高效稳定。此外,该主板还采用了华丽的ROG风格,配备了华丽的RGB照明效果,为您的计算机增添了视觉享受。总而言之,华硕ROGStri

赛扬g4900和i36100哪个好当涉及到赛扬G4900和I36100这两款处理器时,毫无疑问,I36100的性能更胜一筹。赛扬处理器通常被视为低端处理器,主要用于廉价笔记本电脑。而I3处理器则主要用于高端处理器,其性能非常出色。不论是玩游戏还是观看视频,使用I3处理器都不会出现任何卡顿情况。因此,如果你有可能,尽量选择购买英特尔I系列处理器,特别是用于台式机,这样你就能畅享网络世界的乐趣了。赛扬G4900T性能怎么样从性能方面来看,奔腾G4900T在频率方面表现出色,相比之前的版本,CPU性能

在任何语言中编写程序时,将数字表示为输出是一项有趣且重要的任务。对于整数类型(short、long或medium类型的数据),很容易将数字表示为输出。对于浮点数(float或double类型),有时我们需要将其四舍五入到特定的小数位数。例如,如果我们想将52.24568表示为三位小数,需要进行一些预处理。在本文中,我们将介绍几种技术,通过四舍五入将浮点数表示为特定的小数位数。在不同的方法中,使用类似C的格式化字符串、使用精度参数以及使用数学库中的round()函数是很重要的。让我们逐个来看。带有

链接列表使用动态内存分配,即它们相应地增长和收缩。它们被定义为节点的集合。这里,节点有两部分,即数据和链路。数据、链接和链表的表示如下-链表的类型链表有四种类型,如下:-单链表/单链表双/双向链表循环单链表循环双链表我们使用递归方法求链表长度的逻辑是-intlength(node*temp){ if(temp==NULL) returnl; else{&n

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

Java中的数字重要的是要理解数字类不是一个有形的类,而是一个抽象的类。在它内部,我们有一组定义其功能的包装类。这些包装类包括Integer、Byte、Double、Short、Float和Long。您可能会注意到,这些与我们之前讨论的基本数据类型相同,但它们表示为具有大写名称的单独类,以符合类命名约定。根据特定函数或程序范围的要求,编译器自动将原始数据类型转换为对象,反之亦然,并且数字类是java.lang包的一部分。此过程称为自动装箱和拆箱。通过掌握数字类及其对应的包装类的抽象性质,我们可以
