当前位置:网站首页>计算S=a+aa+…+aa…a
计算S=a+aa+…+aa…a
2022-07-31 01:25:00 【算法与编程之美】
0 引言
运用python编写代码可以用来轻松地解决数学的运算问题。
1 问题
求s= a + aa + aaa + … + aa…a 的值(最后一个数中 a 的个数为n ),其中 a 是一个1~9的数字,例如:2 + 22 + 222 + 2222 + 22222 。
输入:一行,包括两个整数,第1个为a,第2个为n(1 ≤ a ≤ 9,1 ≤ n ≤ 9),以英文逗号分隔。
输出:一行,s的值。
2 方法
定义一个函数,利用for循环来进行不断地加总,并且在原始输入的值a进行重新赋值,得到a+aa+aaa+…+a…a的和的计算。输入的a和n要用逗号隔开,使用split函数。
3 实验结果与讨论
通过实验、实践等证明提出的方法是有效的,是能够解决开头提出的问题。
代码清单 1
def get(a,n): Sn=0 sum=0 for i in range(1,n+1): Sn=Sn*10+a Sum+=Sn return sum a,n=map(eval,input(‘请输入a和n:’).split(‘,’)) result=get(a,n) print(result) |
4 结语
本题中运用了函数和循环语句,通过程序来计算数学问题,使计算变得方便和快捷,能够很好的解决数值大和多的问题。
边栏推荐
- ShardingSphere之未分片表配置实战(六)
- 软件测试要达到一个什么水平才能找到一份9K的工作?
- Bert usage and word prediction based on Keras_bert model
- Problem record in the use of TypeScript
- C语言_结构体指针数组函数选票系统
- ROS Action communication
- The Meta Metaverse Division lost 2.8 billion in the second quarter, still want to continue to bet?Metaverse development has yet to see a way out
- 太阳能板最大面积 od js
- typescript10-常用基础类型
- prometheus 监控概述
猜你喜欢
随机推荐
ShardingSphere's vertical sub-database sub-table actual combat (5)
Teach you how to configure Jenkins automated email notifications
MySql data recovery method personal summary
分布式.幂等性
typescript12 - union types
ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)
C language _ structure pointer array function voting system
TiDB 在多点数字化零售场景下的应用
typescript13-类型别名
822. Walk the Grid
typescript15-(同时指定参数和返回值类型)
手把手教你配置Jenkins自动化邮件通知
In Google Cloud API gateway APISIX T2A and T2D performance test
typescript9-常用基础类型
《实战》基于电商领域的词性提取及其决策树模型建模
822. 走方格
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
1782. Count the number of point pairs Double pointer
System design. Short chain system design
使用docker安装mysql