Hoj _ sum of squares and cubes and

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.