如何根据表示所需的火柴数量对字符串进行排序?
简介
在本教程中,我们实现了一种根据表示字符串所需的火柴数量对字符串进行排序的方法。在此方法中,我们使用 N 个火柴对一个数组进行排序。该数组可以包含数字、单词或两者。火柴棒用于将其排列成特定数字或字符的形状。
演示 1
输入 = Arr = ["1", "3", "4"] 输出 = 排序后的数组为 1 4 3
解释
在上述输入数组中,数组元素为 1、3 和 4
计算 1 所需的火柴棒数量 = 1
计算 3 所需的火柴棒数量 = 5
计算 4 所需的火柴棒数量 = 4
演示 2
输入 = Arr = ["23", "HI", "ABC"] 输出 = 排序后的数组为 HI 23 ABC
解释
上述输入数组中的元素为 23、HI 和 C。
计算 23 所需的火柴数量 = 10
计算"HI"所需的火柴数量 = 6
计算"ABC"所需的火柴数量 = 17
C++ 库函数
sizeof() − 这是 C++ 中的编译时一元运算符,用于计算变量、常量和数据类型的大小。
sizeof(value);
vector − 它是 C++ 中的动态数组。它提供有效的数组功能。它是用于存储元素的数据结构之一。
vector<data_type> vcetor_name;
vector:begin() − 它是 Vector 类中的预定义函数,定义在 <vector> 头文件中。它返回 Vector 起始元素的指针。
vector_name.begin();
vector:end() − 它是 Vector 类中的预定义函数,定义在 <vector> 头文件中。它返回指向 Vector 最后一个元素的指针。
vector_name.end();
vector:sort() − 它是 Vector 类中的预定义函数,定义在 <vector> 头文件中。它使用 begin() 和 end() 函数作为参数对 Vector 元素进行排序。
sort(vector_name.begin(),vector_name.end());
unordered_map − 它是 C++ 中的一种数据结构,用于存储具有唯一键值对的元素。其元素未按特定方式排序。
unordered_map<data_type> map_name;
size() − 它是 C++ 中的一个库函数,用于返回输入值的长度。它定义在 C++ 标准库中。
value.size();
auto − 它是 C++ 中的自动变量。它有助于在运行时声明变量的数据类型。
auto auto_name;
算法
输入一个数组。
声明一个变量,用于存储拼成 26 个字母所需的火柴数量。
声明一个变量,用于存储拼成 0 到 9 所需的火柴数量。
遍历每个数组元素,计算所需的火柴数量。
根据火柴数量对数组元素进行排序。
打印排序后的数组元素。
示例 1
我们用 C++ 实现了本教程的问题陈述。使用一个向量来存储数组字符串。
#include <bits/stdc++.h> using namespace std; // 用于存储组成 A 到 Z 字符所需火柴数量的数组 int matchsticksAlphabets[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 }; // 用于存储组成 0 到 9 数字所需火柴数量的数组 int matchsticksNumber[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // 使用 count 变量计算火柴数量的函数 int countingSticks(string strVar) { int cntVar = 0; // 通过 for 循环迭代数组中的每个字符串 for (int x = 0; strVar[x]; x++) { char charVar = strVar[x]; if (charVar >= 'A' && charVar <= 'Z') cntVar += matchsticksAlphabets[charVar - 'A']; else cntVar += matchsticksNumber[charVar - '0']; } return cntVar; } // 根据火柴数量对数组元素进行排序的函数 void sortingArr(string arrEle[], int s){ vector<pair<int, string> > vpVar; // 用于存储每个字符串的火柴数量的向量 for (int x = 0; x < s; x++) { vpVar.push_back(make_pair(countingSticks(arrEle[x]),arrEle[x])); } sort(vpVar.begin(), vpVar.end()); // 对向量元素进行排序的函数 cout << "排序后的数组为:"; for (int x = 0; x < vpVar.size(); x++) // 打印已排序元素的向量 cout << vpVar[x].second << " "; } int main(){ string arrEle[] = { "23", "HI", "ABC" }; int s = sizeof(arrEle) / sizeof(arrEle[0]); sortingArr(arrEle, s); //调用函数对数组进行排序 return 0; }
输出
The sorted array is: HI 23 ABC
示例 2
我们用 C++ 实现了一个教程问题:根据表示字符串字符所需的火柴数量对字符串进行排序。为了实现该方法,我们使用 map 及其函数,根据火柴数量对字符串进行排序。比较运算符用于对数组元素进行排序。
#include <iostream> #include <string> #include <algorithm> #include <unordered_map> using namespace std; // 数组,用于存储组成 A 到 Z 字母所需的火柴数量 int letterSticks[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 }; // 数组,用于存储组成 0 到 9 数字所需的火柴数量 int numberSticks[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // 声明用于计数火柴数量的函数 int countingSticks(const string& strVar, const unordered_map<char, int>& matchstickCounts) { int cntVar = 0; for (char charVar : strVar) { auto itVar = matchstickCounts.find(charVar); //声明自动变量 if (itVar != matchstickCounts.end()) cntVar += itVar->second; } return cntVar; } //使用映射函数查找更多火柴的函数 bool compareStringsByMatchsticks(const string& strVar1, const string& strVar2, const unordered_map<char, int>& matchstickCounts) { int matchsticks1 = countingSticks(strVar1, matchstickCounts); int matchsticks2 = countingSticks(strVar2, matchstickCounts); return matchsticks1 < matchsticks2; } // 声明一个函数,用于根据火柴数量对数组进行排序 void sortedArr(string arrVar[], int s, const unordered_map<char, int>& matchstickCounts) { sort(arrVar, arrVar + s, [&](const string& strVar1, const string& strVar2){ return compareStringsByMatchsticks(strVar1, strVar2, matchstickCounts); }); cout << "The sorted array elements are : "; for (int x = 0; x < s; x++) // 打印按火柴数量排序的数组 cout << arrVar[x] << " "; } int main() { unordered_map<char, int> matchstickCounts; // 定义用于存储字符串的映射 for (int x = 0; x < 26; x++) matchstickCounts['A' + x] = alphabetSticks[x]; for (int x = 0; x < 10; x++) matchstickCounts['0' + x] = numberSticks[x]; string arrVar[] = { "123", "HI", "ABC" }; int s = sizeof(arrVar) / sizeof(arrVar[0]); sortedArr(arrVar, s, matchstickCounts); return 0; }
输出
The sorted array elements are : HI 123 ABC
结论
本教程已结束。在本教程中,我们根据火柴棒的数量对字符串数组进行了排序。火柴棒由字符和数字组成。所需的火柴棒总数存储在两个数组中。我们使用 map 和 vector 实现了该任务的方法。map 和 vector 的不同函数用于通过查找数组中每个字符串的火柴棒数量来对数组元素进行排序。