[Solved] Cookery Contest with Java, C++, Python

Suzanne is participating in the Cookery Contest to be held at her Company. Suzanne is just a beginner in cooking but is more creative. She wanted to give a good show though she is going to cook for the first time. So she decided to cook only a small portion of her recipe, which has the same ratios of ingredients but makes less food.
 
Suzanne however, does not like fractions. The original recipe contains only whole numbers of ingredients, and Suzanne wants the reduced recipe to only contain whole numbers of ingredients as well. Help her determine how much of each ingredient to use in order to make as little food as possible.

Note: if it is not possible to reduce as whole ratio from the original ratio she will use the original ratio.
 
Input Format:
The first line of the input consists of a positive integer N, which corresponds to the number of ingredients.
The next line contains N space-separated integers, each indicating the quantity of a particular ingredient that is used.

Output Format:
Output exactly N space-separated integers on a line that gives the quantity of each ingredient that Suzanne should use in order to make as little food as possible.
Refer to sample input and output for formatting specifications.

Sample Input 1:
2
4 4

Sample Output 1:
1 1

Sample Input 2:
3
2 3 4

Sample Output 2:
2 3 4

Solution

import java.util.*;
class Main
{
    public static void main(String jr[])
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[10];
        int i,j,k,g=1;
        for(i=0;i<n;i++)
        {
          a[i] = sc.nextInt();
        }
         k=a[0];
        for(i=1;i<n;i++)
        {
           if(a[i]<k)
           k=a[i];
           //sauravhathi
        }
        for(i=1;i<=k;i++)
        {
        for(j=0;j<n;j++)
        {
            if(a[j]%i!=0)
            {
                break;
            }
        }
        if(j==n)
        g=i;
        }
        for(i=0;i<n;i++)
        {
        System.out.print(a[i]/g);
        }
        
}

}
include<stdio.h>
int main()
{
    int n, a[10], i, j, k, g = 1
    scanf("%d", & n)
    for(i=0
        i < n
        i++)
    {
        scanf("%d", & a[i])
    }
    k = a[0]
    for(i=1
        i < n
        i++)
    {
        if(a[i] < k)
        k = a[i]
    }
    for(i=1
        i <= k
        i++)
    {
        for(j=0
            j < n
            j++)
        {
            if(a[j] % i != 0)
            {
                break
            }
        }
        if(j == n)
        g = i
    }
    for(i=0
        i < n
        i++)
    {
        printf("%d ", a[i]/g)
    }
    return 0
}
n = int(input())
g = 1
a = list(map(int, input().split()))
k = a[0]
for i in range(1, n):
    if a[i] < k:
        k = a[i]
for i in range(1, k + 1):
    for j in range(0, n):
        if a[j] % i != 0:
            break
    if j == n:
        g = i
for i in range(0, n):
    print(a[i] / g, end=" ")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *