文章

38

粉丝

56

获赞

64

访问

4.0k

头像
进制转换 题解:已燃尽
P1178 北京大学上机题
发布于2025年3月12日 21:50
阅读数 18

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

// 去除前导零,返回处理后的字符串
string remove_leading_zeros(const string &s) {
    size_t start = 0;
    while (start < s.size() && s[start] == '0') {
        start++;
    }
    if (start == s.size()) { // 全零的情况
        return "0";
    }
    return s.substr(start);
}

// 将十进制字符串除以2,返回商字符串,并通过引用返回余数
string divide_by_two(const string &s, int &remainder) {
    string quotient;
    remainder = 0; // 初始化余数为0
    for (char c : s) {
        int digit = c - '0'; // 将字符转为数字
        int current = remainder * 10 + digit; // 当前值 = 余数*10 + 当前位数字
        int q = current / 2; // 商位
        remainder = current % 2; // 新的余数
  &n...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发