Problem:-
You have N rectangles. A rectangle is golden if the ratio of its sides is in between , both inclusive. Your task is to find the number of golden rectangles.
Input format
- First line: Integer denoting the number of rectangles
- Each of the following lines: Two integers denoting the width and height of a rectangle
Output format
- Print the answer in a single line.
Constraints
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
There are three golden rectangles: (165, 100), (170, 100), (160, 100).
Code:-
Solution 1 ( C language):-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isInRatio(double a, double b){
return (a/b>=1.6 && a/b<=1.7);
}
int main () {
char first[10] = {0};
fgets(first, 10, stdin);
int N = strtoll(first, NULL, 10);
int sum = 0;
for(int i = 0; i<N; i++){
char line[100]={0};
fgets(line, 100, stdin);
char *end;
int W = strtoll(line, &end, 10);
int H = strtoll(end, NULL, 10);
if(isInRatio(W, H)||isInRatio(H,W)){
sum++;
}
}
printf("%d\n", sum);
return 0;
}
Solution 2 ( C++ language):-
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define rep(i, k, n) for (long long int i = k; i < n; i++)
#define rept(i, k, n) for (auto i = k; i != n; ++i)
#define drep(i, k, n) for (long long int i = k; i >= n; i--)
#define endl '\n'
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll t;
cin>>t;
int count=0;
while(t-->0)
{
int w,h;
cin>>w>>h;
if(w>=1.6*h && w<=1.7*h)
{
count++;
}
else if(h>=1.6*w && h<=1.7*w)
{
count++;
}
// cout<<rat<<endl;
}
cout<<count<<endl;
}
Solution 3 (java language):-
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class GoldenRectangle {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
keywords:-
,golden rectangle hackerearth solutions,golden rectangle hackerearth solutions java,you have n rectangles a rectangle is golden,golden rectangle in java,golden string hackerearth solution,golden rectangle problem,popular shop problem hackerearth,special shop hackerearth solutions,
Recommended Post:-
- codechef problems:-
Wipro :-
- Update the booking ID | Wipro previous year question paper solution
- Pages in PDF
- Find the location id
- Find the odd digits
- Find the Product ID
Infytq :-
Key Points;-
Hackerrank:-
- Python : missing characters : hackerrank solution
- Python : string transformation | Hackerrank solution
- Active Traders certification test problem | Hackerrank Solution
- Usernames changes certification test problem | Hackerrank Solution
- string Representation of objects certification test hackerrank solution
- Average Function | hackerrank certification problem solution
C-tutorial:-
- Micros in C
- Pointer in c
- Function declaration
- Types of user define function
- return type of function
- 2D array
- c program to convert specified days into years weeks and days
- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation
0 Comments