原文最初发表于2009年9月18日,并消失于地震

C

/* A + B Problem in C */
#include <stdio.h>

int main(void) {
	int a, b;

	while (scanf("%d%d", &a, &b) != EOF) {
		printf("%d\n", a + b);
	}

	return 0;
}


C++

// A + B Problem in C++
#include <iostream>

int main() {
	int a, b;

    while (std::cin >> a >> b) {
		std::cout << a + b << std::endl;
	}

	return 0;
}

Python

#!/usr/bin/python
# A + B Problem in Python

import sys

for line in sys.stdin:
	print sum(map(int, line.split()))

Ruby

#!/usr/bin/ruby
# A + B Problem in Ruby

readlines.each{|line|
	p line.split.collect{|x| x.to_i}.inject{|y, x| y += x}
}

C#

// A + B Problem in C#
using System;

public class EMP {
	public static void Main(string[] args) {
		String input;
		while ((input = Console.ReadLine()) != null) {
			String[] inputs = input.Split();
			int n = Int32.Parse(inputs[0]), m = Int32.Parse(inputs[1]);
			Console.WriteLine(n + m);
		}
	}
}

Java

// A + B Problem in Java
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		while ((line = in.readLine()) != null) {
			String[] ab = line.split(" ");
			System.out.println(Integer.parseInt(ab[0]) + Integer.parseInt(ab[1]));
		}
	}
}

Pascal

{A + B Problem in pascal}
program gao(input, output);

var
	n, m: integer;

begin
	while not eof(input) do
	begin
		readln(n, m);
		writeln(n + m);
	end;
end.

php

#!/usr/bin/php
<?
// A + B Problem in php
while (fscanf(STDIN, "%d%d", $a, $b) == 2) {
	echo $a + $b, "\n";
}
?>

perl

#!/usr/bin/perl -pla
# A + B Problem in perl
$_=$F[0]+$F[1]
7 Responses to “A + B Problem”
  1. quark says:

    main = interact $ unlines . map (show . sum . map read . words) . lines 有没有别的写法?
    ocaml 貌似要处理 EOF 异常怎么写都长

  2. fookwood says:

    请教一下怎样使鼠标移到代码上右上角会出现.”view source”,”copy to clipboard”等图标…

  3. quark says:

    果然我用的是GeShi,不过转义是我最关心的问题,许多插件在HTML文本模式下编辑勉强可以,一旦切换到所见即所得再保存一下就悲剧了 T_T
    虽然目前用的WP-Syntax在大部分情况还比较不错,但是在#include的处理上总是悲剧 -.-

  4.  
Leave a Reply