"BOJ 2839 설탕 배달"의 두 판 사이의 차이

7번째 줄: 7번째 줄:
{{BOJ 2단계}}
{{BOJ 2단계}}
{{BOJ 단계 푸터}}
{{BOJ 단계 푸터}}
==C==
<source lang='c'>
#include<stdio.h>
int main() {
    int a;
    scanf("%d", &a);
    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 ) {
        printf("%d", -1);
        return 0;
    }
    printf("%d", big + small );
}
</source>


==Java==
==Java==

2018년 8월 4일 (토) 15:15 판

1 개요

BOJ 2839 설탕 배달

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

  • 나누기, 나머지 연산을 이용하는 문제
  • 알고리즘 분류: 수학, 구현
BOJ 단계별로 풀어보기
순번 문제 풀이

틀:BOJ 2단계 틀:BOJ 단계 푸터

2 C

#include<stdio.h>
int main() {
    int a;
    scanf("%d", &a);
    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 ) {
        printf("%d", -1);
        return 0;
    }
    printf("%d", big + small );
}

3 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 );
    }
}

4 Perl

use 5.010; no warnings 'experimental';
my $n = int(<>);
my ($small, $big, $q) = (0, $n/5, $n%5);

given ( int($q) ) {
    when ($_ eq 1) { $big--; $small = 2; }
    when ($_ eq 2) { $big -= 2; $small = 4; }
    when ($_ eq 3) { $small = 1; }
    when ($_ eq 4) { $big--; $small = 3; }
}

die("-1\n") if ($big < 0 || $small < 0);
say $big + $small;

5 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;

6 Python

n = int(input())
big = n // 5
q = n % 5
small = 0

if q == 1:
    big -= 1
    small = 2
elif q == 2:
    big -= 2
    small = 4
elif q == 3:
    small = 1
elif q == 4:
    big -= 1
    small = 3

if big < 0 or small < 0:
    print( -1 )
else:
    print( big + small )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}