1. 程式人生 > >『ACM入門』藍橋杯ACM訓練系統基本輸入輸出教程

『ACM入門』藍橋杯ACM訓練系統基本輸入輸出教程

『ACM入門』藍橋杯ACM訓練系統基本輸入輸出教程

在介紹訓練場的OJ系統之前,首先為大家介紹一下ACM:

ACM原代表美國計算機協會,因其舉辦的ICPC即國際大學生程式設計競賽而聞名全世界,此項賽事要求學生的在五小時內解決全英文問題,並在效率和速度以及程式碼的審查上要求非常嚴格以至近乎苛刻,被譽為是計算機界的“奧林匹克”。在大學中,因其含金量、認可度等非常之高,故而在大學生名企就業、保研、留學等方面都有著極大的幫助。ACM也因其獨有的比賽趣味也在今天的高校中也得到了廣泛的推廣,許多大學生都為之著迷、甚至大學四年都為之獻身。足以說明ACM的魅力所在。

OJ簡介:

ACM比賽中主要以OJ(即Online Judge)判題為主,用來線上檢測程式的正確性。OJ採用後臺黑箱測試,測試資料非常全面,涵蓋各種特殊情況。並且在結果的比對上也不放過一個空格和回車,這就要求程式設計師要有非常嚴謹的思維。著名的OJ有POJ、HOJ、UVA等。

輕量級入門OJ ACM訓練平臺:http://www.dotcpp.com

在各大OJ的ACM比賽賽題上,往往都會給出問題的描述(Description)、問題的輸入和輸出要求,並會給出幾組樣例資料。所以選手要在完全理解的基礎上至少通過了樣例資料才再提交程式碼。

下面介紹幾種常見的輸入輸出格式。

A+B Ⅰ

這種輸入的典型題目就是A+B
A+B for Input-Output Practice (I)

時間限制: 1Sec 記憶體限制: 64MB

題目描述
Your task is to Calculate a + b. Too easy?! Of course! I specially designed the problem for acm beginners. You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim
輸入
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
輸出
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
樣例輸入
1 5
10 20
樣例輸出
6
30

此題只要求使用者求A+B的和,但此類題目往往說明測試資料有多組。則預設是到檔案(後臺測試用例在檔案裡)末尾結束。我們則可以採用迴圈的方式不斷接收測試用例,並且每接收一組輸出一組(不必全部輸入再全部輸出,因為OJ只比對一次最後的結果)。

參考C程式碼:

#include<stdio.h>
int main()
{
 int a,b;
 while(scanf(“%d%d”,&a,&b)==2) //利用scanf的返回值
 {
 printf(“%d\n”,a+b);
 }
 return 0;
}

參考Java程式碼:

import java.util.Scanner;

public class AplusB1 {

	public static void main(String[] args) {
		// TODO 自動生成的方法存根
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			System.out.println(a+b);	
		}	
	}
}

A+BⅡ

依然是A+B,我們可以看一下這道題
A+B for Input-Output Practice (II)

時間限制: 1Sec 記憶體限制: 64MB

題目描述
The first line integer means the number of input integer a and b. Your task is to Calculate a + b.
輸入
Your task is to Calculate a + b. The first line integer means the numbers of pairs of input integers.
輸出
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
樣例輸入
2
1 5
10 20
樣例輸出
6
30

依然是求A+B,但它的輸入資料為:

2

1 5

10 20

輸出為:

6

30

此題相比第一道而言,會提前告訴你是幾組資料,第一行的2就表示有兩組資料。則此時我們可以考慮這樣寫:

#include<stdio.h>
int main()
{
 int n;
 int a,b;
 scanf("%d",&n);
 while(n--)
 {
 scanf("%d%d",&a,&b);
 printf("%d\n",a+b);
 }
 return 0;
}

A+B Ⅲ
A+B for Input-Output Practice (III)

時間限制: 1Sec 記憶體限制: 64MB

題目描述
Your task is to Calculate a + b.
輸入
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
輸出
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
樣例輸入
1 5
10 20
0 0
樣例輸出
6
30

繼續是A+B,不同的是題目中說明,有多組資料,但是以A和B都為0時結束,則此時,就要做判斷。不能依然輸出0。

參考答案:

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

A+B Ⅳ
A+B for Input-Output Practice (IV)

時間限制: 1Sec 記憶體限制: 64MB

題目描述
Your task is to Calculate the sum of some integers.
輸入
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
輸出
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
樣例輸入
4 1 2 3 4
5 1 2 3 4 5
0
樣例輸出
10
15

題目描述:此類題目是求n個數的和,輸入為先輸入一個數組n,然後後面跟n個數字,求這n個數字的和。同樣有多組資料,當n=0時結束。
樣例輸入:

4 1 2 3 4

5 1 2 3 4 5

0

樣例輸出:

10

15

參考寫法:

#include<stdio.h>
int main()
{
 int n;
 int sum,temp;
 while(scanf("%d",&n) && n)
 {
 sum=0;
 while(n--)
 {
 scanf("%d",&temp);
 sum+=temp;
 }
 printf("%d\n",sum);
 }
 return 0;
}

總結:

常見的ACM的輸入輸出格式如這些。

多組資料可能還會用到EOF、NULL這些巨集。比如scanf、getchar、gets的返回值。

萬變不離其宗,有些題目可能是這些格式的組合,亦或者是個別情況的變種,比如輸出格式要求的變化等等,就需要各位ACMer靈活多變了。