AcWing 3634. IP地址
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e6 + 10;
int t, n, m, k, l, r, op, x, y,cnt;
string str;
string tmp;
char last='.';
void solve() {
cin >> str;
str.push_back('.');
string ans = "0x";
for (char &ch : str) {
if (!(ch >= '0' && ch <= '9')&&!(ch=='.')) {
cout << "Error";
return;
}
if (ch == '.') {
cnt++;
if (x > 255||cnt>4||last=='.') {
cout << "Error";
return;
}
tmp.clear();
while(x){
tmp.push_back(x%16<10?x%16+'0':x%16+'A'-10);
x/=16;
}
while(tmp.size()<2)tmp.push_back('0');
reverse(tmp.begin(),tmp.end());
ans += tmp;
} else {
x = x*10 +ch - '0';
}
last = ch;
}
if(cnt!=4){
cout<<"Error";
return ;
}
cout<<ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}