C
Modified I/O sample from kattis help pages
#include <stdio.h>
int main(void) {
char str[1000];
while (fgets(str, 1000, stdin)) { //Or scanf
printf("%s", &str);
}
return 0;
}
C++
- Language Reference
- Modified I/O sample from kattis help pages
#include <iostream>
int main(void) {
long long data;
while (std::cin >> data)
std::cout << data << std::endl;
return 0;
}
Java
- Official Java Documentation
- Unofficial Java reference
- I/O sample from kattis help pages
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLong()) {
long data = sc.nextLong();
System.out.println(data);
}
}
}
JavaScript (NodeJs)
I/O sample from kattis help pages
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (line) => {
let data = parseInt(line);
console.log(data);
});
Octave
- Language reference
- Solution to simple problem.
[name,count,read,err]=scanf("%s %d","C");
for j = 1:count(1,1)
printf("Hipp hipp hurra, %s!\n", name);
endfor
Python
- Local mirror of python documentation
- I/O sample from kattis help pages
#! /usr/bin/python3
import sys
for line in sys.stdin:
data = int(line)
print(data)
Racket
If you use
racket
for programming contests, you should use#lang racket
, not one of the teaching languages.For language reference, run
raco doc
from the command line.Sample solution to a slightly more complicated problem. This one has formatted output and reading individual lines of input.
Code snippets showing input/output in racket
#lang racket
;; read stdin line by line, and output each line uppercased with a
;; line number
(define (upcase-stdin)
(for ([line (in-lines (current-input-port))]
[count (in-naturals)])
(printf "~a: ~a~n" count (string-upcase line))))
;; read stdin into a list of numbers, return the list
(define (read-numbers)
(for/list ([line (in-lines (current-input-port))])
(string->number line)))
;; read stdin line by line, splitting into words.
(define (read-words)
(for/list ([line (in-lines (current-input-port))])
(string-split line)))