解题思路:
注意事项:
参考代码:
1.
#include<iostream> #include <algorithm> #include <string> using namespace std; bool judge_num(string num, int len){ for (int i = 0; i < len; i++){ if (isdigit(num[i])) continue; else return false; } int tmp = stoi(num, 0, 10); if (0 <= tmp && tmp <= 255) return true; return false; } int main() { string s; while (cin >> s && s != "End"){ s += '.'; string tmp; char cnt = 0; bool flag = true; if (count(s.begin(), s.end(), '.') == 4){ for (int i = 0; i < s.length(); i++){ if (s[i] == '.'){ if (judge_num(tmp, tmp.size())){ cnt += 1; } else{ cout << "N\n"; flag = false; break; } tmp.erase(); } else{ tmp += s[i]; } } if (flag) cout << "Y\n"; } else { cout << "N\n"; } } return 0; }
2.
#include<iostream> #include <algorithm> #include <string> using namespace std; int main() { string s; while (cin >> s){ s += '.'; bool flag = true; auto len = s.length(); if (count(s.begin(), s.end(), '.') == 4){ for (int i = 0; i < 4; i++) // check if str is a number try { int end = s.find('.'); int tmp = stoi(s.substr(0, end), 0, 10); if (tmp > 255 || tmp < 0){ cout << "N\n"; flag = false; break; } s = s.substr(end + 1, len); } catch(...){ // not a number cout << "N\n"; flag = false; break; } } else { // not enough '.' cout << "N\n"; continue; } if (flag) cout << "Y\n"; } return 0; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:627 |
C语言训练-排序问题<2> (C++代码)浏览:936 |
C语言训练-求素数问题 (C语言代码)浏览:773 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:436 |
数组输出 (C语言代码)错误???浏览:602 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:632 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:467 |
WU-蓝桥杯算法提高VIP-企业奖金发放 (C++代码)浏览:1267 |
IP判断 (C语言代码)浏览:820 |
蚂蚁感冒 (C语言代码)浏览:1408 |