Your car is old, it breaks easily. The shock absorbers are gone and
you think it can handle about 15 more bumps before it dies totally.Unfortunately for you, your drive is very bumpy! Given a string
showing either flat road ("") or bumps (“n”), work out if you make it
home safely. 15 bumps or under, return “Woohoo!”, over 15 bumps return
“Car Dead”.
road字符串是由’n’/ '’ 组成的. 只要遇到15次以上的n车就会报废
要点
- 用之前学过的
count_if
统计出字符n的个数即可
#include <string>
std::string bumps(std::string road){
auto bumps = std::count_if(road.begin() , road.end() , [](char ch){ return 'n' == ch; } ) ;
return bumps > 15 ? "Car Dead" :"Woohoo!" ;
}