题目链接:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1874
划水题,但是需要注意的方面太多,最好考虑全面再写代码
个人感觉使用stringstream方便一点,可以将字符串快速分开
代码:
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstdlib> #include<algorithm> #include<string> #include<iostream> #include<sstream> using namespace std; bool check(string s) { int dotnum = 0; for (int i = 0; i < s.size(); i++) if (s.at(i) == '.') s[i] = ' ', dotnum++; if (dotnum > 3) return false; string str[5] = {}; stringstream ss; ss << s; for (int i = 0; i < 4; i++) ss >> str[i]; for (int i = 0; i < 4; i++) { if (str[i] == "" || str[i].size() > 3) return false; int ai = 0; for (int j = 0; j < str[i].size(); j++) { if (str[i].at(j) < '0' || str[i].at(j) > '9') return false; else { ai *= 10; ai += str[i].at(j) - '0'; } } if (ai < 0 || ai>255) return false; } return true; } int main() { ios::sync_with_stdio(true); int t; cin >> t; for (int i = 1; i <= t; i++) { string s; cin >> s; if (!check(s)) { cout << "No" << endl; } else cout << "Yes" << endl; } return 0; }