如何按字母顺序对数组进行排序,同时将数字转换为单词?
简介
本教程处理按字母顺序对数组进行排序,同时将每个数字转换为单词的问题。将数字转换为单词意味着将数字更改为其数字名称。例如,65 是六十五。在这里,我们考虑一个数值数组,将所有数组元素转换为单词并按字母顺序排列。将单词转换为各自的数字后,打印排序后的数组元素。
演示 1
Input = Arr = {13, 1, 6, 7} Output = 1 7 6 13
解释
输入数组元素为 13、1、6、7
按字母顺序排序后的输出为 1 7 6 13。转换为单词后的数字如下所示 -
1 = one 7 = seven 6 = six 13 = thirteen
因此,所有数字均按其数字名称的字母顺序排序。
演示 2
Input = Ass = {17, 34, 65, 12, 10} Output = 17 65 10 34 12
解释
使用输入数组元素{17, 34, 65, 12, 10},转换为单词后的排序数字为 17 65 10 34 12。这些数字的单词转换如下 -
17 = Seventeen 65 = Sixty-five 10 = ten 34 = thirty-four 12 = twelve
因此,所有单词都按字母顺序排序。
C++ 库函数
sizeof − 它用于在编译时查找变量、数据类型的大小,因为它是编译时运算符。它是 C++ 中的关键字。
sizeof(value);
Vector − 它是 C++ 中的动态数组。它的所有函数都在 <vector> 头文件中定义。它是用于存储元素的数据结构之一,无需预定义大小。
vector<data_type> vector_name;
Map − 这是一种数据结构,用于存储元素及其映射值和键值。其所有元素都与键和映射值对相关联。键是唯一的,并以某种特定方式排序。
map <data_type> map_name;
vector.push_back() − 它是 <vector> 头文件中的预定义函数。它将元素推送或插入到向量的末尾。
vector_name.push_back(value);
vector.begin() − 这是 vector 头文件的预定义函数。它返回向量的起始位置。
vector_name.begin();
vector.end() −它是vector头文件的预定义函数。它返回向量的结束位置。
vector_name.end();
size() −它是标准的C++库函数。它返回输入字符串的长度。
string.size();
vector.sort() −这个vector类库函数按升序对vector元素进行排序。它需要参数来对向量元素进行排序。
sort(vector_name.begin(), vector_name.end());
算法
取一个数值数组。
将数组中的每个数字转换为单词。
要转换为单词,请考虑一个向量。
将数字名称保存到向量中。
对每个数字迭代向量以获取其单词形式。
将每个数字转换为单词后,按字母顺序对所有数字进行排序。
打印排序后的数组元素。
示例 1
实现一种方法,使用数字名称按字母顺序对数组元素进行排序。声明向量来存储所有数字的单词。当代码找到大于两位数的数字时,它会拆分该数字并确定其单词。遍历所有向量来找到每个数字的单词。
#include <bits/stdc++.h> using namespace std; string ones[] // storing words for the from 1 to 19 = { "", "one ", "two ", "three ","four ", "five ", "six ","seven ", "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ","fourteen ", "fifteen ", "sixteen ","seventeen ", "eighteen ", "nineteen "}; string tens[] // Variable for storing the words for the numbers multiple of 10 till 90 = { "", "", "twenty ","thirty ", "forty ","fifty ", "sixty" ,"seventy ", "eighty ","ninety " }; // 查找未存储在上述变量中的数字的单词的函数 string wordsForNum(int a, string str) { string s = ""; if (a > 19) // 当数字 os >19 时 s += tens[a / 10] + those[a % 10]; else s += those[a]; if (a) // 当数字非零时 s += str; return s; } // 将数字转换为单词并打印的函数 string convertingToWords(int a) { string result; result += wordsForNum((a / 10000000),"crore "); result += wordsForNum(((a / 100000) % 100),"lakh "); // 存储更大的数字 result += wordsForNum(((a / 1000) % 100),"thousand "); result += wordsForNum(((a / 100) % 10),"hundred "); if (a > 100 && a % 100) result += "and "; result += wordsForNum((a % 100), ""); return result; } // 将数字及其单词按字母顺序排序的函数 void sortingNum(int arrNum[], int a) { vector<pair<string, int> > vecNum; // 在向量变量中添加或插入数字的单词 for (int x = 0; x < a; x++) { vecNum.push_back(make_pair(convertingToWords(arrNum[x]), arrNum[x])); } sort(vecNum.begin(), vecNum.end()); for (int x = 0; x < vecNum.size(); x++) cout << vecNum[x].second << " "; } int main() { int arrNum[] = {17, 34, 65, 12, 10}; int a = sizeof(arrNum) / sizeof(arrNum[0]); sortingNum(arrNum, a); return 0; }
输出
17 65 10 34 12
示例 2
在此方法中,我们通过 C++ 将数组元素转换为单词后按字母顺序对其进行排序。此处,输出的是按排序顺序排列的数字单词。使用映射来存储数字单词。
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; map<int, string> numNames = { {0, "zero"}, {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"},{10, "ten"}, {11, "eleven"}, {12, "twelve"}, {13, "thirteen"}, {14, "fourteen"},{15, "fifteen"}, {16, "sixteen"}, {17, "seventeen"}, {18, "eighteen"}, {19, "nineteen"},{20, "twenty"}, {30, "thirty"}, {40, "forty"}, {50, "fifty"}, {60, "sixty"},{70, "seventy"}, {80, "eighty"}, {90, "ninety"} }; //将数字转换为文字的函数 string numToWords(int n) { if (n < 20) return numNames[n]; else if (n < 100) { if (n % 10 == 0) return numNames[n]; else return numNames[n / 10 * 10] + " " + numNames[n % 10]; } else if (n < 1000){ if (n % 100 == 0) return numNames[n / 100] + " hundred"; else return numNames[n / 100] + " hundred and " + numToWords(n % 100); } else return "Number out of range (0-999)"; } int main() { vector<int> number = {13, 1, 6, 7};// 使用预定义值定义数组 vector<string> numNamesArray; // 将数字转换为其对应的名称 for (int n : number) numNamesArray.push_back(numToWords(n)); sort(numNamesArray.begin(), numNamesArray.end()); // 按字母顺序对名称进行排序 cout << "按字母顺序对数组进行排序,其中单词为 :\n; for (const string& words : numNamesArray){ cout << words << endl; } return 0; }
输出
Sorted array in alphabetical order with words : one seven six thirteen
结论
我们已经到了本教程的结尾。在本教程中,我们根据数组元素的数字名称对数组元素进行排序。所有数组元素都是正数。我们通过示例演示了问题陈述,以详细说明任务的含义。
使用 C++ 实现了两种解决任务的方法,使用映射和向量来存储数组数字的单词。在其中一个输出中,我们打印了按字母顺序排序的数组数字名称。