HJ14 字符串排序HJ15 求int型正整数在内存中存储时1的个数HJ17 坐标移动

HJ14 字符串排序

字符串排序_牛客题霸_牛客网

题目分析

使用C++的标准库函数sort来简单解决。首先,我们读取给定数量的字符串,然后使用sort函数对它们进行字典序排序,最后输出排序后的字符串列表。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n; // 读取字符串的数量

    vector<string> strs(n);
    for (int i = 0; i < n; ++i) {
        cin >> strs[i]; // 读取每个字符串
    }

    sort(strs.begin(), strs.end()); // 使用sort函数按字典序对字符串进行排序

    for (const string& str : strs) {
        cout << str << endl; // 输出排序后的字符串
    }

    return 0;
}

我的题解:

一开始不知道sort可以直接排序字符串,于是自己写了一个比较函数逐字符比较

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

bool compare(std::string &a, std::string &b) {
    for (int i = 0; i < std::min(a.size(), b.size()); i++) {
        if (a[i] != b[i]) return a[i] < b[i];
    }
    return a.size() <= b.size();
}


int main() {
    int n = 0;
    std::string word;
    std::vector<std::string> words;

    std::cin >> n;
    while (n--) {
        std::cin >> word;
        words.push_back(word);
    }
    std::sort(words.begin(), words.end(), compare);
    for (auto i:words) {
        std::cout << i << std::endl;
    }
    return 0;
}

HJ15 求int型正整数在内存中存储时1的个数

求int型正整数在内存中存储时1的个数_牛客题霸_牛客网

题目分析

C++中,可以通过不断地将数字右移并检查最低位是否为1来实现这一点。下面是一个简单的解决方案:

#include <iostream>
using namespace std;

int countOnes(int n) {
    int count = 0;
    while (n) {
        count += n & 1; // 检查最低位是否为1
        n >>= 1; // 右移一位
    }
    return count;
}

int main() {
    int n;
    cin >> n; // 读取输入的整数
    cout << countOnes(n); // 输出1的个数
    return 0;
}

我的题解

通过减去最大的2的幂次方数,逐步减少数字,直到数字变为0,从而计算1的数量。

#include <iostream>
#include <cmath>

int numsofone(int& num) {
    if (num == 0) return 0;
    int result = 1, i = 0;
    while (pow(2, i) != num) {
        i++;
        if (std::pow(2, i) > num) {
            result ++;
            num = num - pow(2, (i - 1));
            i = 0;
        }
    }
    return result;

}


int main() {
    int num = 0;
    std::cin >> num;
    std::cout << numsofone(num) << std::endl;
    return 0;

}

HJ17 坐标移动

题目分析

  1. 解析输入字符串:使用std::istringstreamstd::getline()读取以分号(;)分隔的命令。
  2. 处理每个命令:对于每个命令,检查其是否有效(即以ASWD开头,后跟一串数字)。
  3. 更新坐标:根据命令更新当前坐标。
  4. 输出最终坐标
#include <iostream>
#include <sstream>
#include <string>
#include <regex>

int main() {
    std::string input;
    std::getline(std::cin, input); // 读取整行输入
    std::istringstream ss(input);
    std::string command;
    int x = 0, y = 0; // 初始坐标

    // 用正则表达式匹配有效命令:字母(A、S、W、D) + 数字
    std::regex validCommandPattern(R"([ASWD]\d+)");

    while (std::getline(ss, command, ';')) {
        if (std::regex_match(command, validCommandPattern)) {
            char direction = command[0];
            int steps = std::stoi(command.substr(1));

            switch (direction) {
                case 'A': // 向左移动
                    x -= steps;
                    break;
                case 'D': // 向右移动
                    x += steps;
                    break;
                case 'W': // 向上移动
                    y += steps;
                    break;
                case 'S': // 向下移动
                    y -= steps;
                    break;
            }
        }
    }

    std::cout << x << "," << y << std::endl; // 输出最终坐标

    return 0;
}

我的题解

不用正则

#include <iostream>
#include <string>
#include <vector>

int main() {
    int x = 0, y = 0, num = 0;
    std::string line, temp;
    std::vector<std::string> vec;
    std::getline(std::cin, line);
    for (int i = 0; i < line.size(); i++) {
        if (line[i] != ';') {
            temp.push_back(line[i]);
        }
        else{
            vec.push_back(temp);
            temp.clear();
        }
    }
    for (std::string i : vec) {
        bool isnum = true;
        if (i.size() <= 1 | i.size() > 3) continue;
        if (i[0] == 'A' ||i[0] == 'W' ||i[0] == 'S' ||i[0] == 'D') {
            std::string sub = i.substr(1);
            for (auto i : sub) {
                if (i < '0' || i > '9') isnum = false;
            }
            if (!isnum) continue;
            num = std::stoi(sub);
            if (num > 0 && num <= 99) {
                if (i[0] == 'A') x -= num;
                else if (i[0] == 'W') y += num;
                else if (i[0] == 'S') y -= num;
                else if (i[0] == 'D') x += num;
            }
            
        }
    }
    std::cout << x << ',' << y << std::endl;

}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值