0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC 154 D Perlの恩返し

Last updated at Posted at 2020-02-11

はじめに

おっ、ABC 154やっとるな。
D - Dice in Line、Perlで問いたろ。

Perl

提出っ

main.pl
# !/usr/bin/perl

use strict;
use warnings;

my ($n, $k);
{
    my $in = <STDIN>;
    ($n, $k) = split(" ", $in);
}
my @p;
{
    my $in = <STDIN>;
    @p = split(" ", $in);
}
my $sum = 0;
for (my $i=0; $i<$k; $i++)
{
    $sum += $p[$i];
}
my $max = $sum;
if ($n>$k)
{
    for (my $i=0; $i<$n-$k+1; $i++)
    {
        $sum = $sum - $p[$i] + $p[$i+$k];
        $max = $sum if $max < $sum;
    }
}
$max = ($max + $k) / 2;
print "$max\n";
AC
どやっ
java でも問いたろっ

java

そりゃ

main.java
import java.util.*;

class Main {
    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);
        final int N = Integer.parseInt(sc.next());
        final int K = Integer.parseInt(sc.next());
        final int P[] = new int[N];
        for (int i = 0; i < N; i++) {
            P[i] = Integer.parseInt(sc.next());
        }
        sc.close();

        int sum = 0;
        for (int i = 0; i < K; i++) {
            sum += P[i];
        }
        int max = sum;
        if (N > K) {
            for (int i = 0; i < N - K + 1; i++) {
                sum = sum - P[i] + P[i + K];
                if (sum > max) {
                    max = sum;
                }
            }
        }
        double d = (max + K) / 2.0;

        System.out.println(d);
    }
}
java.lang.ArrayIndexOutOfBoundsException

ぐぬぬ

まとめ

誤    for (my $i=0; $i<$n-$k+1; $i++)
正    for (my $i=0; $i<$n-$k; $i++)
  • Perl の場合、未定義値undefは数値として比較された場合「0」として扱われる
  • java や ruby の場合、例外が発生する
  • c の場合、しれっととんでもない数値を返す

参照したサイト
undef関数 - 未定義値 - Perl入門ゼミ - Perlゼミのプログラミング ...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?