<分区>
是否可以用字符串替换整数。例如,如果您有一个程序接受像 23933 这样的大整数,您可以这样做:
int 2 = "two" int 3 = "three" int 9 = "nine"
因此输出将如下所示:
"two three nine three three"
你会怎么做?
<分区>
是否可以用字符串替换整数。例如,如果您有一个程序接受像 23933 这样的大整数,您可以这样做:
int 2 = "two" int 3 = "three" int 9 = "nine"
因此输出将如下所示:
"two three nine three three"
你会怎么做?
最佳答案
void printNum(int x)
{
static const char * const num[] = {
"zero ", "one ", "two " , "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine "
};
if (x < 10) {
printf(num[x]);
return;
}
printNum(x / 10);
printNum(x % 10);
}
或者非常紧的版本:
void printNum(int x)
{
static const char * const num[] = {"zero ", "one ", "two ", "three ",
"four ", "five ", "six ", "seven ",
"eight ", "nine "};
(x < 10)? printf(num[x]) : (printNum(x / 10), printNum(x % 10));
}
关于C 编程 - 用字符串替换 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22409985/