codechef program #21 Cutting Recipes;codechef program #22: Puppy and Sum

RECIPE


  1. #1:PROBLEM STATEMENT:
  2. The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredients, but makes less food. The chef, however, does not like fractions. The original recipe contains only whole numbers of ingredients, and the chef wants the reduced recipe to only contain whole numbers of ingredients as well. Help the chef determine how much of each ingredient to use in order to make as little food as possible.

    Input

    Input will begin with an integer T, the number of test cases. Each test case consists of a single line. The line begins with a positive integer N, the number of ingredients. N integers follow, each indicating the quantity of a particular ingredient that is used.

    Output

    For each test case, output exactly N space-separated integers on a line, giving the quantity of each ingredient that the chef should use in order to make as little food as possible.

    Sample Input

    3
    2 4 4
    3 2 3 4
    4 3 15 9 6
    

    Sample Output

    1 1
    2 3 4
    1 5 3 2
    

    Constraints

    T≤100
    2≤N≤50
    All ingredient quantities are between 1 and 1000, inclusive.

My Solution:
  1. import java.util.Scanner;
  2. class recipe
  3. {
  4. public static int GCF(int a, int b) {
  5. if (b == 0) return a;
  6. else return (GCF (b, a % b));
  7. }
  8. public static void main(String args[])
  9. {
  10. Scanner sc=new Scanner(System.in);
  11. int counter=sc.nextInt();
  12. for(int i=0;i)
  13. {int arr[]= new int[sc.nextInt()];
  • for(int j=0;jlength;j++)
  • {
  • arr[j]=sc.nextInt();
  • }
  • int t=GCF(arr[0],arr[1]);
  • for(int y=0;ylength
  • ;y++)
  • {
  • t=GCF(arr[y],t);
  • }
  • for(int k=0;klength
  • ;k++)
  • {
  • System.out.println((arr[k]/t));
  • }
  • }
  • }
  • }



    1. #2:PROBLEM STATEMENT:
      Yesterday, puppy Tuzik learned a magically efficient method to find the sum of the integers from 1 to N. He denotes it as sum(N). But today, as a true explorer, he defined his own new function: sum(D, N), which means the operation sum applied D times: the first time to N, and each subsequent time to the result of the previous operation.
      For example, if D = 2 and N = 3, then sum(2, 3) equals to sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 21.
      Tuzik wants to calculate some values of the sum(D, N) function. Will you help him with that?

      Input

      The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing two integers D and N.

      Output

      For each testcase, output one integer on a separate line.

      Constraints

      • 1 ≤ T ≤ 16
      • 1 ≤ D, N ≤ 4

      Example

      Input:
      2
      1 4
      2 3
      
      Output:
      10
      21
      

      Explanation:

      The first test case: sum(1, 4) = sum(4) = 1 + 2 + 3 + 4 = 10.

      The second test case: sum(2, 3) = sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 1 + 2 + 3 + 4 + 5 + 6 = 21.


      My Solution:

      import java.util.Scanner;


      public class pam 
      {
      public static int function(int a)
      {
      int sum=0;
      for (int yo=1;yo<=a;yo++)
      {
      sum+=yo;
      }

      return sum;
      }
      public static void main(String args[])
      {
      Scanner in=new Scanner(System.in);
      int op=in.nextInt();
      for (int a=0;a
      {
      int b=in.nextInt();
      int c=in.nextInt();
      int d=c;
      for(int qually=0;qually
      {
      d=function(d);
      }
      System.out.println(d);
      }

      }
      }

      Comments

      Popular Posts