題目描述:
解題思路:
? ? ? ? 本題采用貪心思想
圖解
題解:
#include<bits/stdc++.h>
using namespace std;const int N = 1e6 + 9;
char s[N];//寫字符串數組的一種方法,像數組一樣***int main()
{int n, x;cin >> n >> x;for(int i = 1; i <= n; i++)cin >> s[i];//cin >> s + 1另一種輸入方法,但要注意cin的是首地址。同時以上兩種都無法讀取空格//因為題目不需要讀取空格,因此這樣寫sort(s + 1, s + n + 1);//不規律數據建議先排序再想思路if(s[1] == s[n]){for(int i = 1; i <= n / x + (n % x ? 1 : 0); i++)cout << s[i];//可以在循環內利用三目運算符,以分類處理}else if(s[x] == s[1]){for(int i = x; i <= n; i++)cout << s[i];}else cout << s[x];return 0;
}
擴展知識點:?
? ? ? ? 字符串大小比較:
a和aa比,后者更大,因為a和空白比,有比沒有更大。