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

ShopWind网店系统
ShopWind网店系统

ShopWind网店系统是国内最专业的网店程序之一,采用ASP语言设计开发,速度快、性能好、安全性高。ShopWind网店购物系统提供性化的后台管理界面,标准的网上商店管理模式和强大的网店软件后台管理功能。ShopWind网店系统提供了灵活强大的模板机制,内置多套免费精美模板,同时可在后台任意更换,让您即刻快速建立不同的网店外观。同时您可以对网模板自定义设计,建立个性化网店形象。ShopWind网

下载
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

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

715

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

625

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

739

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

617

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1235

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

575

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

697

2023.08.11

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.1万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.3万人学习

Django 教程
Django 教程

共28课时 | 2.6万人学习

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

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