Advertisement
AquaBlitz11

Print number

Jun 22nd, 2020
1,544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. typedef long long ll;
  4.  
  5. void read_digit(ll n) {
  6.     assert(n >= 0 && n <= 9);
  7.     if (n == 1) printf("one");
  8.     else if (n == 2) printf("two");
  9.     else if (n == 3) printf("three");
  10.     else if (n == 4) printf("four");
  11.     else if (n == 5) printf("five");
  12.     else if (n == 6) printf("six");
  13.     else if (n == 7) printf("seven");
  14.     else if (n == 8) printf("eight");
  15.     else if (n == 9) printf("nine");
  16. }
  17.  
  18. void read_two_digits(ll n) {
  19.     assert(n >= 1 && n <= 99);
  20.     if (n == 10) printf("ten");
  21.     else if (n == 11) printf("eleven");
  22.     else if (n == 12) printf("twelve");
  23.     else if (n == 13) printf("thirteen");
  24.     else if (n == 14) printf("fourteen");
  25.     else if (n == 15) printf("fifteen");
  26.     else if (n == 16) printf("sixteen");
  27.     else if (n == 17) printf("seventeen");
  28.     else if (n == 18) printf("eighteen");
  29.     else if (n == 19) printf("nineteen");
  30.     else {
  31.         if (n/10 == 2) printf("twenty");
  32.         else if (n/10 == 3) printf("thirty");
  33.         else if (n/10 == 4) printf("forty");
  34.         else if (n/10 == 5) printf("fifty");
  35.         else if (n/10 == 6) printf("sixty");
  36.         else if (n/10 == 7) printf("seventy");
  37.         else if (n/10 == 8) printf("eighty");
  38.         else if (n/10 == 9) printf("ninety");
  39.         if (n/10 >= 2 && n%10 > 0) printf("-");
  40.         read_digit(n%10);
  41.     }
  42. }
  43.  
  44. void read_three_digits(ll n) {
  45.     assert(n >= 1 && n <= 999);
  46.     if (n/100 > 0) {
  47.         read_digit(n/100);
  48.         printf(" hundred");
  49.         if (n%100 > 0)
  50.             printf(" and ");
  51.     }
  52.     read_two_digits(n%100);
  53. }
  54.  
  55. // zone1 billion zone2 million zone3 thousand zone4
  56. void read_num(ll n) {
  57.     assert(n >= 0 && n <= (ll)1e12-1);
  58.     if (n == 0) {
  59.         printf("zero");
  60.         return;
  61.     }
  62.     ll zone4 = n % 1000;
  63.     ll zone3 = (n/1000) % 1000;
  64.     ll zone2 = (n/1000000) % 1000;
  65.     ll zone1 = (n/1000000000) % 1000;
  66.     bool comma = false;
  67.     if (zone1 > 0) {
  68.         read_three_digits(zone1); printf(" billion"); comma = true;
  69.     }
  70.     if (zone2 > 0) {
  71.         if (comma) printf(", ");
  72.         read_three_digits(zone2); printf(" million"); comma = true;
  73.     }
  74.     if (zone3 > 0) {
  75.         if (comma) printf(", ");
  76.         read_three_digits(zone3); printf(" thousand"); comma = true;
  77.     }
  78.     if (zone4 > 0) {
  79.         if (comma) printf(", ");
  80.         read_three_digits(zone4); comma = true;
  81.     }
  82. }
  83.  
  84. int main(void) {
  85.     ll n;
  86.     scanf("%lld", &n);
  87.     read_num(n);
  88.     printf("\n");
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement