Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- typedef long long ll;
- void read_digit(ll n) {
- assert(n >= 0 && n <= 9);
- if (n == 1) printf("one");
- else if (n == 2) printf("two");
- else if (n == 3) printf("three");
- else if (n == 4) printf("four");
- else if (n == 5) printf("five");
- else if (n == 6) printf("six");
- else if (n == 7) printf("seven");
- else if (n == 8) printf("eight");
- else if (n == 9) printf("nine");
- }
- void read_two_digits(ll n) {
- assert(n >= 1 && n <= 99);
- if (n == 10) printf("ten");
- else if (n == 11) printf("eleven");
- else if (n == 12) printf("twelve");
- else if (n == 13) printf("thirteen");
- else if (n == 14) printf("fourteen");
- else if (n == 15) printf("fifteen");
- else if (n == 16) printf("sixteen");
- else if (n == 17) printf("seventeen");
- else if (n == 18) printf("eighteen");
- else if (n == 19) printf("nineteen");
- else {
- if (n/10 == 2) printf("twenty");
- else if (n/10 == 3) printf("thirty");
- else if (n/10 == 4) printf("forty");
- else if (n/10 == 5) printf("fifty");
- else if (n/10 == 6) printf("sixty");
- else if (n/10 == 7) printf("seventy");
- else if (n/10 == 8) printf("eighty");
- else if (n/10 == 9) printf("ninety");
- if (n/10 >= 2 && n%10 > 0) printf("-");
- read_digit(n%10);
- }
- }
- void read_three_digits(ll n) {
- assert(n >= 1 && n <= 999);
- if (n/100 > 0) {
- read_digit(n/100);
- printf(" hundred");
- if (n%100 > 0)
- printf(" and ");
- }
- read_two_digits(n%100);
- }
- // zone1 billion zone2 million zone3 thousand zone4
- void read_num(ll n) {
- assert(n >= 0 && n <= (ll)1e12-1);
- if (n == 0) {
- printf("zero");
- return;
- }
- ll zone4 = n % 1000;
- ll zone3 = (n/1000) % 1000;
- ll zone2 = (n/1000000) % 1000;
- ll zone1 = (n/1000000000) % 1000;
- bool comma = false;
- if (zone1 > 0) {
- read_three_digits(zone1); printf(" billion"); comma = true;
- }
- if (zone2 > 0) {
- if (comma) printf(", ");
- read_three_digits(zone2); printf(" million"); comma = true;
- }
- if (zone3 > 0) {
- if (comma) printf(", ");
- read_three_digits(zone3); printf(" thousand"); comma = true;
- }
- if (zone4 > 0) {
- if (comma) printf(", ");
- read_three_digits(zone4); comma = true;
- }
- }
- int main(void) {
- ll n;
- scanf("%lld", &n);
- read_num(n);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement