📌  相关文章
📜  当每个数字转换为单词时,按字母顺序对数组进行排序

📅  最后修改于: 2021-09-06 06:02:46             🧑  作者: Mango

给定一个包含N 个非负整数的数组 arr[] ,任务是在将每个数字转换为单词时按字母顺序对这些整数进行排序。

例子:

方法:为了按字母顺序对数字进行排序,我们首先需要将数字转换为其单词形式。因此,想法是将每个元素及其词形存储在一个向量对中,然后根据数字对应的词对向量中的所有元素进行排序。所以:

  • 预先计算所有单位数字的词形并将其存储在一个数组中。
  • 预先计算并将所有十位数字的词形存储在另一个数组中。
  • 对余下的所有大于2位的数字,除以数字,加上词形。
  • 遍历数组 arr[] 中数字的每一位数字,并将数字的相应单词形式作为一对存储在向量中。
  • 遍历向量并根据单词对向量进行排序。
  • 最后,打印排序后的顺序。

下面是上述方法的实现:

C++
// C++ program to sort an array of
// integers alphabetically
 
#include 
using namespace std;
 
// Variable to store the word form of
// units digit and up to twenty
string one[]
    = { "", "one ", "two ", "three ",
        "four ", "five ", "six ",
        "seven ", "eight ", "nine ", "ten ",
        "eleven ", "twelve ", "thirteen ",
        "fourteen ", "fifteen ", "sixteen ",
        "seventeen ", "eighteen ", "nineteen " };
 
// Variable to store the word form of
// tens digit
string ten[]
    = { "", "", "twenty ",
        "thirty ", "forty ",
        "fifty ", "sixty ",
        "seventy ", "eighty ",
        "ninety " };
 
// Function to convert a two digit number
// to the word by using the above defined arrays
string numToWords(int n, string s)
{
    string str = "";
 
    // If n is more than 19, divide it
    if (n > 19)
        str += ten[n / 10] + one[n % 10];
    else
        str += one[n];
 
    // If n is non-zero
    if (n)
        str += s;
 
    return str;
}
 
// Function to print a given number in words
string convertToWords(int n)
{
    // Stores the word representation
    // of the given number n
    string out;
 
    // Handles digits at ten millions
    // and hundred millions places
    out += numToWords((n / 10000000),
                      "crore ");
 
    // Handles digits at hundred thousands
    // and one millions places
    out += numToWords(((n / 100000) % 100),
                      "lakh ");
 
    // Handles digits at thousands and
    // tens thousands places
    out += numToWords(((n / 1000) % 100),
                      "thousand ");
 
    // Handles digit at hundreds places
    out += numToWords(((n / 100) % 10),
                      "hundred ");
 
    if (n > 100 && n % 100)
        out += "and ";
 
    // Call the above function to convert
    // the number into words
    out += numToWords((n % 100), "");
 
    return out;
}
 
// Function to sort the array according to
// the albhabetical order
void sortArr(int arr[], int n)
{
    // Vector to store the number in words
    // with respective elements
    vector > vp;
 
    // Inserting number in words
    // with respective elements in vector pair
    for (int i = 0; i < n; i++) {
        vp.push_back(make_pair(
            convertToWords(arr[i]), arr[i]));
    }
 
    // Sort the vector, this will sort the pair
    // according to the alphabetical order.
    sort(vp.begin(), vp.end());
 
    // Print the sorted vector content
    for (int i = 0; i < vp.size(); i++)
        cout << vp[i].second << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 12, 10, 102, 31, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    sortArr(arr, n);
 
    return 0;
}


Python3
# Python3 program to sort an array of
# integers alphabetically
 
# Variable to store the word form of
# units digit and up to twenty
one = [ "", "one ", "two ", "three ",
        "four ", "five ", "six ",
        "seven ", "eight ", "nine ", "ten ",
        "eleven ", "twelve ", "thirteen ",
        "fourteen ", "fifteen ", "sixteen ",
        "seventeen ", "eighteen ", "nineteen " ]
 
# Variable to store the word form of
# tens digit
ten = [ "", "", "twenty ",
        "thirty ", "forty ",
        "fifty ", "sixty ",
        "seventy ", "eighty ",
        "ninety " ]
 
# Function to convert a two digit number
# to the word by using the above defined arrays
def numToWords(n, s):
 
    st = ""
 
    # If n is more than 19, divide it
    if (n > 19):
        st += ten[n // 10] + one[n % 10]
    else:
        st += one[n]
 
    # If n is non-zero
    if (n):
        st += s
 
    return st
 
# Function to print a given number in words
def convertToWords(n):
 
    # Stores the word representation
    # of the given number n
    out = ""
 
    # Handles digits at ten millions
    # and hundred millions places
    out += numToWords((n // 10000000),
                      "crore ")
 
    # Handles digits at hundred thousands
    # and one millions places
    out += numToWords(((n // 100000) % 100),
                      "lakh ")
 
    # Handles digits at thousands and
    # tens thousands places
    out += numToWords(((n // 1000) % 100),
                      "thousand ")
 
    # Handles digit at hundreds places
    out += numToWords(((n // 100) % 10),
                      "hundred ")
 
    if (n > 100 and n % 100):
        out += "and "
 
    # Call the above function to convert
    # the number into words
    out += numToWords((n % 100), "")
 
    return out
 
# Function to sort the array according to
# the albhabetical order
def sortArr(arr, n):
 
    # Vector to store the number in words
    # with respective elements
    vp = []
 
    # Inserting number in words
    # with respective elements in vector pair
    for i in range(n):
        vp.append((convertToWords(arr[i]),
                                  arr[i]));
    
    # Sort the vector, this will sort the pair
    # according to the alphabetical order.
    vp.sort()
 
    # Print the sorted vector content
    for i in range(len(vp)):
        print (vp[i][1], end = " ")
 
# Driver code
if __name__ == "__main__":
   
    arr = [ 12, 10, 102, 31, 15 ]
    n = len(arr)
 
    sortArr(arr, n)
 
# This code is contributed by chitranayal


Javascript


输出:
15 102 10 31 12

时间复杂度: O(N * log(N)) ,其中 N 是数组的大小

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live