题解 | #IP地址#
IP地址
https://www.nowcoder.com/practice/2359e23180194f99828f5cd9c764236a
#include <bits/stdc++.h> using namespace std; int main() { string ip; while(cin>>ip) { int len = ip.length(); stack <int> stk; int flag = 0; for(int i = 0; i < len ; i ++) { if(ip[i]!='.') { stk.push(ip[i]-'0'); } else if(ip[i] == '.') { int sum = 0; int times = 0; while(!stk.empty()) { int temp = stk.top(); sum += temp * pow(10,times); times++; stk.pop(); } if(!(sum>=0&&sum<=255)) { flag = 1; } } } int sum = 0; int times = 0; while(!stk.empty()) { int temp = stk.top(); sum += temp * pow(10,times); times++; stk.pop(); } if(!(sum>=0&&sum<=255)) { flag = 1; } if(flag == 0) cout << "Yes!" << endl; else cout << "No!" << endl; } return 0; }