Sunday 12 November 2017

Code Challenge Solutions

  1. Seat Belt Problem


Dr. Dexter's car's seat belt detectors have been corrupted. So he wants you to build a module which checks if all the seat belts are worn. If and only if all the seat belts are worn Xinyou must start driving
Write a C program module that checks if all the seat belts have been worn.


Input and Output Format:

Input consists of n+1 lines.

The first line of input is an integer 'n' corresponding to the number of seats. 0 < n < 32767

The next n lines consist of strings 'yes' or 'no' corresponding to whether the seat belts have been worn or not.

Output consists of a single string 'Yes I can drive', 'No I can't drive' or 'Invalid Input'

Assume that all inputs consist of lowercase characters only.

All text in bold corresponds to input and the rest to output


Sample Input1:
2
no
no
Sample Output 1:
No I can't drive


Sample Input 2:
3
yes
yes
yes
Sample Output 2:
Yes I can drive

Sample Input 3:
3
No
Yes
Yes
Sample Output 3:
Invalid Input


Program

#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
int n,i,count=0;
scanf("%d",&n);
if( n>0  )
{
for(   i=0;i<n;i++   )
{
scanf("%s",a);
if(strcmp(a,"yes") == 0)
{
count++;
}
else if( strcmp(a,"No") == 0 || strcmp(a,"Yes") == 0)
{
printf("\nInvalid Input");
return 0;
}
}
if(   count == n     )
printf("Yes I can drive");
else
printf("No I can't drive");
}
else
printf("Invalid Input");
return 0;
}


2.Petrol Variety Problem


Dr. Dexter is now in the gas bunk.

 He has perfectly planned his trip. 
He has been working keenly on the budget so that the trip goes on well.
 Now, he finds that there are two varieties of fuel – Ordinary and Speed.
 Speed fuel yields him a fairly better performance than its ordinary counterpart. 
He wants Xinyou to find out as to which variety of fuel he must opt for to stick to the budget.
Write a C program to find out the variety of fuel.
Input and Output Format:
Input consists of 5 lines.
The first line of input is a floating point number 'm' corresponding to the mileage Dr.Dexter's car yields.
The second line and third lines of input consists of floating point numbers 'o' and 's'
 corresponding to the costs of ordinary fuel and speed fuel respectively.
The fourth line of input is an integer 'd' corresponding to the distance between the bunk and Shinyao.
The fifth line of input is a floating point number 'b' corresponding to the budget for the fuel.
Output consists of a string 'Ordinary' or 'Speed'.
Sample Input 1: 
25 = m
75 = o
100 = s
3 = d
500 = b
Sample Output 1:
Speed
Sample Input 2: 
25
75
100
25
80
Sample Output 2:
Ordinary

Program

#include<stdio.h> 
int main() 
int m,o,s,d,b,i=0; 
scanf("%d%d%d%d%d",&m,&o,&s,&d,&b); 
if(d <= m) 
i++; 
else 
{while( d>m) 
d-=m; 
i++; 
if(b>=(i*s)) 
printf("\nSpeed"); 
else 
if(  b < (i*s)) 
printf("\nOrdinary"); 
}return 0; 

No comments:

Post a Comment