Tuesday, February 18, 2014

program to find minimum number of strokes to type a word using old mobile keypads,

in this program,the first line input contains the number of letters in the word and the second line contains the frequencies of each letter,the third line contains number of buttons present in the mobile keypad and fourth line contains number of letters can be put in the each button

program:

#include <stdio.h>

int main()
{
int a,d=0,y=0,k=0,sum=0,s=0;
int a1[50],q[50];
int a2;
int a21[50];
int t,i,j;
int g=-1;
scanf("%d",&a);
if(a<1||a>50)
{
goto b;
}
for(i=0;i<a;i++)
{
scanf("%d",&a1[i]);
if(a1[i]<1||a1[i]>1000)
{
goto b;
}
}
for(i=0;i<a;i++)
{
for(j=i+1;j<a;j++)
{
if(a1[i]<a1[j])
{
t=a1[i];
a1[i]=a1[j];
a1[j]=t;
}
}

}
scanf("%d",&a2);
if(a2<1||a2>50)
{
goto b;
}
for(i=0;i<a2;i++)
{

scanf("%d",&a21[i]);
if(a21[i]<1||a21[i]>50)
{
goto b;
}
}
for(i=0;i<a2;i++)
{

s=s+a21[i];
}
if(s<a)
{
goto b;
}
for(i=0;i<a2;i++)
{
y=i;
for(j=0;j<a21[i];j++)
{
q[d]=a1[y];
d=d+1;
y=y+a21[i];
}

}
for(i=0;i<a2;i++)
{
for(j=0;j<a21[i];j++)
{
sum=sum+(q[k]*(j+1));
a=a-1;
k=k+1;
}
}
b:if(a==0)
{
printf("%d",sum);
}
else
{
printf("%d",g);
}

return 0;
}

output:

4
7 3 4 1
2
2 2

the minimum number of strokes is 19

screen shot:



the max number of letters we can take input is 50,if that number is greater than 50 or less than 1,the program will print -1 as output,after the frequencies take as input we sorted them and placed in different arrays based on the number of buttons and how many fitted in them.the output is -1 when the numbers of letters more than the number of words that can fit in the buttons

No comments:

Post a Comment