To emulate how Kattis gives your program input locally, first run it in a terminal. Then, after starting the program, paste the input (for example, one of the samples) and press enter. What your program outputs to the terminal after this is the output Kattis receives. Note that you must print exactly what Kattis wants: nothing more, nothing less. Any debug prints will result in a Wrong Answer verdict.
In more exact terms, your program should always read/write to STDIN/STDOUT.
Below follows some examples of how to handle input and output for the Kattis problem N-sum in different programming languages.
Python 3:
n = int(input()) # Not needed since all numbers are on the same line answer = 0 line = input() for number in line.split(): answer += int(number) print(answer)
C++:
#include <iostream>
int main() {
int n;
std::cin >> n;
int answer = 0;
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
answer += x;
}
std::cout << answer << std::endl;
return 0;
}Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/* Note that scanner can be very slow on large inputs. Consider using a buffered reader such as the KattIO library if the problem has large inputs*/
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int answer = 0;
for (int i = 0; i < n; i++) {
answer += sc.nextInt();
}
System.out.println(answer);
}
}C:
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int sum = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%dd", &x);
sum += x;
}
printf("%d\n", sum);
return 0;
}C#:
using System;
class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
var input = Console.ReadLine().Split(' ');
int sum = 0;
foreach (String s in input)
{
sum += int.Parse(s);
}
Console.WriteLine(sum);
}
}Rust:
use std::io;
fn main() {
let input = io::read_to_string(io::stdin()).unwrap();
let mut iter = input.split_whitespace();
let n: usize = iter.next().unwrap().parse().unwrap();
let mut answer = 0;
for _ in 0..n {
let num: i32 = iter.next().unwrap().parse().unwrap();
answer += num;
}
println!("{answer}");
}JavaScript on Node.js:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let lineCount = 0;
rl.on('line', line => {
lineCount++;
if (lineCount === 2) {
let answer = 0;
line.split(' ').map(Number).forEach(x => {
answer += x;
});
console.log(answer);
}
});TypeScript is similar:
import * as readline from 'readline';
const rl: readline.Interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let lineCount: number = 0;
rl.on('line', (line: string): void => {
lineCount++;
if (lineCount === 2) {
let answer: number = 0;
line.split(' ').map(Number).forEach(x => {
answer += x;
});
console.log(answer);
}
});JavaScript on Node.js (alternative). Another way is to read the entire input at once. To run this locally, you can't enter input manually, but must instead supply it by piping a file.
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin', 'utf8').trim().split('\n');
currentLine = 0;
const readLine = function() {
return input[currentLine++];
}
const n = readLine();
const numbers = readLine().split(' ').map(Number);
let answer = 0;
numbers.forEach(x => {
answer += x;
});
console.log(answer);Swift:
_ = readLine() // We don't need N; we read the entire next line
let line = readLine()!
let nums = line.split(separator: " ").map { Int($0)! }
print(nums.reduce(0, +))PHP:
<?php
fscanf(STDIN, "%d\n", $N);
echo array_sum(fscanf(STDIN, str_repeat("%d ", $N)));APL:
N←⍞ ⍝ We don't need N in APL S←⍞ out←+/⍎¨S⊂⍨(S≠' ')∧1,¯1↓S=' ' ⎕←out
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article