题解 | #IP地址#
IP地址
https://www.nowcoder.com/practice/2359e23180194f99828f5cd9c764236a
#include <iostream> #include <string> using namespace std; string ip(string s)//判断函数 { string result = "Yes!"; int count = 0; for (int i = 0; i < s.size(); i++) { if (s[i] < 0 || count>3) { result = "No!"; break; } if (i == s.size() - 1 && count == 2)//最后一个地址,单独讨论 { if (s[i - 2] > '2') { result = "No!"; break; } else if (s[i - 2] == '2' && s[i - 1] > '5') { result = "No!"; break; } else if(s[i - 2] == '2' && s[i - 1] == '5' && s[i] > '5') { result = "No!"; break; } } if (s[i] == '.' && count == 3 ) //前三个地址,并且是三位数时 { if (s[i - 3] > '2') { result = "No!"; break; } else if (s[i - 3] == '2' && s[i - 2] > '5') { result = "No!"; break; } else if (s[i - 3] == '2' && s[i - 2] == '5' && s[i-1] > '5') { result = "No!"; break; } count = -1; } if (s[i] == '.')//每经历一个.,就将count置为-1 { count = -1; } count++;//记录一小段数字的长度 } return result; } int main() { string s1, s2, result1, result2; cin >> s1; result1 = ip(s1); cout << result1 << endl; } // 64 位输出请用 printf("%lld")