如果自然数小于或等于99,则将其打印为罗马数字。
有这方法吗?我该如何解决这个任务? Noob在这里提问。
答案 0 :(得分:0)
这段代码可行,应该是自我解释的。
#include <stdio.h>
void printRomanNumerals(int input);
int main(int argc, const char * argv[]) {
printf("Input your number!\n");
int i;
scanf("%d", &i);
printRomanNumerals(i);
return 0;
}
void printRomanNumerals(int input) {
switch (input / 10) {
case 1:
printf("%s","X");
break;
case 2:
printf("%s","XX");
break;
case 3:
printf("%s","xxx");
break;
case 4:
printf("%s","XL");
break;
case 5:
printf("%s","L");
break;
case 6:
printf("%s","LX");
break;
case 7:
printf("%s","LXX");
break;
case 8:
printf("%s","LXXX");
break;
case 9:
printf("%s","XC");
break;
default:
break;
}
switch (input % 10) {
case 1:
printf("%s","I");
break;
case 2:
printf("%s","II");
break;
case 3:
printf("%s","III");
break;
case 4:
printf("%s","IV");
break;
case 5:
printf("%s","V");
break;
case 6:
printf("%s","VI");
break;
case 7:
printf("%s","VII");
break;
case 8:
printf("%s","VII");
break;
case 9:
printf("%s","IX");
break;
default:
break;
}
}
答案 1 :(得分:0)
这些链接页面上的一些答案太复杂了。为什么新手会得到所有有趣的问题......
#include <stdio.h>
const char *digits[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
const char *prefixes[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
#define LIMIT sizeof(digits) / sizeof(char *) * sizeof(prefixes) / sizeof(char *)
int main() {
int i = 0;
do {
printf("Da mihi numeralis (0 si velis relinquere): ");
(void) scanf("%2i", &i);
(void) fpurge(stdin);
if (0 < i && i < LIMIT) {
printf("%s%s\n", prefixes[i / 10], digits[i % 10]);
}
} while (i > 0);
return 0;
}