如何在將數字轉換為文字的同時按字母順序對陣列進行排序?


引言

本教程將處理在將每個數字轉換為文字的同時按字母順序對陣列進行排序的問題。將數字轉換為文字意味著將數字更改為其數字名稱。例如,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() - 此向量類庫函式按升序對向量元素進行排序。它採用引數對向量元素進行排序。

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 " };
   
// Function for finding the words for numbers that are not stored in the above variables
string wordsForNum(int a, string str) {   
   string s = "";
   if (a > 19)  // when a number os >19
      s += tens[a / 10] + ones[a % 10];
   else
      s += ones[a];
   if (a)    // when number is non-zero
      s += str;
   return s;
}

// Function for converting the numbers to words and printing them
string convertingToWords(int a)   {   
   string result;
   result += wordsForNum((a / 10000000),"crore ");
   result += wordsForNum(((a / 100000) % 100),"lakh ");     // storing greater numbers
   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;
}

// Function for sorting the numbers alphabetically with their words
void sortingNum(int arrNum[], int a)  {   
   vector<pair<string, int> > vecNum;
   
   // adding or inserting the words for the numbers in the vector variable
   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"} 
};

//function for converting number to words
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};// Define the array with predefined values
   
   vector<string> numNamesArray;  // Convert numbers to their corresponding names
   for (int n : number)
      numNamesArray.push_back(numToWords(n));
   sort(numNamesArray.begin(), numNamesArray.end());     // Sort the names alphabetically 
   cout << "Sorted array in alphabetical order with words : \n;
   for (const string& words : numNamesArray){
      cout << words << endl;
   }
   return 0;
}

輸出

Sorted array in alphabetical order with words : 
one
seven
six
thirteen

結論

我們已經完成了本教程。在本教程中,我們根據陣列元素的數字名稱對陣列元素進行了排序。所有陣列元素都是正數。我們用示例演示了問題陳述,以詳細說明任務的含義。

我們使用 C++ 來實現兩種解決任務的方法,使用對映和向量來儲存陣列數字的文字。在一個輸出中,我們列印了陣列的按字母順序排序的數字名稱。

更新於:2023年10月3日

瀏覽量:114

開啟您的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.