题解 | IP地址
IP地址
https://www.nowcoder.com/practice/2359e23180194f99828f5cd9c764236a
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool func(string str) {
bool flag = true;
for (int i = 0; i < str.size(); i++) {
int res = 0, j;
for (j = i; isdigit(str[j]); j++) {
res += str[j] - '0';
res *= 10;
}
res /= 10;
if (res < 0 || res > 255) {
flag = false;
break;
}
i = j;
}
return flag;
}
int main() {
string str;
while (cin >> str) {
if (func(str)) {
cout << "Yes!" << endl;
} else {
cout << "No!" << endl;
}
}
return 0;
}
查看11道真题和解析