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

相关文章

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

HDFS—存储优化(纠删码)

纠删码原理 HDFS 默认情况下,一个文件有3个副本,这样提高了数据的可靠性,但也带来了2倍的冗余开销。 Hadoop3.x 引入了纠删码,采用计算的方式,可以节省约50%左右的存储空间。 此种方式节约了空间,但是会增加 cpu 的计算。 纠删码策略是给具体一个路径设置。所有往此路径下存储的文件,都会执行此策略。 默认只开启对 RS-6-3-1024k

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

spoj705( 求不相同的子串个数)

题意:求串s的不同子串的个数 解题思路:任何子串都是某个后缀的前缀,对n个后缀排序,求某个后缀的前缀的个数,减去height[i](第i个后缀与第i-1 个后缀有相同的height[i]个前缀)。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstrin

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

hdu 1285(拓扑排序)

题意: 给各个队间的胜负关系,让排名次,名词相同按从小到大排。 解析: 拓扑排序是应用于有向无回路图(Direct Acyclic Graph,简称DAG)上的一种排序方式,对一个有向无回路图进行拓扑排序后,所有的顶点形成一个序列,对所有边(u,v),满足u 在v 的前面。该序列说明了顶点表示的事件或状态发生的整体顺序。比较经典的是在工程活动上,某些工程完成后,另一些工程才能继续,此时

XTU 1233 n个硬币连续m个正面个数(dp)

题面: Coins Problem Description: Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing head face or tail face o

我在移动打工的日志

客户:给我搞一下录音 我:不会。不在服务范围。 客户:是不想吧 我:笑嘻嘻(气笑) 客户:小姑娘明明会,却欺负老人 我:笑嘻嘻 客户:那我交话费 我:手机号 客户:给我搞录音 我:不会。不懂。没搞过。 客户:那我交话费 我:手机号。这是电信的啊!!我这是中国移动!! 客户:我不管,我要充话费,充话费是你们的 我:可是这是移动!!中国移动!! 客户:我这是手机号 我:那又如何,这是移动!你是电信!!

《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)

8.3 交换排序 8.3.1 冒泡排序 【算法特点】 (1) 稳定排序。 (2) 可用于链式存储结构。 (3) 移动记录次数较多,算法平均时间性能比直接插入排序差。当初始记录无序,n较大时, 此算法不宜采用。 #include <stdio.h>#include <stdlib.h>#define MAXSIZE 26typedef int KeyType;typedef char In