Python程序找到第一个和最后一个数字的和

python程序找到第一个和最后一个数字的和

在本文中,给定的任务是将整数的第一个数字最后一个数字相加。现在整数可以非常小,也可以很大。因此,这些计划将分为两部分。首先,我们需要找到这个整数有多大,然后从中得到第一个数字。第二部分是从给定的整数中获取最后一个数字,这可以通过将数字除以十并找到余数来轻松完成。在这篇 Python 文章中,使用四个不同的示例,给出了将整数的第一位和最后一位相加的方法。

在第一个示例中,使用重复除以 10 的方法来获取整数的位数。在示例 2 中,math.log10() 用于获取整数的位数。在示例 3 中,将整数转换为字符串以查找其长度;在示例 4 中,首先将整数转换为字符串,然后使用索引值 0 和 -1 来获取第一个和最后一个数字。然后将第一个和最后一个数字相加即可得到结果。

Example 1: 通过使用重复除法来查找整数的首位和末位数字之和,以找到数字的数量。

算法

第 1 步 – 编写一个 countDigits 函数来计算整数的位数。

第二步 – 使用重复除法方法。

立即学习“Python免费学习笔记(深入)”;

第三步 – 现在将整数除以10**count以获取第一个数字。

第四步 – 通过除以10并取余数来获取最后一个数字。

第 5 步 – 添加第一个和最后一个数字。

第 6 步 – 对数组中给出的不同长度的数字执行此操作。

第 7 步 – 打印输出。

Code

的中文翻译为:

代码

listofnumbers =[881234,954321, 7178952, 20033, 459, 20069]import math#define functiondef countDigits(thenumber):   count=0   while thenumber != 0:      thenumber //= 10      count += 1   return count#Use for loopfor 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:  881234The first digit is  8The last digit is  4The sum of first and the last digit is  12The Given number is:  954321The first digit is  9The last digit is  1The sum of first and the last digit is  10The Given number is:  7178952The first digit is  7The last digit is  2The sum of first and the last digit is  9The Given number is:  20033The first digit is  2The last digit is  3The sum of first and the last digit is  5The Given number is:  459The first digit is  4The last digit is  9The sum of first and the last digit is  13The Given number is:  20069The first digit is  2The last digit is  9The 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 moduleimport math#define functiondef 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:  1234The first digit is  1The last digit is  4The sum of first and the last digit is  5The Given number is:  54321The first digit is  5The last digit is  1The sum of first and the last digit is  6The Given number is:  678952The first digit is  6The last digit is  2The sum of first and the last digit is  8The Given number is:  200The first digit is  2The last digit is  0The sum of first and the last digit is  2The Given number is:  45The first digit is  4The last digit is  5The sum of first and the last digit is  9The Given number is:  10069The first digit is  1The last digit is  9The 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 mathdef 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:  11234The first digit is  1The last digit is  4The sum of first and the last digit is  5The Given number is:  554321The first digit is  5The last digit is  1The sum of first and the last digit is  6The Given number is:  6789521The first digit is  6The last digit is  1The sum of first and the last digit is  7The Given number is:  2004The first digit is  2The last digit is  4The sum of first and the last digit is  6The Given number is:  3455The first digit is  3The last digit is  5The sum of first and the last digit is  8The Given number is:  60069The first digit is  6The last digit is  9The 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 loopfor 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:  12343The first digit is  1The last digit is  3The sum of first and the last digit is  4The Given number is:  543210The first digit is  5The last digit is  0The sum of first and the last digit is  5The Given number is:  6789529The first digit is  6The last digit is  9The sum of first and the last digit is  15The Given number is:  9200The first digit is  9The last digit is  0The sum of first and the last digit is  9The Given number is:  45The first digit is  4The last digit is  5The sum of first and the last digit is  9The Given number is:  810069The first digit is  8The last digit is  9The sum of first and the last digit is  17 

这些数字是从一个数组中指定和提取的。

结论

我们在这里给出了各种方法来展示如何将整数的第一个数字和最后一个数字相加。不同长度的不同整数被写入一个数组中。然后对这些整数使用不同的方法。这些方法的区别主要在于查找整数中位数的方法或从中查找第一个和最后一个数字的方法。

