解题思路:
注意事项:
参考代码:
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 人评分
K-进制数 (C++代码)浏览:850 |
C二级辅导-统计字符 (C语言代码)浏览:502 |
C语言训练-计算:t=1-1/(2*2)-1/(3*3)-...-1/(m*m) (C语言代码)浏览:777 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:377 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5214 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:597 |
简单的a+b (C语言代码)浏览:568 |
DNA (C语言代码)浏览:744 |
C二级辅导-计负均正 (C语言代码)浏览:480 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:672 |