📜  将单个数字打印为单词,而无需使用if或switch

📅  最后修改于: 2021-05-30 19:01:51             🧑  作者: Mango

给定一个数字,为单个数字打印单词。不允许使用if或switch。
例子:

Input:  n = 123
Output: One Two Three

Input:  n = 350
Output: Three Five Zero

强烈建议您最小化浏览器,然后自己尝试。
这个想法是使用字符串数组来存储数字到单词的映射。以下是步骤。
令输入数字为n。

  1. 创建一个字符串数组以存储数字到单词的映射。
  2. 创建另一个数组digits []来存储n的各个数字。
  3. 遍历n的数字并将其存储在digits []中。请注意,通过重复存储n%10并执行n = n / 10的标准遍历方式将以相反的顺序遍历数字。
  4. 从头到尾遍历digits数组,并使用在步骤1中创建的映射打印单词。

以下是上述想法的实现。

C++
// C++ program to print individual words without if and
// without switch
#include 
using namespace std;
 
// To store digit to word mapping
char word[][10] = {"zero", "one", "two", "three","four",
                   "five", "six", "seven", "eight", "nine"};
 
void printWordsWithoutIfSwitch(int n)
{
    // Store individual digits
    int digits[10]; // a 32 bit int has at-most 10 digits
 
    int dc = 0; // Initialize digit count for given number 'n'
 
    // The below loop stores individual digits of n in
    // reverse order. do-while is used to handle "0" input
    do
    {
        digits[dc] = n%10;
        n = n/10;
        dc++;
    } while (n != 0);
 
    // Traverse individual digits and print words using
    // word[][]
    for (int i=dc-1; i>=0; i--)
       cout << word[digits[i]] << " ";
}
 
// Driver program
int main()
{
    int n = 350;
    printWordsWithoutIfSwitch(n);
    return 0;
}


Java
// Java program to print individual words without
//  if and without switch
class GFG
{
 
// To store digit to word mapping
static String word[] = {"zero", "one", "two", "three","four",
                "five", "six", "seven", "eight", "nine"};
 
static void printWordsWithoutIfSwitch(int n)
{
    // Store individual digits
    int digits[] = new int[10]; // a 32 bit int has at-most 10 digits
 
    int dc = 0; // Initialize digit count for given number 'n'
 
    // The below loop stores individual digits of n in
    // reverse order. do-while is used to handle "0" input
    do
    {
        digits[dc] = n % 10;
        n = n/10;
        dc++;
    } while (n != 0);
 
    // Traverse individual digits and print words using
    // word[][]
    for (int i = dc - 1; i >= 0; i--)
        System.out.print(word[digits[i]] + " ");
}
 
// Driver program
public static void main(String[] args)
{
    int n = 350;
    printWordsWithoutIfSwitch(n);
}
}
 
// This code has been contributed by 29AjayKumar


C#
// C# program to print individual words without
// if and without switch
using System;
 
class GFG
{
 
// To store digit to word mapping
static String []word = {"zero", "one", "two", "three","four",
                "five", "six", "seven", "eight", "nine"};
 
static void printWordsWithoutIfSwitch(int n)
{
    // Store individual digits
    int []digits = new int[10]; // a 32 bit int has at-most 10 digits
 
    int dc = 0; // Initialize digit count for given number 'n'
 
    // The below loop stores individual digits of n in
    // reverse order. do-while is used to handle "0" input
    do
    {
        digits[dc] = n % 10;
        n = n/10;
        dc++;
    } while (n != 0);
 
    // Traverse individual digits and print words using
    // word[][]
    for (int i = dc - 1; i >= 0; i--)
        Console.Write(word[digits[i]] + " ");
}
 
// Driver program
public static void Main(String[] args)
{
    int n = 350;
    printWordsWithoutIfSwitch(n);
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python program to prindividual words without if and
# without switch
 
# To store digit to word mapping
word= ["zero", "one", "two", "three","four","five",
                "six", "seven", "eight", "nine"]
 
def printWordsWithoutIfSwitch(n):
 
    # Store individual digits
    digits = [0 for i in range(10)] # a 32 bit has at-most 10 digits
 
    dc = 0 # Initialize digit count for given number 'n'
 
    # The below loop stores individual digits of n in
    # reverse order. do-while is used to handle "0" input
    while True:
        digits[dc] = n%10
        n = n//10
        dc += 1
        if(n==0):
            break
 
    # Traverse individual digits and prwords using
    # word[][]
    for i in range(dc-1,-1,-1):
        print(word[digits[i]],end=" ")
 
# Driver program
n = 350
printWordsWithoutIfSwitch(n)
 
# This code is contributed by mohit kumar 29


Javascript


输出:

Three Five Zero
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”