Sum of squares and cubes and
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 93160 accepted submission (s): 29855
Problem description gives a continuous integer and calculates the sum of squares of all even numbers and the sum of all odd numbers.
The input data contains multiple groups of test instances. Each group of test instances contains a row consisting of two integers, M and N.
Output for each group of input data, the output line should contain two integers x and y, indicating the sum of the squares of all the even numbers in the continuous integer of the segment and the sum of all the odd numbers of cubes and.
You can think that a 32-bit integer is enough to save the result.
Sample input1 32 5
Sample output4 2820 152 C language code
1 #include<stdio.h> 2 int main(){ 3 int n,m,pf,lf,t; 4 while(scanf("%d %d",&n,&m)!=EOF){ 5 if(n>m) { 6 t=n; n=m; m=t; 7 } 8 pf=0;lf=0; 9 for(;n<=m;n++){10 if(n%2==0) pf+=n*n;11 else lf+=n*n*n;12 }13 printf("%d %d\n",pf,lf);14 }15 }
Java code
import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int n,m,pf,lf,t; while(s.hasNext()){ pf=0;lf=0; n=s.nextInt();m=s.nextInt(); if(n>m) { t=n; n=m;m=t; } for(;n<=m;n++){ if(n%2==0) pf+=n*n; else lf+=n*n*n; } System.out.format("%d %d", pf,lf).println(); } }}
Consider only two points:
1. Is it an odd or even number;
2. The relationship between two numbers. If the former is large, the latter is small, and the two values are exchanged.
Although both are correct, the running time and memory are significantly different.
Writer: hruinger
Hoj _ sum of squares and cubes and