1000.A+B Problem with sample answers

Time Limit: 1s Memory Limit: 125MB

Given to whole numbers $a, b$, you are expected to caculate the result of expression $a+b$.

Input Format(From the terminal/stdin)

Two integer a,b $(-10^5 \le a,b \le 10^5)$

Output Format(To the terminal/stdout)

Output the result of $a+b$.

Sample Input

Copy
1 2
 · \n

Sample Output

Copy
3
 \n

Hints

Here are some example solution with various programming languages.

  1. C programming language

    #include <stdio.h>
    int main()
    {
    int a,b;
    while(scanf("%d %d",&a, &b) != EOF)
        printf("%d\n",a+b);
    return 0;
    }
  2. C++ programming language

    #include <iostream>
    using namespace std;
    int main()
    {
    int a,b;
    while(cin >> a >> b)
        cout << a+b << endl;
    }
  3. Pascal/Delphi programming language

    program p1001(Input,Output);
    var
    a,b:Integer;
    begin
    while not eof(Input) do
     begin
       Readln(a,b);
       Writeln(a+b);
     end;
    end
  4. Java programming language

    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);
        }
    }
    }
  5. shell scripts

    a=gets.chomp
    b="0"
    a.each_line(" "){
    |d|
    b=b+"+"+d
    }
    puts eval(b)
    read a b
    echo ((a+$b))
  6. Python programming language

    import sys
    for line in sys.stdin:
    a = line.split()
    print int(a[0]) + int(a[1])
  7. PHP scripts

    <?php
    while (fscanf(STDIN, "%d%d", a,b) == 2) {
    print (a+b) . "\n";
    }
    ?>
  8. perl scripts

    #! /usr/bin/perl -w
    while(<>){
    chomp;
    (a,b)=split(/\s/,$_);
    printf "%d\n",a+b;
    }
  9. C# programming language

    using System;
    public class Sum
    {
    public static void Main()
    {
        //string token = Console.ReadLine();
        //int test = int.Parse(token);
        for(int k = 0; k < 1;k++){
             string[] tokens = Console.ReadLine().Split(' ');
             Console.WriteLine(int.Parse(tokens[0]) + int.Parse(tokens[1]));
        }
    }
    }

Submit

请先 登录

© 2025 FAQs