A+B問題是一個基礎的程式設計問題。通常是資訊學線上解題系統用來測試提交和輸入輸出方法的題目。[1]
一般描述
輸入兩個數和(一般是在整數範圍內),輸出的計算結果。
範例程式碼
import java.util.*;
public class AB {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a,b;
a=sc.nextInt();
b=sc.nextInt();
System.out.println(a+b);
} //main end
} //AB end
#include <stdio.h>
int main(void)
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
return 0;
}
#include <iostream>
int main()
{
int a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}
或
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
var a,b:longint;
begin
readln(a,b);
writeln(a+b)
end.
gets.strip.split.map(&:to_i).reduce(:+)
適用於 Python 3 :
print(sum(map(int, input().split())))
PROGRAM P1000
IMPLICIT NONE
INTEGER :: A, B
READ(*,*) A, B
WRITE(*, "(I0)") A + B
END PROGRAM P1000
<?php
$input = trim(file_get_contents("php://stdin"));
list($a, $b) = explode(' ', $input);
echo $a + $b;
擴充
有的版本會對命題的條件進行調整,增加或刪除某些限制條件,使得以上範例代碼無法通過測試,例如:
參考文獻
參見
Wikiwand in your browser!
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.