fork download
  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. vector <string> split(string s, char p = '.') {
  9. vector <string> result;
  10.  
  11. string word = "";
  12. s += p;
  13.  
  14. for (char c : s) {
  15. if (c == p) {
  16. result.push_back(word);
  17. word = "";
  18. } else {
  19. word += c;
  20. }
  21. }
  22.  
  23. return result;
  24. }
  25.  
  26. bool check_if_num (string s) {
  27. if (s.empty()) return false;
  28.  
  29. for (char c : s) {
  30. if (!isdigit(c)) {
  31. return false;
  32. }
  33. }
  34.  
  35. return true;
  36. }
  37.  
  38. int count_dots(string s) {
  39. int result = 0;
  40.  
  41. for (char c : s) {
  42. if (c == '.') {result++;}
  43. }
  44.  
  45. return result;
  46. }
  47.  
  48. int main() {
  49. cin.tie(nullptr);
  50. ios_base::sync_with_stdio(false);
  51. string s;
  52. cin >> s;
  53. if (count_dots(s) != 3 || s[s.size() - 1] == '.') {
  54. cout << "NO";
  55. return 0;
  56. }
  57. for (string w : split(s)) {
  58. string copy_without_zero = "";
  59. for (char c : w) {if (c != '0') {copy_without_zero += c;}}
  60.  
  61. if (copy_without_zero.size() > 3) {
  62. cout << "Bad";
  63. return 0;
  64. } else if (!check_if_num(w)) {
  65. cout << "Bad";
  66. return 0;
  67. }
  68.  
  69. int num = stoi(w);
  70.  
  71. if (num < 0 || num > 255) {
  72. cout << "Bad";
  73. return 0;
  74. }
  75. }
  76.  
  77. cout << "Good";
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5304KB
stdin
127.0.0.0
stdout
Good