/* Warning: Donot try extracting answer from this and sumbit. This will not help you in Long Run */ /* Project Euler Problem 17 */ /* 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. */ #include<stdio.h> #include<string.h> int main() { int i,j; char str[100]; char str2[][100]={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred", "thousand"}; int tot=0; for(i=1;i<1000;i++) { //one digit number 1-9 if(i/10 == 0) { tot+=strlen(str2[i-1]); printf("%s\n",str2[i-1]); } //two digit number 10-99 else if(i/100 == 0) { if(i>=10 && i<=19) { tot+=strlen(str2[i-1]); printf("%s\n",str2[i-1]); } else if( i%10 == 0 ) { tot+=strlen(str2[18+i/10-1]); printf("%s\n",str2[18+i/10-1]); } else { int unit=i%10; int tens=(i/10)%10; tot=tot+strlen(str2[unit-1]) + strlen(str2[18+tens-1]); printf("%s-%s\n",str2[18+tens-1], str2[unit-1]); } } //three digit number 100-999 else if(i/1000 == 0) { tot+=strlen("hundred"); if(i%100 == 0) { tot= tot + strlen(str2[i/100-1]); printf("%s hundred\n",str2[i/100-1] ); } else if(i%10 == 0) { int hundredth=i/100; int tens=((i/10)%10); if(tens == 1) { tot=tot+strlen(str2[hundredth-1]) + strlen("ten")+strlen("and"); printf("%s hundred and ten\n",str2[hundredth-1]); } else { tot=tot+strlen(str2[hundredth-1])+strlen(str2[18+tens-1])+strlen("and"); printf("%s hundred and %s\n",str2[hundredth-1],str2[18+tens-1]); } } else { int hundredth=i/100; int tens=(i/10)%10; int units=i%10; tot=tot+strlen(str2[hundredth-1])+strlen("and"); printf("%s hundred and ", str2[hundredth-1]); if(tens == 1) { tot=tot+strlen(str2[9+units]); printf("%s\n",str2[9+units]); } else if(tens == 0) { tot=tot+strlen(str2[units-1]); printf("%s\n",str2[units-1]); } else { tot=tot+strlen(str2[18+tens-1]) + strlen(str2[units-1]); printf("%s-%s\n",str2[18+tens-1], str2[units-1]); } } } //sleep(1); } tot=tot+strlen("onethousand"); printf("one thousand\n"); printf("%d",tot); return 0; }
Recent Comments