当前位置:网站首页>第2章-14 求整数段和
第2章-14 求整数段和
2022-07-28 07:15:00 【ZJUdebug3790】
给定两个整数A和B,输出从A到B的所有整数以及这些数的和。
输入格式:
输入在一行中给出2个整数A和B,其中−100≤A≤B≤100,其间以空格分隔。
输出格式:
首先顺序输出从A到B的所有整数,每5个数字占一行,每个数字占5个字符宽度,向右对齐。最后在一行中按Sum = X的格式输出全部数字的和X。
输入样例:
-3 8
输出样例:
-3 -2 -1 0 1
2 3 4 5 6
7 8
Sum = 30
正确代码如下:
a,b=map(int,input().split())
s=0
t=1
for i in range(a,b+1):
s+=i
if t%5==0:
print('{0:>5d}'.format(i))
t=1
else:
print('{0:>5d}'.format(i),end='')
t+=1
if t%5==1:
print('Sum = %d' % (s))
else:
print('\nSum = %d'%(s))刚开始,我第2个测试点没有通过,仔细比对后发现是因为打印Sum的时候忘记了条件判断
比如这样的格式错误:

边栏推荐
- CarSim simulation quick start (XII) - Driver Model (2)
- How to configure phpunit under window
- pyspark更改列顺序存入iceberg数据库
- 2021-07-02
- Usage of qmap
- Kubernetes技术与架构(七)
- 2022牛客多校第二场解题报告
- [reprint] man Rsync translation (Chinese Manual of Rsync command)
- Js继承方法
- An article to understand data warehouse: metadata classification, metadata management
猜你喜欢

See how Google uses pre training weights in target detection tasks | CVPR 2022

本人男,27岁技术经理,收入太高,心头慌得一比
![[mindspire YiDianTong robot-01] you may have seen many Knowledge Q & A robots, but this is a little different](/img/d1/c2c2e4a605deddd0073a05d528733f.jpg)
[mindspire YiDianTong robot-01] you may have seen many Knowledge Q & A robots, but this is a little different

半桥BUCK电路—记录篇

The cooperation between starfish OS and metabell is just the beginning

Half bridge buck circuit - record

The fourth phase (2021-2022) research on the implementation of cloud native technology in traditional industries - central state-owned enterprises was officially released

阻塞队列LinkedBlockingQueue 源码解析

Can‘t connect to server on ‘IP‘ (60)

Mysql-怎么添加用户和设置权限?
随机推荐
49-OpenCv深入分析轮廓
CAT1 4G+以太网开发板232数据通过4G模块TCP发到服务器
CarSim simulation quick start (10) - Modeling of braking system
classLoader加载的class的回收
Half bridge buck circuit - record
bash-shell 免交互
Hcip --- LDP and MPLS Technology (detailed explanation)
[Qt5] QT small software release
Sparksql and flinksql create and link table records
Win the bid! Nantah general gbase 8s won the bid for the 2022 database framework project of NARI Group
Analysis of model predictive control (MPC) (IX): numerical solution of quadratic programming (II)
SQL function
Pyspark writes data to iceberg
leetcode/单词长度的最大乘积
C#,入门教程——程序运行时的调试技巧与逻辑错误探针技术与源代码
博客搭建七:hugo
How to configure phpunit under window
uniapp上下滑屏切换支持视频和图片轮播实现,类似抖音效果
PHP基础知识 - PHP 使用 PDO
Unity切换到另一个场景的时候,发现该场景变暗了