使用C语言写代码实现下面目的,输入一个int范围内的整数,输出对应的中文数字。(超出int范围不要求) 例如输入1234567,输出位一百二十三万四千五百六十七
时间: 2024-05-11 20:14:53 浏览: 9
好的,我可以回答这个问题!以下是用C语言实现的代码:
```
#include <stdio.h>
void int_to_cn(int n) {
if (n < 0) {
putchar('负');
n = -n;
}
if (n >= 100000000) {
int_to_cn(n / 100000000);
printf("亿");
n %= 100000000;
if (n == 0) return;
}
if (n >= 10000) {
int_to_cn(n / 10000);
printf("万");
n %= 10000;
if (n == 0) return;
}
if (n >= 1000) {
int_to_cn(n / 1000);
printf("千");
n %= 1000;
if (n == 0) return;
}
if (n >= 100) {
int_to_cn(n / 100);
printf("百");
n %= 100;
if (n == 0) return;
}
if (n >= 10) {
if (n >= 20) {
putchar('十');
int_to_cn(n % 10);
} else {
printf("十");
if (n == 10) return;
putchar('零');
int_to_cn(n % 10);
}
} else {
switch (n) {
case 1: printf("一"); break;
case 2: printf("二"); break;
case 3: printf("三"); break;
case 4: printf("四"); break;
case 5: printf("五"); break;
case 6: printf("六"); break;
case 7: printf("七"); break;
case 8: printf("八"); break;
case 9: printf("九"); break;
}
}
}
int main() {
int n;
scanf("%d", &n);
int_to_cn(n);
return 0;
}
```
如果输入1234567,输出的结果应该是:“一百二十三万四千五百六十七”。希望我的回答能帮到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)