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

本文主要是介绍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; // 检查最低位是否为1n >>= 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;}

这篇关于HJ14 字符串排序HJ15 求int型正整数在内存中存储时1的个数HJ17 坐标移动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/847071

相关文章

Golang基于内存的键值存储缓存库go-cache

《Golang基于内存的键值存储缓存库go-cache》go-cache是一个内存中的key:valuestore/cache库,适用于单机应用程序,本文主要介绍了Golang基于内存的键值存储缓存库... 目录文档安装方法示例1示例2使用注意点优点缺点go-cache 和 Redis 缓存对比1)功能特性

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

Python重命名文件并移动到对应文件夹

《Python重命名文件并移动到对应文件夹》在日常的文件管理和处理过程中,我们可能会遇到需要将文件整理到不同文件夹中的需求,下面我们就来看看如何使用Python实现重命名文件并移动到对应文件夹吧... 目录检查并删除空文件夹1. 基本需求2. 实现代码解析3. 代码解释4. 代码执行结果5. 总结方法补充在

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Redis存储的列表分页和检索的实现方法

《Redis存储的列表分页和检索的实现方法》在Redis中,列表(List)是一种有序的数据结构,通常用于存储一系列元素,由于列表是有序的,可以通过索引来访问元素,因此可以很方便地实现分页和检索功能,... 目录一、Redis 列表的基本操作二、分页实现三、检索实现3.1 方法 1:客户端过滤3.2 方法