2007平方和与立方和c++

Problem Description
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
Sample Input
1 3
2 5
Sample Output
4 28
20 152

#include<iostream>
using namespace std;
int main()
{
	int m, n;
	int x, y;
	while (cin >> m >> n)
	{
		if (m>n)
		{
			int a = 0;
			a = n;
			n = m;
			m = a;
		}
		x = 0;
		y = 0;
		for (int i = m; i <= n; i++)
		{
			if (i % 2 == 1)
			{
				y = y + i * i*i;
			}
			else
			{
				x = x + i * i;
			}
		}
		cout << x << ' ' << y << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41274723/article/details/89499968