当前位置:网站首页>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.
边栏推荐
猜你喜欢

VSCode Plugin: Nested Comments

基于FPGA的售货机

coldfusion文件读取漏洞(CVE-2010-2861)

Drools WorkBench的简介与使用

mmdetection训练一个模型相关命令

Fiddler抓包模拟弱网络环境测试

系统需求多变如何设计

MySql installation and configuration super detailed tutorial and simple method of building database and table

Between two orderly array of additive and Topk problem

Crypto Life, a day in the life of a Web3 project partner
随机推荐
Drools Rule Properties, Advanced Syntax
初识C语言 -- 数组
Drools basic introduction, introductory case, basic syntax
STP选举(步骤+案列)详解
PDF split/merge
What are the project management tools like MS Project
Tower of Hanoi problem
Introduction and use of Drools WorkBench
FPGA-based vending machine
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
用户交互+格式化输出
cudaMemcpy学习笔记
The real CTO is a technical person who understands products
What have I experienced to become a tester who is harder than development?
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
keep-alive缓存组件
Arbitrum 专访 | L2 Summer, 脱颖而出的 Arbitrum 为开发者带来了什么?
mysql 视图
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法