BOJ 2839 설탕 배달

Jmnote (토론 | 기여)님의 2018년 7월 26일 (목) 16:57 판 (→‎Java)

1 개요

BOJ 2839 설탕 배달

[[분류:BOJ {{{단계}}}단계]]

  • 나누기, 나머지 연산을 이용하는 문제
  • 알고리즘 분류: 수학, 구현

2 Java

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        
        int big = a / 5;
        int q = a % 5;
        
        int small = 0;
        switch( q ) {
            case 1: big--; small = 2; break;
            case 2: big -= 2; small = 4; break;
            case 3: small = 1; break;
            case 4: big--; small = 3; break;
        }
        if( big < 0 || small < 0 ) {
            System.out.println( -1 );
            return;
        }
        System.out.println( big + small );
    }
}

3 PHP

<?php
fscanf(STDIN,"%d",$n);
$big = floor( $n / 5 );
$q = $n % 5;
$small = 0;
switch( $q ) {
case 1: $big--; $small=2; break;
case 2: $big-=2; $small=4; break;
case 3: $small=1; break;
case 4: $big--; $small=3; break;
}
if($big<0 || $small<0) {
    echo -1;
    exit;
}
echo $big+$small;
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}