Это смущает.Пожалуйста, помогите мне понять этот код - PullRequest
0 голосов
/ 26 сентября 2019
string s;
cin >> s;
bool pot = s.find('1', 1) == string::npos;
int ans = pot ? s.size() / 2 :(s.size() + 1) / 2; 

Пожалуйста, объясните мне этот код C ++.

1 Ответ

1 голос
/ 26 сентября 2019

Функционально эквивалентно следующее:

string s;
cin >> s;  // Read a string from stdin

size_t pos = s.find('1', 1); // Find the first '1' character in the string starting with the second character
bool pot = ( pos == string::npos ); // pot is true when '1' is not found

// If not found, the answer is half the size of the string rounded down; otherwise it is half the size of the string rounded up
int ans;
if (pot) {
    ans = s.size() / 2;
} else {
    ans = (s.size() + 1) / 2;
}
...