Golden rectangles

A rectangle is golden if the ratio of its sides is in between  [1.6,1.7both inclusive. Find the number of golden rectangles.

Example

Input: 
5
10 1
165 100
180 100
170 100
160 100
Output: 3

Approach:

Java

import java.util.Scanner;

public class GoldenRectangles {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        in.nextLine();
        int cnt = 0;
        for (int i = 0; i < N; i++) {
            String nL[] = in.nextLine().split(" ");
            double w = Double.parseDouble(nL[0]);
            double h = Double.parseDouble(nL[1]);
            if (goldenRect(w, h)) {
                cnt++;
            }
        }
        in.close();
        System.out.println(cnt);
    }

    private static boolean goldenRect(double wdouble h) {
        double l = 1.6, r = 1.7;
        double p = w / h;
        double q = h / w;
        if ((p >= l && p <= r) || (q >= l && q <= r))
            return true;
        return false;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    double w,h;
    cin>>n;
    int cnt=0;
    for(int i=0;i<n;i++)
     {
         cin>>w>>h;
         double p=w/h;
         double q=h/w;
         double l=1.6,r=1.7;
         if((p>=l&&p<=r)||(q>=l&&q<=r))
         cnt++;
     }
    cout<<cnt<<"\n";
}



No comments:

Post a Comment