给定一个数字,打印每个数字的单词。不允许使用if或switch。 例如:
null
Input: n = 123Output: One Two ThreeInput: n = 350Output: Three Five Zero
我们强烈建议您尽量减少浏览器,并先自己尝试。 其思想是使用字符串数组来存储数字到单词的映射。以下是步骤。 让输入数为n。
- 创建一个字符串数组来存储数字到单词的映射。
- 创建另一个数组digits[]以存储n的单个数字。
- 遍历n的数字并将其存储在数字[]中。请注意,通过重复存储n%10并执行n=n/10的标准遍历方式,以相反的顺序遍历数字。
- 从头到尾遍历digits数组,并使用步骤1中创建的映射打印单词。
下面是上述想法的实现。
C++
// C++ program to print individual words without if and // without switch #include <bits/stdc++.h> 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 print individual 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 print words 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
<script> // Javascript program to print individual words without // if and without switch // To store digit to word mapping let word=[ "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" ]; function printWordsWithoutIfSwitch(n) { // Store individual digits let digits = new Array(10); // a 32 bit int has at-most 10 digits let 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 = Math.floor(n/10); dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (let i = dc - 1; i >= 0; i--) document.write(word[digits[i]] + " " ); } // Driver program let n = 350; printWordsWithoutIfSwitch(n); // This code is contributed by unknown2108 </script> |
输出:
Three Five Zero
感谢乌特卡什·特里维迪提出上述解决方案。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END