杭电oj~~2007

这里写的有点复杂了,不过还好AC了,重要的有一点,别忘了判断输入两个数的大小

题目描述:

平方和与立方和

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


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

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

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

Sample Input

1 3 2 5
 

Sample Output

4 28 20 152
 
AC代码:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			long x=0,y=0;
			int a,b;
			int c[] = new int[1000005];
			a = in.nextInt();
			b = in.nextInt();
			if(a>b)
			{
				int temp = a;
				a =b;
				b = temp;
			}
			for(int i=a;i<=b;i++)
			{
				c[i] = i;
				if(c[i]%2==0)
				{
					x = x+c[i]*c[i];
				}
				else
				{
					y = y+c[i]*c[i]*c[i];
				}
			}
			System.out.println(x+" "+y);
		}

	}

}


你可能感兴趣的:(oj小题)