백준/구현

2920 음계

have a good time 2021. 10. 18. 20:05

https://www.acmicpc.net/problem/2920

 

2920번: 음계

다장조는 c d e f g a b C, 총 8개 음으로 이루어져있다. 이 문제에서 8개 음은 다음과 같이 숫자로 바꾸어 표현한다. c는 1로, d는 2로, ..., C를 8로 바꾼다. 1부터 8까지 차례대로 연주한다면 ascending, 8

www.acmicpc.net

 

 

 

아래 코드 처럼 말도 안되는 코드를 작성해서는 안된다.

 

for(int i=0; i<8;i++) {
	    		if(input[i]==a[i]) {
	    			System.out.println( "ascending");
	    			
	    		}else if(input[i]==b[i]) {
	    			System.out.println( "descending");
	    			
	    		}else  {
	    			System.out.println( "mixed");
		    		
	    		}}

나는 a[]={1,2,3,4,5,6,7,8} 이라고 하고, b[]={8,7,6,5,4,3,2,1}이라고 했다

예를 들어서 입력값이 81726354 라면,

input={8,1,7,2,6,3,5,4}

 

1)

i=0일때, 

b[0]=input[0] 이므로, descending 을 출력한다.

 

2)

i=1일때, input[1]!=a[1] , input[1]!=b[1] 이므로 mixed가 출력될 것이고....

이런식인거다..

 

만약 System.out.println 아래 줄에 break 를 걸어주면, input[0] 만 a[0], b[0]이랑 비교하고 끝내는거고,

continue를 걸어주면, i=1로 이동해서, System.out.println 하는 거니깐 의미 없음 

 

 

 

 

 

 

 

 

 

 

최종으로 맞은 코드

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;




public class Main {

	
	    public static void main(String[] args) throws IOException {

	    	
	    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	 
	    
	    	int input[] = new int [8];
	    	String s = br.readLine();
	    	
	    	StringTokenizer st = new StringTokenizer(s , " ");
	    	
	    	for(int i=0; i<8;i++){
	    		input[i]=Integer.parseInt(st.nextToken());
	    	}
	    	
	    	String result = "mixed";
	    	
	    	
	    	
	    	for(int i=0; i<7;i++) {
	    		if( input[i+1]==input[i]+1) {
	    		result = "ascending";
	    			
	    		}else if( input[i+1]==input[i]-1) {
	    			result =  "descending";
	    			
	    		}else  {
	    			result = "mixed";
		    	break;
	    		}}
	    		
	    	System.out.println(result);
	    			
	    
	    	
	    	}	}

 

 

참고 자료 : https://oper6210.tistory.com/23

 

[JAVA 자바] 백준 2920번 음계

이 문제는 spilt를 사용해 입력한 숫자를 구분해줘도 되고 int형 배열에 바로 입력 값을 저장 해도 되는 문제이다. 첫 번째는 int형 배열에 입력 값을 저장 하여 값을 구하는 방법이다. 두 번째는 spi

oper6210.tistory.com

 

 

 

 

** 생각해 볼 부분

 

위의 코드처럼 System.out.println을 맨 마지막에 하지 않고, 각 경우 마다 하면

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;




public class Main {

	
	    public static void main(String[] args) throws IOException {

	    	
	    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	 
	    
	    	int input[] = new int [8];
	    	String s = br.readLine();
	    	
	    	StringTokenizer st = new StringTokenizer(s , " ");
	    	
	    	for(int i=0; i<8;i++){
	    		input[i]=Integer.parseInt(st.nextToken());
	    	}
	    	
	   
	    	
	    	
	    	
	    	for(int i=0; i<7;i++) {
	    		if( input[i+1]==input[i]+1) {
	    			System.out.println("ascending"); 
	    			
	    		}else if( input[i+1]==input[i]-1) {
	    		
	    			System.out.println("descending"); 
	    			
	    		}else  {
	    			System.out.println("mixed"); 
	    		
		    	break;
	    		}}
	    		
	    
	    			
	    
	    	
	    	}	}

오류가 난다.

 

생각해볼 부분