문제를 잘읽고 풀어야겠다는 생각을 심어준 문제이다. 제대로 풀었는데 왜 틀렸다고 나오는지 해맸는데 문제를 잘못 이해한것이다.
while문을 돌리며 cnt를 1씩 증가시켜 cnt가 홀수일때와 짝수일때를 나눠서 풀면 쉽게 해결할 수 있는 문제이다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int a=1; | |
int v1=1; | |
int cnt=1; | |
int result=1; | |
int input = sc.nextInt(); | |
while(true) { | |
if(input<=result) { | |
break; | |
} | |
cnt++; | |
if(cnt%2==0) { | |
v1 = input-result; | |
a=cnt-v1+1; | |
}else { | |
a= input-result; | |
v1=cnt-a+1; | |
} | |
for(int i=0; i<cnt; i++ ) { | |
result++; | |
} | |
} | |
System.out.printf("%d/%d\n",v1,a); | |
} | |
} | |
'백준 알고리즘 > JAVA' 카테고리의 다른 글
[JAVA 자바] 백준 1011번 Fly me to the Alpha Centauri (2) | 2018.07.12 |
---|---|
[JAVA 자바] 백준 1316번 그룹 단어 체커 (0) | 2018.07.11 |
[JAVA 자바] 백준 2292번 벌집 (0) | 2018.07.10 |
[JAVA 자바] 백준 2438번 별찍기-1 (0) | 2018.07.10 |
[JAVA 자바] 백준 1157번 단어 공부 (0) | 2018.07.10 |