(hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)

题目:

平方和与立方和

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 108212    Accepted Submission(s): 34915


Problem Description
给定一段连续的整数。求出他们中全部偶数的平方和以及全部奇数的立方和。


 

Input
输入数据包括多组測试实例,每组測试实例包括一行,由两个整数m和n组成。
 

Output
对于每组输入数据,输出一行,应包括两个整数x和y。分别表示该段连续的整数中全部偶数的平方和以及全部奇数的立方和。
你能够觉得32位整数足以保存结果。

 

Sample Input
1 3 2 5
 

Sample Output
4 28 20 152
 

Author
lcy
 

Source
 


题目分析:

              简单题。


代码例如以下:


/*
 * c.cpp
 *
 *  Created on: 2015年3月20日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <cmath>


using namespace std;


int main(){
	int a,b;
	while(scanf("%d%d",&a,&b)!=EOF){
		double ans_ji = 0;
		double ans_ou = 0;
		int i;

		//这道题须要注意一下的就是要考虑b<a的情况
		if(b < a){
			swap(a,b);
		}
		for(i = a ; i <= b ; ++i){
			if(i % 2 == 1){
				ans_ji += pow(i+0.0,3);
			}else{
				ans_ou += pow(i+0.0,2);
			}
		}

		printf("%d %d
",(int)ans_ou,(int)ans_ji);
	}

	return 0;
}






原文地址:https://www.cnblogs.com/bhlsheji/p/5149379.html