给定一个整数N。任务是将数字转换为字符。
例子:
Input: N = 74254
Output: Seven four two five four
Input: N = 23
Output: Two three
一种有效的方法:
- 倒数。
- 从右到左遍历反转的数字。
- 通过使用模数提取最后一位数字,然后使用切换大小写来获取相应的单词。
- 迭代时,将数字除以10。
C++
// C++ program to convert number in characters
#include
using namespace std;
void NumbertoCharacter(int n)
{
int rev = 0, r = 0;
// To calculate the reverse of the number
while (n > 0) {
// The remainder will give the last digit of the number
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
while (rev > 0) {
// Extract the first digit of the reversed number
r = rev % 10;
// Match it with switch case
switch (r) {
case 1:
cout << "one ";
break;
case 2:
cout << "two ";
break;
case 3:
cout << "three ";
break;
case 4:
cout << "four ";
break;
case 5:
cout << "five ";
break;
case 6:
cout << "six ";
break;
case 7:
cout << "seven ";
break;
case 8:
cout << "eight ";
break;
case 9:
cout << "nine ";
break;
case 0:
cout << "zero ";
break;
default:
cout << "UnValid ";
break;
}
// Divide the number by 10 to get the next number
rev = rev / 10;
}
}
// Driver code
#include
int main()
{
int n = 12345;
NumbertoCharacter(n);
return 0;
}
Java
// Java program to convert number in characters
class GFG{
static void NumbertoCharacter(int n)
{
int rev = 0, r = 0;
// To calculate the reverse of the number
while (n > 0)
{
// The remainder will give
// the last digit of the number
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
while (rev > 0)
{
// Extract the first digit
// of the reversed number
r = rev % 10;
// Match it with switch case
switch (r)
{
case 1:
System.out.print("one ");
break;
case 2:
System.out.print("two ");
break;
case 3:
System.out.print("three ");
break;
case 4:
System.out.print("four ");
break;
case 5:
System.out.print("five ");
break;
case 6:
System.out.print("six ");
break;
case 7:
System.out.print("seven ");
break;
case 8:
System.out.print("eight ");
break;
case 9:
System.out.print("nine ");
break;
case 0:
System.out.print("zero ");
break;
default:
System.out.print("UnValid ");
break;
}
// Divide the number by 10
// to get the next number
rev = rev / 10;
}
}
// Driver code
public static void main(String[] args)
{
int n = 12345;
NumbertoCharacter(n);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to convert
# number in characters
def NumbertoCharacter(n):
rev = 0; r = 0;
# To calculate the
# reverse of the number
while (n > 0):
# The remainder will give
# the last digit of the number
r = n % 10;
rev = rev * 10 + r;
n = n // 10;
while (rev > 0):
# Extract the first digit
# of the reversed number
r = rev % 10;
# Match it with switch case
switcher = {
0 : "zero ",
1 : "one ",
2 : "two ",
3 : "three ",
4 : "four ",
5 : "five ",
6 : "six ",
7 : "seven ",
8 : "eight ",
9 : "nine "
}
print( switcher.get(r, "UnValid"),
end = " ") ;
# Divide the number by 10
# to get the next number
rev = rev // 10;
# Driver code
if __name__ == '__main__':
n = 12345;
NumbertoCharacter(n);
# This code is contributed by gauravrajput1
C#
// C# program to convert number in characters
using System;
class GFG{
static void NumbertoCharacter(int n)
{
int rev = 0, r = 0;
// To calculate the reverse
// of the number
while (n > 0)
{
// The remainder will give
// the last digit of the number
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
while (rev > 0)
{
// Extract the first digit
// of the reversed number
r = rev % 10;
// Match it with switch case
switch (r)
{
case 1:
Console.Write("one ");
break;
case 2:
Console.Write("two ");
break;
case 3:
Console.Write("three ");
break;
case 4:
Console.Write("four ");
break;
case 5:
Console.Write("five ");
break;
case 6:
Console.Write("six ");
break;
case 7:
Console.Write("seven ");
break;
case 8:
Console.Write("eight ");
break;
case 9:
Console.Write("nine ");
break;
case 0:
Console.Write("zero ");
break;
default:
Console.Write("UnValid ");
break;
}
// Divide the number by 10
// to get the next number
rev = rev / 10;
}
}
// Driver code
public static void Main(String[] args)
{
int n = 12345;
NumbertoCharacter(n);
}
}
// This code is contributed by sapnasingh4991
输出:
one two three four five
时间复杂度: O(k)
k是数字的长度。
空间复杂度: O(1)