paizaオンラインハッカソン Vol.4 Lite「エンジニアでも恋がしたい!」をいろんな言語で解いてみた
応募期間終わりましたし、もう大丈夫だよね……
マンガ版「エンジニアでも恋がしたい!」〜転職初日にぶつかった女の子が同僚だった件〜|paizaオンラインハッカソン4 Litepaizaさんのいつもおアレを、今回はいろんな言語で解いてみました。
「全部」じゃないのは横着したからだったりなかったり……
使用した言語
- Ruby
- Bash ※ワンライナー
- COBOL
- Python ※初めて書いた
- C ※初めて書いた
- Java
- Scala
- JavaScript ※Node.jsで書くのは初めて
- C#
ロゼッタストーンみたくしていただければと。
Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
count = gets.to_i | |
sum = 0 | |
count.times{ | |
sum = sum + gets.to_i | |
} | |
print sum, "\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
count = gets.to_i | |
sum = 0 | |
count.times{ | |
line = gets.chomp.split(" ") | |
t = line[0].to_i | |
s = line[1].to_i | |
p = line[2].to_i | |
items = t - s | |
if items > 0 | |
sum = sum + items * p | |
end | |
} | |
print sum, "\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
line = gets.chomp.split(" ") | |
range = line[0].to_i | |
count = line[1].to_i | |
max = 0 | |
sum = 0 | |
tmp = [] | |
count.times do |i| | |
n = gets.to_i | |
tmp[i] = n | |
sum = sum + n | |
if i >= range | |
sum = sum - tmp[i - range] | |
end | |
if sum > max | |
max = sum | |
end | |
end | |
print max, "\n" |
Bashワンライナー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk 'NR>1{sum = sum + $0}END{print sum}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk 'NR>1{items=($1 - $2);if(items>0) sum = sum + items * $3}END{print sum}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk 'NR==1{range=$1}NR>1{sum=sum+$1-tmp[NR];tmp[NR+range]=$1;if(sum>max)max=sum}END{print max}' |
※適宜改行を入れるととこんな感じ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk ' | |
NR==1 { | |
range = $1 | |
} | |
NR>1 { | |
sum = sum + $1 - tmp[NR]; | |
tmp[NR+range]=$1; | |
if(sum>max) max=sum | |
} | |
END { | |
print max | |
}' |
COBOL
計算結果をトリミングして出力するところが一番難しかった。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDENTIFICATION DIVISION. | |
PROGRAM-ID. answer-1. | |
DATA DIVISION. | |
WORKING-STORAGE SECTION. | |
01 INPUT-AREA. | |
03 INPUT-REC PIC X(15). | |
03 INPUT-01 PIC 9(9). | |
01 OUTPUT-AREA. | |
03 OUTPUT-REC PIC Z(8)9. | |
03 OUT-LEN PIC 9(9) VALUE 9. | |
03 OUT-START PIC 9(9). | |
03 OUT-END PIC 9(9). | |
01 WORK-AREA. | |
03 WK-COUNT PIC 9(9). | |
03 WK-SUM PIC 9(9). | |
*> | |
PROCEDURE DIVISION. | |
MAIN-RTN. | |
PERFORM READ-RTN. | |
MOVE INPUT-01 TO WK-COUNT. | |
PERFORM WK-COUNT TIMES | |
PERFORM READ-RTN | |
COMPUTE WK-SUM = WK-SUM + INPUT-01 | |
END-PERFORM. | |
MOVE WK-SUM TO OUTPUT-REC. | |
PERFORM OUTPUT-RTN. | |
EXIT PROGRAM. | |
STOP RUN. | |
*> | |
READ-RTN SECTION. | |
ACCEPT INPUT-REC. | |
UNSTRING INPUT-REC DELIMITED BY " " INTO INPUT-01. | |
EXIT. | |
*> | |
OUTPUT-RTN SECTION. | |
INSPECT OUTPUT-REC TALLYING OUT-START FOR ALL " ". | |
COMPUTE OUT-END = OUT-LEN - OUT-START. | |
COMPUTE OUT-START = OUT-START + 1. | |
DISPLAY OUTPUT-REC(OUT-START:OUT-END). | |
EXIT. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDENTIFICATION DIVISION. | |
PROGRAM-ID. answer-2. | |
DATA DIVISION. | |
WORKING-STORAGE SECTION. | |
01 INPUT-AREA. | |
03 INPUT-REC PIC X(15). | |
03 INPUT-01 PIC 9(9). | |
03 INPUT-02 PIC 9(9). | |
03 INPUT-03 PIC 9(9). | |
01 OUT-AREA. | |
03 OUTPUT-REC PIC Z(10)9. | |
03 OUT-LEN PIC 9(9) VALUE 11. | |
03 OUT-START PIC 9(9). | |
03 OUT-END PIC 9(9). | |
01 WORK-AREA. | |
03 WK-COUNT PIC 9(9). | |
03 WK-SUM PIC 9(9). | |
03 WK-ITEMS PIC S9(9). | |
*> | |
PROCEDURE DIVISION. | |
MAIN-RTN. | |
PERFORM READ-RTN. | |
MOVE INPUT-01 TO WK-COUNT. | |
PERFORM WK-COUNT TIMES | |
PERFORM READ-RTN | |
COMPUTE WK-ITEMS = INPUT-01 - INPUT-02 | |
IF WK-ITEMS > 0 | |
COMPUTE WK-SUM = WK-SUM + INPUT-03 * WK-ITEMS | |
END-IF | |
END-PERFORM. | |
MOVE WK-SUM TO OUTPUT-REC. | |
PERFORM OUTPUT-RTN. | |
EXIT PROGRAM. | |
STOP RUN. | |
*> | |
READ-RTN SECTION. | |
ACCEPT INPUT-REC. | |
UNSTRING INPUT-REC DELIMITED BY " " INTO | |
INPUT-01 INPUT-02 INPUT-03. | |
EXIT. | |
*> | |
OUTPUT-RTN SECTION. | |
INSPECT OUTPUT-REC TALLYING OUT-START FOR ALL " ". | |
COMPUTE OUT-END = OUT-LEN - OUT-START. | |
COMPUTE OUT-START = OUT-START + 1. | |
DISPLAY OUTPUT-REC(OUT-START:OUT-END). | |
EXIT. |
※COBOLのQ3はテストケース3が通らなかったorz なぜだ……
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDENTIFICATION DIVISION. | |
PROGRAM-ID. answer-3. | |
DATA DIVISION. | |
WORKING-STORAGE SECTION. | |
01 INPUT-AREA. | |
03 INPUT-REC PIC X(15). | |
03 INPUT-01 PIC 9(9). | |
03 INPUT-02 PIC 9(9). | |
01 OUT-AREA. | |
03 OUTPUT-REC PIC Z(12)9. | |
03 OUT-LEN PIC 9(9) VALUE 13. | |
03 OUT-START PIC 9(9). | |
03 OUT-END PIC 9(9). | |
01 WORK-AREA. | |
03 WK-III PIC 9(6). | |
03 WK-JJJ PIC 9(6). | |
03 WK-COUNT PIC 9(6). | |
03 WK-SUM PIC 9(10). | |
03 WK-MAX PIC 9(10). | |
03 WK-RANGE PIC 9(9). | |
03 WK-TMP PIC 9(9) OCCURS 300000 TIMES. | |
*> | |
PROCEDURE DIVISION. | |
MAIN-RTN. | |
PERFORM READ-RTN. | |
MOVE INPUT-01 TO WK-RANGE. | |
MOVE INPUT-02 TO WK-COUNT. | |
PERFORM WK-COUNT TIMES | |
PERFORM READ-RTN | |
ADD 1 TO WK-III | |
MOVE INPUT-01 TO WK-TMP(WK-III) | |
ADD INPUT-01 TO WK-SUM | |
IF WK-III > WK-RANGE | |
COMPUTE WK-JJJ = WK-III - WK-RANGE | |
COMPUTE WK-SUM = WK-SUM - WK-TMP(WK-JJJ) | |
END-IF | |
IF WK-SUM > WK-MAX | |
MOVE WK-SUM TO WK-MAX | |
END-IF | |
END-PERFORM. | |
MOVE WK-MAX TO OUTPUT-REC. | |
PERFORM OUTPUT-RTN. | |
EXIT PROGRAM. | |
STOP RUN. | |
*> | |
READ-RTN SECTION. | |
ACCEPT INPUT-REC. | |
UNSTRING INPUT-REC DELIMITED BY " " INTO | |
INPUT-01 INPUT-02. | |
EXIT. | |
*> | |
OUTPUT-RTN SECTION. | |
INSPECT OUTPUT-REC TALLYING OUT-START FOR ALL " ". | |
COMPUTE OUT-END = OUT-LEN - OUT-START. | |
COMPUTE OUT-START = OUT-START + 1. | |
DISPLAY OUTPUT-REC(OUT-START:OUT-END). | |
EXIT. |
Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
count = int(raw_input()) | |
sum = 0 | |
for i in xrange(count): | |
n = int(raw_input()) | |
sum = sum + n | |
print sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
count = int(raw_input()) | |
sum = 0 | |
for i in xrange(count): | |
line = raw_input().split(" ") | |
t = int(line[0]) | |
s = int(line[1]) | |
p = int(line[2]) | |
items = t - s | |
if items > 0: | |
sum = sum + items * p | |
print sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
line = raw_input().split(" ") | |
rang = int(line[0]) | |
count = int(line[1]) | |
sum = 0 | |
max = 0 | |
tmp = [] | |
for i in xrange(count): | |
n = int(raw_input()) | |
tmp.append(n) | |
sum = sum + n | |
if i >= rang: | |
sum = sum - tmp[i - rang] | |
if sum > max: | |
max = sum | |
print max |
C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main() { | |
char buf[15], *token1; | |
int i, count, n, sum; | |
sum = 0; | |
fgets(buf, sizeof(buf), stdin); | |
count = atoi(buf); | |
for (i=0; i<count; i++) { | |
fgets(buf, sizeof(buf), stdin); | |
token1 = strtok(buf, " "); | |
n = atoi(token1); | |
sum = sum + n; | |
} | |
printf("%d\n", sum); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main() { | |
char buf[15], *token1, *token2, *token3; | |
int i, count, sum; | |
int t, s, p, items; | |
sum = 0; | |
fgets(buf, sizeof(buf), stdin); | |
count = atoi(buf); | |
for (i=0; i<count; i++) { | |
fgets(buf, sizeof(buf), stdin); | |
token1 = strtok(buf, " "); | |
token2 = strtok(NULL, " "); | |
token3 = strtok(NULL, " "); | |
t = atoi(token1); | |
s = atoi(token2); | |
p = atoi(token3); | |
items = t - s; | |
if (items > 0) { | |
sum = sum + items * p; | |
} | |
} | |
printf("%d\n", sum); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main() { | |
char buf[15], *token1, *token2; | |
int i, range, count; | |
long max, sum; | |
int n, tmp[300000]; | |
sum = 0; | |
max = 0; | |
fgets(buf, sizeof(buf), stdin); | |
token1 = strtok(buf, " "); | |
token2 = strtok(NULL, " "); | |
range = atoi(token1); | |
count = atoi(token2); | |
for (i=0; i<count; i++) { | |
fgets(buf, sizeof(buf), stdin); | |
n = atoi(buf); | |
tmp[i] = n; | |
sum = sum + n; | |
if (i>=range) sum = sum - tmp[i-range]; | |
if (sum > max) max = sum; | |
} | |
printf("%ld\n", max); | |
return 0; | |
} |
Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int count = sc.nextInt(); | |
long sum = 0; | |
for (int i=0; i<count; i++) { | |
int n = sc.nextInt(); | |
sum = sum + n; | |
} | |
System.out.println(sum); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int count = sc.nextInt(); | |
long sum = 0; | |
for (int i=0; i<count; i++) { | |
int t = sc.nextInt(); | |
int s = sc.nextInt(); | |
int p = sc.nextInt(); | |
int items = t -s; | |
if (items > 0) { | |
sum = sum + items * p; | |
} | |
} | |
System.out.println(sum); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int range = sc.nextInt(); | |
int count = sc.nextInt(); | |
long sum = 0; | |
long max = 0; | |
Map<Integer, Integer> tmp = new HashMap<Integer, Integer>(300000); | |
for (int i=0; i<count; i++) { | |
int n = sc.nextInt(); | |
tmp.put(i, n); | |
sum = sum + n; | |
if (i >= range) { | |
sum = sum - tmp.get(i-range); | |
} | |
if (sum > max) { | |
max = sum; | |
} | |
} | |
System.out.println(max); | |
} | |
} |
Scala
とてもJavaっぽいスタイル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
val sc = new java.util.Scanner(System.in) | |
val count = sc.nextInt | |
var sum = 0 | |
for (i <- 1 to count) { | |
val n = sc.nextInt | |
sum = sum + n | |
} | |
println(sum) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
val sc = new java.util.Scanner(System.in) | |
val count = sc.nextInt | |
var sum = 0 | |
for (i <- 1 to count) { | |
val t = sc.nextInt | |
val s = sc.nextInt | |
val p = sc.nextInt | |
val items = t - s | |
if (items > 0) sum = sum + items * p | |
} | |
println(sum) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
val sc = new java.util.Scanner(System.in) | |
val range = sc.nextInt | |
val count = sc.nextInt | |
var sum : Long = 0 | |
var max : Long = 0 | |
var tmp = scala.collection.mutable.Map.empty[Int, Int] | |
for (i <- 1 to count) { | |
val n = sc.nextInt | |
tmp.put(i, n) | |
sum = sum + n | |
if (i > range) sum = sum - tmp(i-range) | |
if (sum > max) max = sum | |
} | |
println(max) | |
} |
JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('data', function (chunk) { | |
var lines = chunk.toString().split('\n'); | |
var count = lines[0]; | |
var sum = 0; | |
for(var i=1; i<=count; i++) { | |
var line = lines[i].split(" "); | |
sum = sum + parseInt(line[0]); | |
} | |
console.log(sum); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('data', function (chunk) { | |
var lines = chunk.toString().split('\n'); | |
var count = lines[0]; | |
var sum = 0; | |
for(var i=1; i<=count; i++) { | |
var line = lines[i].split(" "); | |
var t = parseInt(line[0]); | |
var s = parseInt(line[1]); | |
var p = parseInt(line[2]); | |
var items = t - s; | |
if (items >0) { | |
sum = sum + items * p; | |
} | |
} | |
console.log(sum); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var text = ""; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('data', function (chunk) { | |
text = text + chunk.toString(); | |
}); | |
process.stdin.on('end', function () { | |
main(text); | |
}); | |
function main(data) { | |
var lines = data.split('\n'); | |
var line = lines[0].split(" "); | |
var range = parseInt(line[0]); | |
var count = parseInt(line[1]); | |
var sum = 0; | |
var max = 0; | |
var tmp = {}; | |
for(var i=1; i<=count; i++) { | |
var n = parseInt(lines[i]); | |
tmp[i] = n; | |
sum = sum + n; | |
if (i > range) sum = sum - tmp[i-range]; | |
if (sum > max) max = sum; | |
} | |
console.log(max); | |
} |
※ファイルサイズが大きいとchunkで切れてしまうので、一旦全部結合してから処理をしています
C#
飽きてきたので変なことをしている…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
public class Hello{ | |
public static void Main(){ | |
ProcessStartInfo psi = new ProcessStartInfo(); | |
psi.FileName = "awk"; | |
psi.Arguments = "'NR>1{sum = sum + $0}END{print sum}'"; | |
psi.CreateNoWindow = true; | |
psi.UseShellExecute = false; | |
psi.RedirectStandardInput = false; | |
psi.RedirectStandardOutput = true; | |
Process p = Process.Start(psi); | |
String outputString = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
Console.Write(outputString); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
public class Hello{ | |
public static void Main(){ | |
ProcessStartInfo psi = new ProcessStartInfo(); | |
psi.FileName = "awk"; | |
psi.Arguments = "'NR>1{items=($1 - $2);if(items>0) sum = sum + items * $3}END{print sum}'"; | |
psi.CreateNoWindow = true; | |
psi.UseShellExecute = false; | |
psi.RedirectStandardInput = false; | |
psi.RedirectStandardOutput = true; | |
Process p = Process.Start(psi); | |
String outputString = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
Console.Write(outputString); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
public class Hello{ | |
public static void Main(){ | |
ProcessStartInfo psi = new ProcessStartInfo(); | |
psi.FileName = "awk"; | |
psi.Arguments = "'NR==1{range=$1}NR>1{sum=sum+$1-tmp[NR];tmp[NR+range]=$1;if(sum>max)max=sum}END{print max}'"; | |
psi.CreateNoWindow = true; | |
psi.UseShellExecute = false; | |
psi.RedirectStandardInput = false; | |
psi.RedirectStandardOutput = true; | |
Process p = Process.Start(psi); | |
String outputString = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
Console.Write(outputString); | |
} | |
} |