First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Wednesday, July 16, 2014

Odd Sum.

Author : Jisan
Programming language : Java
Difficulty : Easy
Status : Accepted
Problem number : UVA-10783
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1724

Code:

import java.util.Scanner;
public class Uva_10783Demo {
    public static void main(String args[]){
    Scanner input=new Scanner(System.in);
    int i=input.nextInt();
    for(int k=1;k<=i;k++){
    int a=input.nextInt();
    int b=input.nextInt();
    System.out.printf("Case %d: %d\n",  k, OddSum(a, b));
    }
    }
    public static int OddSum(int a,int b){
     if (a % 2 == 0)
           a++;
 
       if (b % 2 == 0) {
           b--;
       }
       int i = (b - a + 2) / 2;
       return i * (a + (a + 2 * (i - 1))) / 2;
    }
}

No comments:

Post a Comment