HDU 2007 平方和与立方和

题目链接:HDU 2007

Description

给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。

Input

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

Output

对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。

Sample Input

1 3
2 5

Sample Output

4 28
20 152

代码

import java.util.*;
class  Main
{
	
	public static void main(String[] args) 
	{
		int  ans1,ans2;
		Scanner read=new Scanner(System.in);
		int l,r;
		while(read.hasNext()){
			ans1=0;
			ans2=0;
			l=read.nextInt();
			r=read.nextInt();
			if(r<l){
				int t=r;
				r=l;
				l=t;
			}
			for(int i=l;i<=r;i++){
				if(i%2==1)
					ans1+=i*i*i;
				else ans2+=i*i;
			}
			System.out.print(ans2+" "+ans1);
			System.out.println();
	    }
     }
}
posted @ 2018-09-21 14:57  Titordong  阅读(150)  评论(0编辑  收藏  举报