解题思路:
纯字符处理,挨个判断即可。
参考代码:
#include<iostream> #include<string.h> using namespace std; int main() { char str[20]; start:while (cin >> str) { int lenth; lenth = strlen(str); char part[4][5] = { '\0' }; int sum_point = 0; for (int i = 0, j = 0, k = 0; i < lenth; i++, k++) {//将str按.划分开分别存储 if (str[i] == '.') { sum_point++; j++; k = -1; if (sum_point == 4) {//判断点的个数,不小于四个即结束,防止溢出 cout << 'N' << endl; goto start; } } else {//复制到part part[j][k] = str[i]; if (k == 3) { cout << 'N' << endl; goto start; } } } if (sum_point != 3) {//点个数不等于三则错误 cout << 'N' << endl; goto start; } else { for (int l = 0; l < 4; l++)//挨个判断part if ((part[l][0] == '0' && part[l][1] != '\0') || (part[l][0] < '0' || part[l][0] > '9')) {//前导判断,排除前导为0及前导为符号 cout << 'N' << endl; goto start; } else if (strlen(part[l]) == 3) {//对数字大小和255比较 if (part[l][0] > '2') { cout << 'N' << endl; goto start; } else if (part[l][0] == '2') { if (part[l][1] > '5') { cout << 'N' << endl; goto start; } else if (part[l][1] == '5') { if (part[l][2] > '5') { cout << 'N' << endl; goto start; } else { if(l == 3) cout << 'Y' << endl; } } else { if (l == 3) cout << 'Y' << endl; } } else { if (l == 3) cout << 'Y' << endl; } } else { if (l == 3) cout << 'Y' << endl; } } } return 0; }
0.0分
0 人评分