luogu#P1001. A+B Problem
A+B Problem
Background
If you're new to programming contests, please read this carefully:
In programming contests, your program's output must contain only the answer—nothing extra.
That includes messages like "Please enter integers a and b" or any other prompts for the user.
If you print anything like that, the system will mark your submission as Wrong Answer (shown as WA on Luogu).
When checking your output, the judge system will ignore spaces at the end of any line and extra blank lines after the last line of output.
But if your code prints extra text (like prompts or explanations), it will be marked wrong—even if the answer itself is correct.
So, if your program works fine on your own computer but gets WA when you submit it, don't blame the judge system.
Instead, double-check that you're not printing anything unnecessary. You can look at the sample code at the end of this problem for reference.
Also, try using the IDE mode on Luogu—it helps avoid small differences between your local environment and the judge's system.
Finally, don't post your solution in the discussion section.
Solutions should go in the "Solution" area. If you post a full solution in the discussion section, it may be deleted, and you could even be muted.
If you can't submit a solution, it means too many people have already done so—but you still shouldn't post it in the discussion section.
Only if your method is truly different from all existing ones should you contact a site admin to ask for permission to add your solution.
Problem Description
Input two integers, and , and output their sum ().
Notes:
- In Pascal, using
integermay overflow. - Negative numbers are allowed.
- In C/C++, the
mainfunction must returnint, and you mustreturn 0. This is a general requirement for problems on Luogu as well as in NOIP/CSP/NOI contests.
Let's get started with this simple problem and begin the journey toward becoming an expert.
Every great idea has a humble beginning.
Input Format
Two space-separated integers.
Output Format
A single integer.
20 30
50
Hint
Program Examples in Various Languages:
C
#include <stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n", a+b);
return 0;
}
C++
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
Pascal
var a, b: longint;
begin
readln(a,b);
writeln(a+b);
end.
Python3
s = input().split()
print(int(s[0]) + int(s[1]))
Java
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
Scanner cin=new Scanner(System.in);
int a = cin.nextInt(), b = cin.nextInt();
System.out.println(a+b);
}
}
JavaScript (Node.js)
const fs = require('fs')
const data = fs.readFileSync('/dev/stdin')
const result = data.toString('ascii').trim().split(' ').map(x => parseInt(x)).reduce((a, b) => a + b, 0)
console.log(result)
process.exit() // Ensure this line is added at the exit point
Ruby
a, b = gets.split.map(&:to_i)
print a+b
PHP
<?php
$input = trim(file_get_contents("php://stdin"));
list($a, $b) = explode(' ', $input);
echo $a + $b;
Rust
use std::io;
fn main(){
let mut input=String::new();
io::stdin().read_line(&mut input).unwrap();
let mut s=input.trim().split(' ');
let a:i32=s.next().unwrap()
.parse().unwrap();
let b:i32=s.next().unwrap()
.parse().unwrap();
println!("{}",a+b);
}
Go
package main
import "fmt"
func main() {
var a, b int
fmt.Scanf("%d%d", &a, &b)
fmt.Println(a+b)
}
C# Mono
using System;
public class APlusB{
private static void Main(){
string[] input = Console.ReadLine().Split(' ');
Console.WriteLine(int.Parse(input[0]) + int.Parse(input[1]));
}
}
Visual Basic Mono
Imports System
Module APlusB
Sub Main()
Dim ins As String() = Console.ReadLine().Split(New Char(){" "c})
Console.WriteLine(Int(ins(0))+Int(ins(1)))
End Sub
End Module
Kotlin
fun main(args: Array<String>) {
val (a, b) = readLine()!!.split(' ').map(String::toInt)
println(a + b)
}
Haskell
main = do
[a, b] <- (map read . words) `fmap` getLine
print (a+b)
Lua
a = io.read('*n')
b = io.read('*n')
print(a + b)
OCaml
Scanf.scanf "%i %i\n" (fun a b -> print_int (a + b))
Julia
nums = map(x -> parse(Int, x), split(readline(), " "))
println(nums[1] + nums[2])
Scala
object Main {
def main(args: Array[String]): Unit = {
import java.util.Scanner
val cin = new Scanner(System.in)
val a = cin.nextInt()
val b = cin.nextInt()
System.out.println(a + b)
}
}
Perl
my $in = <STDIN>;
chomp $in;
$in = [split /[\s,]+/, $in];
my $c = $in->[0] + $in->[1];
print "$c\n";