'Quiz/etc.'에 해당되는 글 2건

import java.util.Scanner;


public class Main {

static Scanner in = new Scanner(System.in);

public static void main(String[] args){

//케이스횟수

int caseN = in.nextInt();

//각 케이스별 값

boolean[] caseT = new boolean[caseN];

for(int i=0 ; i<caseN ; i++){

//배열크기 N

int n = in.nextInt();

//테스트 후 결과값 저장

caseT[i] = test(n);

}

for(int i=0 ; i<caseN ; i++){

System.out.println(caseT[i]? "YES":"NO");

}

}

public static boolean test(int n){

in.nextLine();

String[] ai = in.nextLine().split(" ");

int sum = 0; //총 합

int leftSum = 0; //왼쪽 합

boolean sumCheck = false;

int[] arr = new int[n];

for(int i=0 ; i<n ; i++){

arr[i] = Integer.parseInt(ai[i]);

sum += arr[i]; 

}

//n번 돌거나 양쪽의 합이 같은 값이 될때까지 수행

for(int i=0 ; i<n && !sumCheck; i++){

//총합-(왼쪽+자신)=오른쪽 값이기 때문에 같지 않다면 leftSum에 자신을 더한다

if(sum-(leftSum+arr[i]) != leftSum){

leftSum += arr[i];

}else{

//값이 같기에 값을 참값으로 바꾼다

sumCheck = true;

}

}

return sumCheck;

}

}

'Quiz > etc.' 카테고리의 다른 글

정수쌍  (0) 2017.03.22
블로그 이미지

D.Story

,

정수쌍

Quiz/etc. 2017. 3. 22. 14:44

import java.util.HashSet;

import java.util.Scanner;

import java.util.Set;


public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String[] nk = in.nextLine().split(" ");

int n = Integer.parseInt(nk[0]);

int k = Integer.parseInt(nk[1]);

int count = 0;

String[] arrN = in.nextLine().split(" ");

// n개가 아닐때 정수를 다시 받음

while(arrN.length != n){

arrN = in.nextLine().split(" ");

}

//HashSet의 contains는 시간복잡도가 O(1)이기에 사용

Set<Integer> arr = new HashSet<Integer>();

// int형으로 변환하여 리스트에 넣음

for(String s : arrN){

arr.add(Integer.parseInt(s));

}

for(int i=0 ; i<arr.size() ; i++){

// i번째 숫자와 차가 k인  정수를 조회

if(arr.contains(Integer.parseInt(arrN[i])-k)){

System.out.println(Integer.parseInt(arrN[i]));

count++;

}

}

System.out.println(count);

}

}

'Quiz > etc.' 카테고리의 다른 글

양쪽의 합이 같은 경우 찾기  (0) 2017.03.22
블로그 이미지

D.Story

,