【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a520
【解題想法】
- 以一個空白「全部取代」兩個連續的空白稱之為一個動作。
- 找出字串中最長的連續空白的長度 mx。
- 需要的動作次數為 ceil (log2 (mx))。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
string s;
while (getline(cin, s)){
double mx = 0.0, cnt = 0.0;
for (int i = 0; i < s.size(); i++){
if (s[i] == ' ') cnt += 1.0;
else {
mx = max(mx, cnt);
cnt = 0.0;
}
}
cout << ceil(log2(mx)) << "\n";
}
return 0;
}