Advertisement

c++L1-008 计算连续整数段的和(满分:10分)

阅读量:
复制代码
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main() {
    	//define and input
    	int a = -100;
    	int b = 100;
    	cin >> a >> b;
    	int sum = (a + b) * (b - a + 1) / 2;
    	
    	ios_base::fmtflags old= cout.setf(ios::right, ios::adjustfield);
    	//output
    	for (int i = a; i <= b; i++) {
    		cout  << setw(5) << i;
    		if ((i - a + 1) % 5 == 0) cout << endl;
    	}
    	cout.setf(old, ios::adjustfield);
    	if ((b - a + 1) % 5 != 0) cout << endl;
    cout << "Sum = " << sum ;
    	return 0;
    }
    
    
      
      
      
      

全部评论 (0)

还没有任何评论哟~