当前位置:网站首页>Calculate S=a+aa+…+aa…a
Calculate S=a+aa+…+aa…a
2022-07-31 01:52:00 【The beauty of algorithms and programming】
0 Introduction
Using python to write code can be used to easily solve mathematical operation problems.
1 Question
Find the value of s= a + aa + aaa + … + aa…a (the number of a in the last number is n), where a is a number from 1 to 9, for example: 2 + 22 + 222 +2222 + 22222.
Input: One line, including two integers, the first is a, the second is n (1 ≤ a ≤ 9, 1 ≤ n ≤ 9), separated by commas.
Output: one line, the value of s.
2 Method
Define a function that uses a for loop to continuously add up, and reassign the original input value a to obtain the calculation of the sum of a+aa+aaa+…+a…a.The input a and n should be separated by commas and use the split function.
3 Experimental Results and Discussion
It has been proved through experiments and practice that the proposed method is effective and can solve the problems raised at the beginning.
Code Listing 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('Please input a and n:').split(',')) result=get(a,n) print(result) |
4 Concluding Remarks
In this question, functions and loop statements are used to calculate mathematical problems through programs, which makes the calculation convenient and fast, and can solve problems with large and many values.
边栏推荐
- 16、注册中心-consul
- CV-Model [3]: MobileNet v2
- Verify the integer input
- multiplayer-hlap 包有问题,无法升级的解决方案
- How to expose Prometheus metrics in go programs
- pc端判断当前使用浏览器类型
- 221. 最大正方形
- Software Testing Defect Reporting - Definition, Composition, Defect Lifecycle, Defect Tracking Post-Production Process, Defect Tracking Process, Purpose of Defect Tracking, Defect Management Tools
- Path and the largest
- C language applet -- common classic practice questions
猜你喜欢
uniapp uses 3rd party fonts
leetcode-952:按公因数计算最大组件大小
rpm install postgresql12
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
coldfusion8后台计划任务拿shell
最大路径和
静态路由+PAT+静态NAT(讲解+实验)
二层广播风暴(产生原因+判断+解决)
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
随机推荐
静态路由+PAT+静态NAT(讲解+实验)
The PC side determines the type of browser currently in use
Static route analysis (the longest mask matching principle + active and standby routes)
打印任务排序 js od华为
Teach you how to configure Jenkins automated email notifications
爬虫文本数据清洗
验证整数输入
一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
coldfusion文件读取漏洞(CVE-2010-2861)
Real-time image acquisition based on FPGA
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
pc端判断当前使用浏览器类型
【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
Kyushu cloud as cloud computing standardization excellent member unit
Is there a way to earn 300 yuan a day by doing a side business?
PDF split/merge
验证 XML 文档
link与@import的区别
充电效果模拟
【flask入门系列】Flask-SQLAlchemy的使用