杭電ACM Steps
最近在短學期的任務下,我開始啦杭電的ACM Steps模塊。
其實在4月份左右做過一次ACM的基本輸入輸出,當時使用的全都是C語言,到現在基本都遺忘啦!
目錄
1,A+B for Input-Output Practice (I)
2,A+B for Input-Output Practice (II)
3,A+B for Input-Output Practice (III)
4,A+B for Input-Output Practice (IV)
5,A+B for Input-Output Practice (V)
6,A+B for Input-Output Practice (VI)
7,A+B for Input-Output Practice (VII)
8,A+B for Input-Output Practice (VIII)
如今使用C++確實比C語言更簡單,那麼今天進行總結一下吧!
(圖片是昨晚截的,奇怪的是這一串時間只有我一個在提交代碼,全部顯示的是本人提交代碼,真的是太難得!我就想截個圖紀念一下,哈哈哈哈!!!)
文章中代碼都已Accept
1. The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.(連續輸入一對整數a和b)
A+B for Input-Output Practice (I)
#include <iostream>
using namespace std;
int main()
{
int a, b;
while(cin >> a >> b)
{
cout << a + b << endl;
}
return 0;
}
2. Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.(先輸入N然後輸入N行的一對整數a和b)
A+B for Input-Output Practice (II)
#include <iostream>
using namespace std;
int main()
{
int a, b;
int n;
cin >> n;
while(n--){
while(cin >> a >> b)
{
cout << a + b << endl;
}
}
return 0;
}
3. 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.
(包含多個測試用例,每個測試用例包含一對整數a和b,只有一對整數都爲0,那麼終止測試)
A+B for Input-Output Practice (III)
#include <iostream>
using namespace std;
int main()
{
int a, b;
while(cin >> a >> b && (a || b))
{
cout << a + b << endl;
}
return 0;
}
4. 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.
(每個測試用例中先輸入N,然後輸入N個整數,一旦N爲0那麼終止輸入)
A+B for Input-Output Practice (IV)
#include <iostream>
using namespace std;
int main()
{
int temp;
int sum ;
int n;
while(cin >> n && n)
{
sum = 0;//這裏每次必須置爲0
while(n--){
cin >> temp;
sum += temp;
}
cout << sum << endl;
}
return 0;
}
5. Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
(先輸入N代表需要輸入N個測試用例,在每個測試用例前先輸入M,接着輸入M個整數)
A+B for Input-Output Practice (V)
#include <iostream>
using namespace std;
int main()
{
int temp;
int sum ;
int n, m;
cin >> n;
while(n--){
cin >> m;
sum = 0;
while(m--)
{
cin >> temp;
sum += temp;
}
cout << sum << endl;
}
return 0;
}
6. Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.
(輸入多個測試用例,每組測試用例前輸入N,接着輸入N個整數)
A+B for Input-Output Practice (VI)
#include <iostream>
using namespace std;
int main()
{
int temp;
int sum ;
int m;
while(cin >> m)
{
sum = 0;
while(m--)
{
cin >> temp;
sum += temp;
}
cout << sum << endl;
}
return 0;
}
前六個基本輸入輸出基本包含所有的,無需死記硬背。看題目輸入的要求即可,重點是下面兩個的輸出去要求(格式的問題)
7. The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.(輸入連續的測試用例,每個測試用例包含一對整數,輸入要求和1是一樣的)
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line(要求每輸出一個結果必須中間空一行)
A+B for Input-Output Practice (VII)
#include <iostream>
using namespace std;
int main()
{
int a, b;
while(cin >> a >> b)
{
//直接在原來基礎上cout << a + b << endl;增加一個endl
cout << a + b << endl << endl;
}
return 0;
}
8. Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
(先輸入N,然後輸入N行測試用例,每個用例先輸入M,接着輸入M個整數,輸入要求和5是一眼的)
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
(輸出結果佔一行,而且連個結果之間需要空一行)
#include <iostream>
using namespace std;
int main()
{
int temp;
int sum ;
int n, m;
cin >> n;
while(n--){
cin >> m;
sum = 0;
while(m--)
{
cin >> temp;
sum += temp;
}
if (n != 0)
cout << sum << endl << endl;
else
cout << sum << endl;
//注意點:
/*
if (n != 0)
cout << sum << endl << endl;
else
cout << sum ;//會報Presentation Error
*/
/*
cout << sum << endl << endl;
//此情況也會報Presentation Error
*/
}
return 0;
}