0

0

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

王林

王林

发布时间:2023-09-13 11:17:08

|

1632人浏览过

|

来源于tutorialspoint

转载

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 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 步 - 打印输出总和。

CreateWise AI
CreateWise AI

为播客创作者设计的AI创作工具,AI自动去口癖、提交亮点和生成Show notes、标题等

下载
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速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
counta和count的区别
counta和count的区别

Count函数用于计算指定范围内数字的个数,而CountA函数用于计算指定范围内非空单元格的个数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

203

2023.11.20

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

761

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

221

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1570

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

651

2023.11.24

java读取文件转成字符串的方法
java读取文件转成字符串的方法

Java8引入了新的文件I/O API,使用java.nio.file.Files类读取文件内容更加方便。对于较旧版本的Java,可以使用java.io.FileReader和java.io.BufferedReader来读取文件。在这些方法中,你需要将文件路径替换为你的实际文件路径,并且可能需要处理可能的IOException异常。想了解更多java的相关内容,可以阅读本专题下面的文章。

1228

2024.03.22

php中定义字符串的方式
php中定义字符串的方式

php中定义字符串的方式:单引号;双引号;heredoc语法等等。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

1205

2024.04.29

go语言字符串相关教程
go语言字符串相关教程

本专题整合了go语言字符串相关教程,阅读专题下面的文章了解更多详细内容。

193

2025.07.29

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

26

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP基础入门课程
PHP基础入门课程

共33课时 | 2.3万人学习

WEB前端教程【HTML5+CSS3+JS】
WEB前端教程【HTML5+CSS3+JS】

共101课时 | 10.2万人学习

apipost极速入门
apipost极速入门

共6课时 | 0.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号