Friday, 15 August 2014

Problem 17 Project Euler.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?

//answer=21103+11+10=21124
#include<String.h>
char *c;
char *pick(int x){
switch(x)
{
case 0:c="zero";break;
case 1:c="one";break;
case 2:c="two";break;
case 3:c="three";break;
case 4:c="four";break;
case 5:c="five";break;
case 6:c="six";break;
case 7:c="seven";break;
case 8:c="eight";break;
case 9:c="nine";break;
    case 10:c="ten";break;
    case 11:c="eleven";break;
    case 12:c="twelve";break;
    case 13:c="thirteen";break;
    case 14:c="fourteen";break;
    case 15:c="fifteen";break;
    case 16:c="sixteen";break;
    case 17:c="seventeen";break;
    case 18:c="eighteen";break;
    case 19:c="nineteen";break;
    case 20:c="twenty";break;
    case 30:c="thirty";break;
    case 40:c="forty";break;
    case 50:c="fifty";break;
    case 60:c="sixty";break;
    case 70:c="seventy";break;
    case 80:c="eighty";break;
    case 90:c="ninety";break;
    case 100:c="hundred";break;

    default:break;
    }

return c;


}


main()
{
int len;
int sum=0;
char *c1,*c2,*c3,*c4,*c5;
int num;
int x1,x2,x3,x4,x5;
scanf("%d",&num);
while(num>=1){
if(num<=20)
{
c1=pick(num);
c2=0;
len=strlen(c1);
sum+=len;
printf("\n%s%d",c1,len);

}





if(num>20){
    if(num<100){
        x1=(num/10)*10;

       c1=pick(x1);
        x2=num-x1;
        c2=pick(x2);
        if(c2=="zero"){c2="";}
   len=strlen(c1)+strlen(c2);
    sum+=len;
    printf("%s%s\n%d",c1,c2,len);
    }

if(num>100)
{
if(num<=120)
{
    x1=100;
    x2=num-100;
    c1=pick(x1);
    c2=pick(x2);
c3="and";
len=strlen(c1)+strlen(c2)+strlen(c3)+3;
sum+=len;
printf("%s%s%s%s","one",c1,c3,c2);
printf("\n%d",len);

}
if(num>120)
{
    x1=(num/100);
    x5=100;
    x2=((num-(x1*x5))/10)*10;

    x3=(num-(x1*x5))-x2;
c1=pick(x1);
c5=pick(x5);
c3="and";

c4=pick(x3);
if(x2==10){c4="";x2=num-(x1*x5);}
c2=pick(x2);
if(c2=="zero"){c2="";}
if(c4=="zero"){c4="";}
if(c2==""&&c4==""){c3="";}
len=strlen(c1)+strlen(c2)+strlen(c3)+strlen(c4)+strlen(c5);
sum+=len;
printf("%s %s %s %s %s\n%d",c1,c5,c3,c2,c4,len);
}


}





}





num--;
}
printf("n\n\nSUM=%d",sum);
}

No comments:

Post a Comment