L1-008 求整數段和
給定兩個整數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
代碼如下
#include <iostream>
using namespace std;
int main()
{
int A,B;
int count=0;
int Sum=0;
cin>>A>>B;
for (int i=A; i<=B; i++)
{
cout.setf(ios::right);//設定右對齊
cout.width(5);//設定寬度為5
cout<<i;
count++;
if (count%5==0 && i!=B)//每五個數字換行
{
cout<<endl;
}
Sum=Sum+i;
}
cout<<"\n"<<"Sum"<<" = "<<Sum;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/226258.html
標籤:其他
上一篇:Educational Codeforces Round 98 (Rated for Div. 2) E. Two Editorials 細節題
