fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MS 1000
  6.  
  7. char oneDigit[][10] = {"Khong", "Mot", "Hai", "Ba", "Bon", "Nam", "Sau", "Bay", "Tam", "Chin"};
  8. char base[][10] = {"", "Muoi", "Tram", "Nghin", "Muoi", "Tram", "Trieu", "Muoi", "Tram", "Ty"};
  9.  
  10. int checkString(char s[]) {
  11. for (int i = 0; i < strlen(s); i++)
  12. if (s[i] < '0' || s[i] > '9')
  13. return 0;
  14. return 1;
  15. }
  16.  
  17. char *d2w(char s[]) {
  18. char *word = calloc(1, MS);
  19. int size = strlen(s);
  20. if (size == 0 || !checkString(s))
  21. return NULL;
  22. for (int i = 0; i < size; i++)
  23. s[i] -= '0';
  24. if (size == 1) {
  25. strcpy(word, oneDigit[(int)s[0]]);
  26. return word;
  27. }
  28. for (int i = 0, j = size - 1; i < j; i++, j--) {
  29. char t = s[i];
  30. s[i] = s[j];
  31. s[j] = t;
  32. }
  33. while (s[size - 1] == 0) {
  34. size--;
  35. }
  36. for (int i = size - 1; i >= 0; i--) {
  37. int k = i;
  38. while (k >= 10) {
  39. k -= 9;
  40. }
  41. if (!strcmp(oneDigit[(int)s[i]], "Khong") && !strcmp(base[k], "Muoi") && s[i - 1] != 0)
  42. strcat(word, "Linh ");
  43. else if (!strcmp(oneDigit[(int)s[i]], "Mot") && !strcmp(base[k], "Muoi"))
  44. strcat(word, "Muoi ");
  45. else if (s[i] != 0 || (!strcmp(base[k], "Tram") && (s[i - 2] != 0 || s[i - 1] != 0))) {
  46. if (s[i] == 5 && (i == 0 || s[i + 1] == 1)) {
  47. strcat(word, "Lam");
  48. strcat(word, " ");
  49. } else {
  50. strcat(word, oneDigit[(int)s[i]]);
  51. strcat(word, " ");
  52. }
  53. strcat(word, base[k]);
  54. strcat(word, " ");
  55. } else if (((i % 3 == 0) && (s[i + 1] != 0 || s[i + 2] != 0)) || (!strcmp(base[k - 6], "Nghin"))) {
  56. strcat(word, base[k]);
  57. strcat(word, " ");
  58. }
  59. }
  60. return word;
  61. }
  62.  
  63. int main() {
  64. char s[MS];
  65. while (fscanf(stdin, "%s", s) != EOF) {
  66. int k = 0;
  67. while (s[k] == '0') {
  68. k++;
  69. }
  70. for (int i = k; i < strlen(s); i++)
  71. printf("%c", s[i]);
  72. printf(" in words is: %s\n", d2w(s));
  73. }
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0s 9432KB
stdin
1000005
1010000
15
125
120
110
115
105
2001
1234567
5005105
5015110
2010000
2010
5555555
1050
5
25
1054
001
sda
1000001011111
1000001000000001
015120
stdout
1000005 in words is: Mot Trieu Khong Tram Linh Lam  
1010000 in words is: Mot Trieu Khong Tram Muoi Nghin 
15 in words is: Muoi Lam  
125 in words is: Mot Tram Hai Muoi Lam  
120 in words is: Mot Tram Hai Muoi  
110 in words is: Mot Tram Muoi  
115 in words is: Mot Tram Muoi Lam  
105 in words is: Mot Tram Linh Lam  
2001 in words is: Hai Nghin Khong Tram Linh Mot  
1234567 in words is: Mot Trieu Hai Tram Ba Muoi Bon Nghin Nam Tram Sau Muoi Bay  
5005105 in words is: Nam Trieu Khong Tram Linh Nam Nghin Mot Tram Linh Lam  
5015110 in words is: Nam Trieu Khong Tram Muoi Lam Nghin Mot Tram Muoi  
2010000 in words is: Hai Trieu Khong Tram Muoi Nghin 
2010 in words is: Hai Nghin Khong Tram Muoi  
5555555 in words is: Nam Trieu Nam Tram Nam Muoi Nam Nghin Nam Tram Nam Muoi Lam  
1050 in words is: Mot Nghin Khong Tram Nam Muoi  
5 in words is: Nam
25 in words is: Hai Muoi Lam  
1054 in words is: Mot Nghin Khong Tram Nam Muoi Bon  
1 in words is: Mot  
sda in words is: (null)
1000001011111 in words is: Mot Nghin Ty Khong Tram Linh Mot Trieu Khong Tram Muoi Mot Nghin Mot Tram Muoi Mot  
1000001000000001 in words is: Mot Trieu Khong Tram Linh Mot Ty Khong Tram Linh Mot  
15120 in words is: Muoi Lam Nghin Mot Tram Hai Muoi