以上就是Python程序找到第一个和最后一个数字的和的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1342776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 05:53:33
下一篇 2025年12月13日 05:53:51

相关推荐

  • 利用CSS实现纯英文数字自动换行

    下面为大家带来一篇css代码使纯英文数字自动换行的简单实现。内容挺不错的,现在就分享给大家,也给大家做个参考。 当一个定义了宽度的块状元素中填充的全部为纯英文或者纯数字的时候,在IE和FF中都会撑大容器,不会自动换行 并且当数字或者英文中带有汉字时,会从汉字处换行,而纯汉字却可以自动换行。这个问题如…

    好文分享 2025年12月24日
    000
  • html5怎么设置input只能输入数字

    在html5中,可以通过input标签的type属性来实现只能输入数字的功能,只需要将type属性的值设置为“number”即可,语法“”。 本教程操作环境:windows7系统、HTML5版、Dell G3电脑。 标签规定用户可输入数据的输入字段。 根据不同的 type 属性,输入字段有多种形态。…

    2025年12月21日
    000
  • HTML怎么实现数字焦点图轮播代码

    html怎么实现数字焦点图轮播代码?数字焦点图轮播怎么做?数字焦点图轮播需要注意什么?给大家一份实现数字焦点图轮播代码,需要的朋友可以借鉴一下。 数字焦点图轮播代码 @@##@@ @@##@@ @@##@@ 1 2 3 数字焦点图轮播代码就是这么多了,更多精彩请关注创想鸟其它相关文章! 相关阅读: …

    好文分享 2025年12月21日
    000
  • C/C++中的数字连线游戏?

    游戏 – 假设有一个 n × n 的方格数组。其中,一些方格是空的,一些是实心的,还有一些非实心的方格由整数 1、2、3、… 设置。每个整数在棋盘上保持或占据恰好两个不同的方格。玩家的任务是借助仅实现水平和垂直移动的简单路径来连接棋盘上每个整数的两次出现。不允许两条不同的路径…

    2025年12月17日
    000
  • 一个数字连线游戏?

    数字连接是一种逻辑谜题,涉及在网格中找到连接数字的路径。 Numberlink谜题的一个简单例子 Numberlink谜题的解答 规则 – 玩家必须用单一连续线(或路径)将网格上的所有匹配数字配对。线条不能分叉或交叉,并且数字必须位于每条线的末端(即不在中间)。只有当问题具有唯一解并且网…

    2025年12月17日
    000
  • 在给定的数组中找到最后一个回文字符串

    在这个问题中,我们需要找到数组中的最后一个回文字符串。如果任何字符串在读取时相同,无论是从头开始读取还是从末尾开始读取,都可以说该字符串是回文。我们可以比较起始字符和结束字符来检查特定字符串是否是回文。查找回文字符串的另一种方法是将字符串反转并与原始字符串进行比较。 问题陈述 – 我们给…

    2025年12月17日
    000
  • C++程序用于计算使数字n变为1所需的最小操作次数

    假设我们有一个数字n。我们任意执行这些操作之一 – 当 n 可被 2 整除时,将 n 替换为 n/2 当 n 可被 3 整除时,将 n 替换为 2n/3 当 n 可被 5 整除时,将 n 替换为 4n/5 立即学习“C++免费学习笔记(深入)”; li> 我们必须计算出数字 1 所…

    2025年12月17日
    100
  • 使用堆栈在C++中反转一个数字

    We are given an integer number Num as input. The goal is to find the reverse of the number using stack. Stack:- A stack is a data structure in C++ whi…

    2025年12月17日
    000
  • 找出在范围内不可被任何数整除的数字,使用C++

    在本文中,我们将讨论查找 1 到 n(给定)之间的数字的问题,这些数字不能被 2 到 10 之间的任何数字整除。让我们通过一些例子来理解这一点 – Input : num = 14Output : 3Explanation: There are three numbers, 1, 11,…

    2025年12月17日
    000
  • C++程序将一个数字四舍五入到n位小数

    在任何语言中编写程序时,将数字表示为输出是一项有趣且重要的任务。对于整数类型(short、long或medium类型的数据),很容易将数字表示为输出。对于浮点数(float或double类型),有时我们需要将其四舍五入到特定的小数位数。例如,如果我们想将52.24568表示为三位小数,需要进行一些预…

    2025年12月17日
    000
  • 两两乘积之和

    集合X = {a, b, c}的成对乘积可以定义为所有可能的集合对乘积的和。集合的成对为Y = {a * a, a * b, a *c, b * b, b * c, c * c},其中乘积是可交换的。因此,集合X的成对乘积是集合Y的元素之和,即aa + ab + ac + bb + bc + cc。…

    2025年12月17日
    000
  • 给定一个字符串,其中字母的表示方式被打乱的数字

    在今天的文章中,我们将深入探讨与C++中字符串操作相关的一个独特问题。这个问题是“在给定字符串中,字母表达式被打乱的数字。” 这个问题可以作为一个很好的练习,来提高你在C++中的字符串操作和数据结构技能。 问题陈述 给定一个字符串,任务是识别其中字母表达方式被打乱的数字。例如,如果输入字符串是&#8…

    2025年12月17日
    000
  • 递归程序在C++中检查一个数字是否是回文数

    我们得到一个整数作为输入。目标是使用递归来确定输入数字 Num 是否为回文。 要检查一个数字是否为回文,请反转该数字并检查两个数字是否相同。如果反转后的数等于原数,则为回文。 示例 输入− Num = 34212; 输出− 34212 不是回文! 解释− 如果我们反转 34212,则得到 21243…

    2025年12月17日
    000
  • 打印n个数字,使它们的和是一个完全平方数

    给定n个数字,程序必须找到这n个数字的和为一个完全平方数 Input : 5Output : 1 3 5 7 91+3+5+7+9=25 i.e (5)^2 算法 START Step 1 : Declare a Macro for size let’s say of 5 and i t…

    2025年12月17日
    000
  • 使用C++编写,在矩阵中找到给定和的一对数字

    在本文中,我们将讨论在给定矩阵中查找具有给定和的对的程序。例如 – Input : matrix[n][m] = { { 4, 6, 4, 65 }, { 56, 1, 12, 32 }, { 4, 5, 6, 44 }, { 13, 9, 11, 25 } }, SUM = 20Out…

    2025年12月17日
    000
  • 前n个自然数的平方和的和

    前 n 个自然数的平方和是求最多 n 项的平方和。本系列求 n 以内每个数字的和,并将该和添加到 sum 变量中。 前 4 个自然数的平方和之和为 – sum = ( 12) + (12 + 22 ) + (12 + 22 + 32) + (12 + 22 + 32 + 4 2 ) = …

    2025年12月17日
    000
  • C程序用于判断给定的数字是否为强数

    一个强数是一个数字,其中各位数字的阶乘之和等于该数字本身。 示例 123!= 1!+2!+3!                    =1+2+6 =9 在这个例子中,123不是一个强数,因为各位数字的阶乘之和不等于该数字本身。 145!=1!+4!+5!             =1+24+120…

    2025年12月17日
    000
  • 找到通过插入给定数字形成的最小数字

    在给定的数字中插入一个数字意味着在给定的数字中添加一个新的数字,可以是在数字的前面、后面或者中间。我们已经给出了一个数字和一个数字,并且必须以尽可能小的方式将该数字添加到数字中。为了方便插入操作,我们将把数字转换为字符串。此外,给定的数字也可以是负数,因此我们必须考虑这种情况。 示例示例 Input…

    2025年12月17日
    000
  • C程序以找到链表的长度

    链接列表使用动态内存分配,即它们相应地增长和收缩。它们被定义为节点的集合。这里,节点有两部分,即数据和链路。数据、链接和链表的表示如下 – 链表的类型 链表有四种类型,如下: – 单链表/ 单链表双/双向链表循环单链表循环双链表 我们使用递归方法求链表长度的逻辑是 &#821…

    2025年12月17日
    000
  • 打印给定数字的乘法表在C中

    程序描述 打印给定数字的乘法表 算法 接受用户提供的任何需要形成乘法的数字 从 I 的值开始乘以给定数 (=1) 将给定数与 I 的值递增,直到 I 值小于或等于12. 示例 /* Program to print the multiplication table of a given number…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信