当前位置:网站首页>Calculate the odd sum of 1~n (1~100 as an example)
Calculate the odd sum of 1~n (1~100 as an example)
2022-07-04 03:33:00 【Yueyechong】
n=int(input())
sum=0
for i in range(1,n+1,2):
sum+=i
print(sum)
Input 100 have to 2500
utilize range function :range(1,5,2) # For from 1 To 5, interval 2( It doesn't contain 5)
Method 2: use while,matlab Thought
b=int(input())
n=1
sum=0
while n<b:
sum+=n
n+=2
print(sum)
Method 3
n=int(input())
sum=0
ls=[]
for i in range(1,n+1):
if i %2!=0:
sum+=i
print(sum)
Method 5: If you want to check the inside
n=int(input())
sum=0
ls=[]
for i in range(1,n+1):
if i %2!=0:
sum+=i
ls.append(i)# Show list , Beginners can't debug Internal numbers can be displayed in a list
print(ls)# Show the numbers
print(sum)
Is the number correct , It can be displayed in a list
边栏推荐
- Learning video website
- system information
- Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
- Leetcode 110 balanced binary tree
- 基于PHP的轻量企业销售管理系统
- Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
- What is the difference between enterprise wechat applet and wechat applet
- JS object definition
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- Add token validation in swagger
猜你喜欢
The difference between MCU serial communication and parallel communication and the understanding of UART
Record a problem that soft deletion fails due to warehouse level error
选择排序与冒泡排序模板
Summary of Chinese remainder theorem
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
I stepped on a foundation pit today
Jenkins configures IP address access
Nbear introduction and use diagram
MySQL data query optimization -- data structure of index
1day vulnerability pushback skills practice (3)
随机推荐
潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
CSCI 2134
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
How to use websocket to realize simple chat function in C #
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Slurm view node configuration information
The property of judging odd or even numbers about XOR.
Leecode 122. Zuijia timing of buying and selling stocks ②
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
I stepped on a foundation pit today
[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
Redis transaction
Package details_ Four access control characters_ Two details of protected
super_ Subclass object memory structure_ Inheritance tree traceability
1day vulnerability pushback skills practice (3)
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
CSP drawing