問題 A: 求最大最小數
題目描述
先輸入N,表示數的個數,然後輸入N個數,求這N個數的最大值和最小值。N<=10000,輸入的數的絕對值不大於10^6
樣例輸入
4
2 0 1 2
樣例輸出
2 0
Note
無。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
int x, mx, mn;
cin >> x;
mx = mn = x;
--n;
while (n--)
{
cin >> x;
mx = max(mx, x);
mn = min(mn, x);
}
cout << mx << " " << mn << endl;
}
return 0;
}
問題 B: 全排列
題目描述
給定一個由不同的小寫字母組成的字符串,輸出這個字符串的所有全排列。
我們假設對於小寫字母有’a’ < ‘b’ < … < ‘y’ < ‘z’,而且給定的字符串中的字母已經按照從小到大的順序排列。
輸入
輸入只有一行,是一個由不同的小寫字母組成的字符串,已知字符串的長度在1到6之間。
輸出
輸出這個字符串的所有排列方式,每行一個排列。要求字母序比較小的排列在前面。字母序如下定義:
已知S = s1s2…sk , T = t1t2…tk,則S < T 等價於,存在p (1 <= p <= k),使得
s1 = t1, s2 = t2, …, sp - 1 = tp - 1, sp < tp成立。
注意每組樣例輸出結束後接一個空行。
樣例輸入
xyz
樣例輸出
xyz
xzy
yxz
yzx
zxy
zyx
提示
用STL中的next_permutation會非常簡潔。
Note
無。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
while (cin >> str)
{
do
{
cout << str << endl;
} while (next_permutation(str.begin(), str.end()));
cout << endl;
}
return 0;
}
問題 C: 數組逆置
題目描述
輸入一個字符串,長度小於等於200,然後將數組逆置輸出。
輸入
測試數據有多組,每組輸入一個字符串。
輸出
對於每組輸入,請輸出逆置後的結果。
樣例輸入
tianqin
樣例輸出
niqnait
提示
注意輸入的字符串可能會有空格。
Note
無。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
while (getline(cin, str))
{
reverse(str.begin(), str.end());
cout << str << endl;
}
return 0;
}
一定要自己寫一遍哦~~~