如何根据火柴棒数量对字符串进行排序?
引言
在本教程中,我们将实现一种基于表示数字或字符所需的火柴棒数量对字符串进行排序的方法。在这个方法中,我们使用 N 根火柴棒对数组进行排序。数组可以包含数字、单词或两者兼而有之。火柴棒用于将它们排列成特定数字或字符的形状。
演示1
Input = Arr = ["1", "3", "4"] Output = The sorted array is 1 4 3
解释
在上面的输入数组中,数组元素为 1、3 和 4
数字 1 需要 1 根火柴棒
数字 3 需要 5 根火柴棒
数字 4 需要 4 根火柴棒
演示2
Input = Arr = ["23", "HI", "ABC" ] Output = The sorted array is 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++实现了教程问题陈述。使用 vector 来存储数组字符串。
#include <bits/stdc++.h>
using namespace std;
// array to store the number of matchsticks required to form characters from A to 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 };
//array to store the number of matchsticks required to from numbers from 0 to 9
int matchsticksNumber[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
// Function for counting the number of matchsticks using count variable
int countingSticks(string strVar) {
int cntVar = 0;
// iterating each string of the array through for loop
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;
}
// Function for sorting the array elements as per number of matchsticks
void sortingArr(string arrEle[], int s){
vector<pair<int, string> > vpVar; // Vector for storing the number of matchsticks for each string
for (int x = 0; x < s; x++) {
vpVar.push_back(make_pair(countingSticks(arrEle[x]),arrEle[x]));
}
sort(vpVar.begin(), vpVar.end()); // function for sorting the vector elements
cout << "The sorted array is: ";
for (int x = 0; x < vpVar.size(); x++) // Printing the vector with sorted elements
cout << vpVar[x].second << " ";
}
int main(){
string arrEle[] = { "23", "HI", "ABC" };
int s = sizeof(arrEle) / sizeof(arrEle[0]);
sortingArr(arrEle, s); //calling function for sorting the array
return 0;
}
输出
The sorted array is: HI 23 ABC
示例 2
我们使用 C++实现了本教程中基于表示其字符所需的火柴棒数量对字符串进行排序的问题。为了实现这种方法,我们使用 map 及其函数根据火柴棒的数量对字符串进行排序。比较运算符用于对数组元素进行排序。
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
//array for storing the number of matchsticks to form alphabets from A to Z
int alphabetSticks[] = { 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 };
// array for storing the number of matchsticks to form numbers from 0 to 9
int numberSticks[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
// declaring function for count the matchsticks
int countingSticks(const string& strVar, const unordered_map<char, int>& matchstickCounts) {
int cntVar = 0;
for (char charVar : strVar) {
auto itVar = matchstickCounts.find(charVar); //declaring automatic variable
if (itVar != matchstickCounts.end())
cntVar += itVar->second;
}
return cntVar;
}
//function for finding the more matchsticks using map functions
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;
}
// declaring a function for sorting the array with number of matchsticks
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++) // Printing the array sorted on the basis of the number of matchsticks
cout << arrVar[x] << " ";
}
int main() {
unordered_map<char, int> matchstickCounts; // Define map for storing the strings
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 的不同函数用于通过查找数组中每个字符串的火柴棒数量来对数组元素进行排序。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP