728x90

풀이)

import java.util.Scanner;
import java.io.FileInputStream;
 
/*
   사용하는 클래스명이 Solution 이어야 하므로, 가급적 Solution.java 를 사용할 것을 권장합니다.
   이러한 상황에서도 동일하게 java Solution 명령으로 프로그램을 수행해볼 수 있습니다.
 */
class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
 
 
        for(int i=1; i<=N; i++){
            int s = sc.nextInt();
            int sum = 0;
            int mus = 0;
            if(s%2==1){
                for(int j=1; j<=s; j++){
                    if(j%2==1){
                        sum += j;
                    } else if (j%2==0) {
                        mus += j;
                    }
                }
            }else{
                for(int j=1; j<=s; j++){
                    if(j%2==1){
                        sum += j;
                    } else if (j%2==0) {
                        mus += j;
                    }
                }
            }
            int answer = sum - mus;
            System.out.println("#"+i+ " " +answer);
        }
    }
}

입력받는 수의 홀수 값은 홀수 값끼리 더하고 짝수값은 짝수끼리 더한다음, 홀수의 합 - 짝수의 합을 빼주는 방법을 이용해서 문제를 풀었다.

728x90