以下样例程序可用于解决这道简单的题目:读入2个整数A和B,然后输出它们的和。
awk (.awk)
{print $1 + $2}
bash (.sh)
while read i; do
echo $((${i/ /+}))
done
clang (.c)
#include <stdio.h>
int main()
{
int a, b;
while(scanf("%d %d",&a, &b) != EOF)
printf("%d
", a + b);
return 0;
}
clang++ (.cpp)
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)
cout << a+b << endl;
return 0;
}
clisp (.cl)
(loop for n = (read t nil nil)
while n
do (format t "~d~C" (+ n (read)) #linefeed))
fpc (.pas)
var
a, b: integer;
begin
while not eof(input) do begin
readln(a, b);
writeln(a + b);
end;
end.
gcc (.c)
#include <stdio.h>
int main()
{
int a, b;
while(scanf("%d %d",&a, &b) != EOF)
printf("%d
", a + b);
return 0;
}
g++ (.cpp)
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)
cout << a+b << endl;
return 0;
}
gccgo、go (.go)
package main
import "fmt"
func main() {
var a, b int
for {
n, _ := fmt.Scanf("%d %d", &a, &b)
if (n != 2) { break }
fmt.Println(a + b)
}
}
gcj (.gcj.java)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try {
while (in.hasNextInt()) {
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
} catch (NullPointerException ex) {
// gcj Scanner has a bug that throws NPE
;
}
}
}
ghc (.hs)
main = interact $ unlines . map (show . sum . map read . words) . lines
javac (.java)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
lua (.lua)
for a, b in io.read('*a'):gmatch('([%d-]+) ([%d-]+)') do
print(tonumber(a) + tonumber(b))
end
mcs (.cs)
public class Program {
public static void Main() {
string line;
while ((line = System.Console.ReadLine ()) != null) {
string[] tokens = line.Split();
System.Console.WriteLine(int.Parse(tokens[0]) + int.Parse(tokens[1]));
}
}
}
node (.js)
var fs = require('fs');
var buf = '';
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
buf.split('
').forEach(function(line) {
var tokens = line.split(' ').map(function(x) { return parseInt(x); });
if (tokens.length != 2) return;
console.log(tokens.reduce(function(a, b) { return a + b; }));
});
});
ocamlc (.ml)
try
while true do
Scanf.scanf " %d %d" (fun a b -> Printf.printf "%d
" (a + b))
done;
None
with
End_of_file -> None
;;
perl (.pl)
#!/usr/bin/perl -pla
$_ = $F[0] + $F[1]
php (.php)
<?php
while (fscanf(STDIN, "%d%d", $a, $b) == 2) {
print ($a + $b) . "
";
}
python2, 3 (.py) (注意python3应使用 input() 读输入)
import sys
for line in sys.stdin:
print(sum(map(int, line.split())))
racket (.rkt)
#lang racket
(define (f)
(let ([n (read)])
(if (eof-object? n)
(void)
(begin (displayln (+ (read) n))
(f)))))
(f)
ruby (.rb)
STDIN.each_line do |line|
puts line.split.map(&:to_i).inject(:+)
end
valac (.vala)
public static int main(string[] args) {
int a = 0, b = 0;
while (stdin.scanf("%d%d", &a, &b) == 2) {
stdout.printf("%d
", a + b);
}
return 0;
}
vbnc (.vb)
Module Program
Sub Main()
Do
Dim line As String = Console.ReadLine()
If line Is Nothing Then Exit Do
Dim a() As String = line.Split(" "c)
Console.WriteLine(CInt(a(0)) + CInt(a(1)))
Loop
End Sub
End Module