文章

7

粉丝

0

获赞

14

访问

212

头像
IP地址 题解:注意0.0.0.0这个地址,80%过不了,找好久
P1023 贵州大学机试题
发布于2025年3月10日 16:19
阅读数 76

#include <bits/stdc++.h>
using namespace std;

// 将十进制的字符串转换为十六进制字符串
string convert(string s) {
    int res = 0; 
    for (int i = 0; i < s.size(); i++) {
        res = res * 10 + (s[i] - '0');
    }

    if (res == 0) {
        return "0";
    }

    string result = "";
    while (res != 0) {
        int temp = res % 16;
        if (temp < 10) {
            result = (char)(temp + '0') + result;  // 拼接字符
        } else {
            result = (char)(temp - 10 + 'A') + result;  // 处理 A-F
        }
        res /= 16;
    }

    // 确保返回两位十六进制数
    if (result.size() == 1) {
        result = '0' + result;
    }

    return result;
}

int main() {
    string ip;
    cin >> ip;

    // 判断“.”数量
    int dotCount = count(ip.begin(), ip.end(), '.');
    if (dotCount != 3) {  // 确保是一个有效的点分十进制IP
        cout << "Error" << endl;
        return 0;
    }

    // 如果 IP 地址是 "0.0.0.0",直接返回 "0x00000000"
    if (ip == "0.0.0.0")...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发