PROBLEM LINK:
https://www.codechef.com/problems/GOC103
https://www.codechef.com/GMCD2016/problems/GOC103
Author: https://www.codechef.com/users/nilesh_dbit
Tester: https://www.codechef.com/users/nilesh_dbit
Editorialist: https://www.codechef.com/users/nilesh_dbit
DIFFICULTY:
EASY
PREREQUISITES:
Matrix multiplication
PROBLEM:
Three people denoted by P1 , P2 , P3 intend to buy some rolls, buns, cakes and bread.
Each of them needs these commodities in differing amounts and can buy them in two shops S1 , S2 .
Which shop is the best for every person P1 , P2 , P3 to pay as little as possible?
The individual prices and desired quantities of the commodities are given in the following tables:
For example, the amount spent by the person P1 in the shop S1 is:
6 · 1 + 5 · 2 + 3 · 5 + 1 ·16 = 16
and in the shop S2 :
6 · 1 + 5 · 3 + 3 · 4 + 1 ·17 = 21,
for the other people similarly.
AUTHOR’S AND TESTER’S SOLUTIONS:
#include<stdio.h>
int main()
{
int a1[10][10],a2[10][10],a[10][10],i,j,k,r,s,t,sum=0;
scanf("%d",&r);
scanf("%d",&s);
scanf("%d",&t);
if((r>=2 && r<=5) && (s>=2 && s<=5) && (t>=2 && t<=5))
{
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",&a1[i][j]);
}
}
for(i=0;i<s;i++)
{
for(j=0;j<t;j++)
{
scanf("%d",&a2[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
for(k=0;k<t;k++)
{
sum=sum+a1[i][k]*a2[k][j];
}
a[i][j]=sum;
sum=0;
}
}
for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
else
printf("No. of rows/column is less than 2 or greater than 5");
return 0;
}