package test1;
/*
* bhatt.umang7@gmail.com
*/
/*
* If the numbers 1 to 5 are written out in words: one, two, three, four, five,
* then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
*
* If all the numbers from 1 to 1000 (one thousand) inclusive were written out
* in words, how many letters would be used?
*
* NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
* forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
* letters. The use of "and" when writing out numbers is in compliance with
* British usage.
*/
// ans 21124
/**
* @author umang
*/
public class Problem_17
{
public static String _100toStr(int n)
{
String ans = "";
if (n % 100 == 0)
{
int temp = n;
temp /= 100;
ans = _1toStr(temp) + " hundred";
} else if (n % 100 >= 1 && n % 100 <= 9)
{
int temp = n;
int temp_1 = n % 10;
temp /= 100;
ans = _1toStr(temp) + " hundred and " + _1toStr(temp_1);
} else
{
int temp = n;
int temp_1 = n % 100;
temp /= 100;
ans = _1toStr(temp) + " hundred and " + _10toStr(temp_1);
}
return ans;
}
public static String _10toStr(int n)
{
int temp = n;
String value = "";
if (temp == 10)
{
value = "ten";
} else if (temp == 11)
{
value = "eleven";
} else if (temp == 12)
{
value = "twelve";
} else if (temp == 13)
{
value = "thirteen";
} else if (temp == 14)
{
value = "fourteen";
} else if (temp == 15)
{
value = "fifteen";
} else if (temp == 16)
{
value = "sixteen";
} else if (temp == 17)
{
value = "seventeen";
} else if (temp == 18)
{
value = "eighteen";
} else if (temp == 19)
{
value = "nineteen";
} else if (temp == 20)
{
value = "twenty";
} else if (temp > 20 && temp < 30)
{
value = "twenty " + _1toStr(temp % 10);
} else if (temp == 30)
{
value = "thirty";
} else if (temp > 30 && temp < 40)
{
value = "thirty " + _1toStr(temp % 10);
} else if (temp == 40)
{
value = "forty";
} else if (temp > 40 && temp < 50)
{
value = "forty " + _1toStr(temp % 10);
} else if (temp == 50)
{
value = "fifty";
} else if (temp > 50 && temp < 60)
{
value = "fifty " + _1toStr(temp % 10);
} else if (temp == 60)
{
value = "sixty";
} else if (temp > 60 && temp < 70)
{
value = "sixty " + _1toStr(temp % 10);
} else if (temp == 70)
{
value = "seventy";
} else if (temp > 70 && temp < 80)
{
value = "seventy " + _1toStr(temp % 10);
} else if (temp == 80)
{
value = "eighty"; // eighty
} else if (temp > 80 && temp < 90)
{
value = "eighty " + _1toStr(temp % 10);
} else if (temp == 90)
{
value = "ninety"; // ninety
} else if (temp > 90 && temp <= 99)
{
value = "ninety " + _1toStr(temp % 10);
}
return value;
}
public static String _1toStr(int n)
{
int temp = n;
String value = "";
String valueArr[] =
{
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
return valueArr[n];
}
public static String notoStr(int no)
{
String ans = "";
if (no < 10)
{
ans = _1toStr(no);
}
if (no >= 10 && no <= 99)
{
ans = _10toStr(no);
}
if (no > 99 && no < 1000)
{
ans = _100toStr(no);
}
return ans;
}
public static void main(String args[])
{
String ans = "";
for (int i = 1; i < 1000; i++)
{
ans = ans + "\n" + notoStr(i);
}
ans += "\none thousand";
System.out.println(ans);
// ans = notoStr(342);
long count = 0;
for (int i = 0; i < ans.length(); i++)
{
if (ans.charAt(i) != ' ' && ans.charAt(i) != '\n' && ans.charAt(i) != '-')
{
count++;
}
}
System.out.println(count);
}
}
|
No comments:
Post a Comment