fork download
  1. #include <stdio.h>
  2.  
  3. void f(int n)
  4. {
  5. int nums[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
  6. char *marks[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
  7. int f[13];
  8. int nn = n;
  9.  
  10. for (int i = 12; i >= 0; i--)
  11. {
  12. f[i] = nn / nums[i];
  13. nn = nn % nums[i];
  14. }
  15.  
  16. for (int i = 12; i >= 0; i--)
  17. {
  18. for (int j = f[i]; j > 0; j--)
  19. {
  20. printf("%s", marks[i]);
  21. }
  22. }
  23.  
  24. printf("\n");
  25. }
  26.  
  27. int main(void)
  28. {
  29. for (int i = 0; i <= 10; i++)
  30. {
  31. printf("%d ", i);
  32. f(i);
  33. }
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
0 
1 I
2 II
3 III
4 IV
5 V
6 VI
7 VII
8 VIII
9 IX
10 X