ACM I/O Templates#
Many people struggle to solve algorithm problems in job interviews and written tests due to unfamiliarity with ACM-style input and output. Input and output are the fundamentals of solving algorithm problems, and you can “memorize” the templates based on your preferred programming language.
Below is a summary of template implementations for common ACM input and output scenarios, covering mainstream programming languages such as C++, Python, Go, Java, and JavaScript. All sample codes are for the “A+B” problem, where the task is to read two numbers A and B and output their sum A+B.
1. Multiple Data Groups: Each Group Starts with n Followed by n Lines of Two Integers#
Practice Problem: A+B Problem I
Input: The first line contains an integer N, indicating there will be N subsequent lines, each containing two integers a and b separated by a space.
C++#
#include<iostream>
using namespace std;
int main() {
int n, a, b;
while (cin >> n) {
while (n--) {
cin >> a >> b;
cout << a + b << endl;
}
}
}
Python#
n = int(input())
for _ in range(n):
a, b = map(int, input().split())
print(a + b)
Go#
package main
import "fmt"
func main(){
var n, a, b int
for {
_, err := fmt.Scanf("%d", &n)
if err != nil {
break
}
for n > 0 {
_, err := fmt.Scanf("%d %d", &a, &b)
if err != nil {
break
}
fmt.Println(a + b)
n --
}
}
}
Java#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
while (n -- > 0) {
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
}
}
}
JavaScript#
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let n, count = 0;
rl.on('line', (input) => {
if (!n) {
n = +input; // Fix: Original "line" is corrected to "input" for consistency
} else {
const [a, b] = input.split(' ').map(Number);
console.log(a + b); // Fix: Incomplete code in original is completed
if (++count >= n) rl.close();
}
})
2. Multiple Lines of Input: Each Line Contains Two Integers#
Practice Problem: A+B Problem II
Input: Contains a series of (a, b) pairs, each separated by a space. Each (a, b) pair occupies one line.
C++#
#include<iostream>
using namespace std;
int main() {
int a, b;
while(cin >> a >> b) cout << a + b << endl;
}
Python#
while True:
try:
a, b = map(int, input().split())
print(a + b)
except:
break
Go#
package main
import "fmt"
func main(){
var a, b int
for {
_, err := fmt.Scanf("%d %d", &a, &b)
if err != nil {
break
}
fmt.Println(a + b)
}
}
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);
}
}
}
JavaScript#
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
const [a, b] = input.split(' ').map(Number);
console.log(a + b);
})
