当前位置:网站首页>Chapter 2-14 sum integer segments

Chapter 2-14 sum integer segments

2022-07-28 08:44:00 ZJUdebug3790

Given two integers A and B, Output from A To B All the integers of and the sum of these numbers .

Input format :

Enter... On one line 2 It's an integer A and B, among −100≤A≤B≤100, Separated by spaces .

Output format :

First, output sequentially from A To B All integers of , Every time 5 A line of numbers , Each number accounts for 5 Character width , Align right . Finally, in a line, press Sum = X Output the sum of all the numbers X.

sample input :

-3 8

sample output :

   -3   -2   -1    0    1
    2    3    4    5    6
    7    8
Sum = 30

  The correct code is as follows :

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))

At first , I first 2 Test points failed , After careful comparison, it was found that it was because of printing Sum I forgot to judge the condition

For example, the format is wrong :

 

原网站

版权声明
本文为[ZJUdebug3790]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207280714542684.html