Monday, 13 November 2017

Code Challenge Solutions 4


1. Hanyu’s Kung Fu Match

Hanyu is glad as his academy of martial arts is chosen to present a warm-up match of Kung Fu at the inaugural ceremony of the Rio Olympics. 
There are 'n' Kung Fu fighters in Hanyu’s academy. 

Hanyu is very particular in selecting two fierce Kung Fu fighters from his academy who are going to put up a thrilling close competitive performance at the ceremony. 
  
For this purpose, he fetches the details of the winning points of all 'n' fighters in his academy and stores it in an array. 
He should now select the two fighters who have scored almost the same win percentages so far, as they would make the audience exhilarating till the end.  
  
Your aim is to help Hanyu find the difference in win percentages between two fighters chosen for the warm-up match. 
                                
Examples: 
Input  :  If n = 4 and winPoints[] = {30,5,20,9} 
Output :  4 

Input Format: 
The first line of the input consists of an integer n, which corresponds to the number of fighters in Hanyu’s academy. Assume that the maximum number of input elements in the array is 50. 
The next 'n' lines in the input corresponding to the winning points of each of the fighter. 
Output Format: 
The output is an integer – difference between two fighters chosen for the warm-up match. 

Refer sample input and output for formatting specifications. 

Sample Input1: 
19 
18 
25 
Sample Output1: 


Program


#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,diff,d;
int a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
diff=abs(a[0]-a[1]);
for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)
{
d=abs(a[i]-a[j]);
if(d<diff)
diff=d;
}

printf("%d",diff);
return 0;
}


2. Bhageera And Mowgli


Mowgli Bagheera is teaching mathematics to Mowgli. He has given N integer numbers to Mowgli and asked him “What is the sum of these N numbers?”Mowgli told him that he is not good in numbers yet and he can’t perform this addition operation but if Bagheera allow him to convert each integer into its nearest 10’s multiple, he can tell the sum of those given numbers. For e.g. if the last digit of the number is 5 or more than 5, he will convert it into nearest 10’s multiple that is greater than the given number. If last digit is less than 5, he will convert it into nearest 10’s multiple that is less than the number. After that he will count the sum of all these numbers and tell Bagheera.  Input Format:  First input is an integer N>=0 which is number of elements given. Next input is an array which will hold N number.  Output Format:  Output is also an integer which will hold the sum.  [All text in bold corresponds to input and the rest corresponds to output] Sample Input and Output:  5 11   16   15   24   4 70


Program:

#include <Stdio.h>
#include <conio.h>
main()
{
   int n,u,i,arr[100];
   int sum=0,temp=0;
   scanf ("%d", &n) ;
   for(i=0;i<n;i++)
   {
       scanf("%d", &arr[i]);
   }

   for(i=0;i<n;i++)
   {
       u = arr[i] % 10;
       if(u>=5)
       {
           temp=arr[i]+(10-u);
       }
       else{
        temp=arr[i]-u;
       }
       sum+=temp;
   }
   printf("\n%d",sum);
   return 0;

}





No comments:

Post a Comment