BOJ 2920 음계

1 개요[ | ]

BOJ 2920 음계
  • 주어진 배열이 오름차순인지 아닌지 판단하는 문제
  • 알고리즘 분류: 배열

2 Java[ | ]

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int[] nums = new int[8];
        for(int i=0; i<8; i++) {
            nums[i] = sc.nextInt();
        }
        //System.out.println( Arrays.toString(nums) );

        String prev_state;
        if( nums[0] < nums[1] ) prev_state = "ascending";
        else prev_state = "descending";

        String state = "";
        for(int i=1; i<7; i++) {
            if( nums[i] < nums[i+1] ) state = "ascending";
            else state = "descending";
            if( prev_state != state ) {
                state = "mixed";
                break;
            }
        }
        System.out.println( state );
    }
}

3 Perl[ | ]

@nums = split / +/, <>;
$state = "";
$prev_state = "descending";
$prev_state = "ascending" if ($nums[0] < $nums[1]);

for (1..6) {
    $state = "descending";
    $state = "ascending" if ($nums[$_] < $nums[$_+1]);
    $state = "mixed" if ($prev_state ne $state);
    last if ($state eq "mixed");
}
printf("%s\n", $state);

4 PHP[ | ]

<?php
$nums = rtrim(fgets(STDIN));
if( $nums == '1 2 3 4 5 6 7 8') die('ascending');
if( $nums == '8 7 6 5 4 3 2 1') die('descending');
echo 'mixed';
<?php
$nums = explode(' ', rtrim(fgets(STDIN)));
$state = "";
$prev_state = ($nums[0]<$nums[1]) ? 'ascending' : 'descending';
for($i=1; $i<7; $i++) {
    $state = ($nums[$i]<$nums[$i+1]) ? 'ascending' : 'descending';
    if( $prev_state != $state ) {
        $state = 'mixed';
        break;
    }
}
echo $state;

5 Python[ | ]

nums = input()
if nums == "1 2 3 4 5 6 7 8":
	print("ascending")
elif nums == "8 7 6 5 4 3 2 1":
	print("descending")
else:
	print("mixed")
nums = input().split(" ")
if nums == sorted(nums):
    print("ascending")
elif nums == sorted(nums, reverse=True):
    print("descending")
else:
    print("mixed")
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}