BOJ 2839 설탕 배달

1 개요[ | ]

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

2 Bash[ | ]

read n
small=0
big=`expr $n / 5`
q=`expr $n % 5`

if [ $q -eq 1 ]; then
        big=`expr $big - 1`
        small=2
elif [ $q -eq 2 ]; then
        big=`expr $big - 2`
        small=4
elif [ $q -eq 3 ]; then
        small=1
elif [ $q -eq 4 ]; then
        big=`expr $big - 1`
        small=3
fi

if [ $big -lt 0 -o $small -lt 0 ]; then
        echo "-1" && exit
fi
expr $big + $small

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

4 C++[ | ]

#include <iostream>
using namespace std;
int main() {
    int a;
    cin >> 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 ) {
        cout << -1;
        return 0;
    }
    cout << big + small;
}

5 C#[ | ]

using System;
public class Program {
    public static void Main() {
        int a = int.Parse(Console.ReadLine());
        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 ) {
            Console.WriteLine( -1 );
            return;
        }
        Console.WriteLine( big + small );
    }
}

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

7 node.js[ | ]

var a = require('fs').readFileSync('/dev/stdin');
var big = Math.floor(a/5)+1;
while( --big >= 0 ) {
    remain = a - big * 5;
    if( remain%3 == 0 ) {
        console.log(big + remain / 3);
        break;
    }
}
if(big<0)console.log(-1);
var fs = require('fs');
var a = fs.readFileSync('/dev/stdin');
var big = Math.floor(a / 5);
var q = a % 5;
var 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 ) {
    console.log( -1 );
}
else {
    console.log( big + small );
}

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

9 PHP[ | ]

<?php
fscanf(STDIN,"%d",$n);
if( $n == 4 || $n == 7 ) {
    echo -1;
    exit;
}
echo intdiv($n,5) + [0,1,2,1,2][$n%5];
<?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;

10 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 )

11 Ruby[ | ]

n = gets.to_i
if( n == 4 || n == 7 )
    print -1
    exit
end
print n/5 + [0,1,2,1,2][n%5]
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}