पहले और आखिरी में आयतों की भुजाएँ और श्रेणी चर दिए गए हैं। लक्ष्य उन आयतों की संख्या ज्ञात करना है जिनकी भुजा की लंबाई/चौड़ाई का अनुपात [पहले, अंतिम] सीमा में है।
उदाहरण के लिए
इनपुट
rec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}} and first = 1.0, last = 1.6 आउटपुट
Count of number of rectangles such that ratio of sides lies in the range [a,b] are: 4
स्पष्टीकरण
The sides that have ratio in the range [ 1.0,1.6 ] are :
{200,210}, {300,190}, {180,200}, {300,200} इनपुट
rec[] = { { 10,20 }, { 30, 10 }, { 100, 500}, {900, 300}, {450, 90}} and
first = 3.0, last = 4.0 आउटपुट
Count of number of rectangles such that ratio of sides lies in the range [a,b] are: 2
स्पष्टीकरण
The sides that have ratio in the range [ 3.0,4.0 ] are :
{30,10}, {900,300} नीचे दिए गए कार्यक्रम में उपयोग किया गया दृष्टिकोण इस प्रकार है -
इस दृष्टिकोण में हम जोड़ी की एक सरणी के रूप में पक्ष लेंगे
-
एक सरणी rec[] प्रकार की जोड़ी लें
। -
श्रेणी निर्धारित करने के लिए पहले और अंतिम दो चर लें।
-
फंक्शन रेशियो_साइड्स (जोड़ी
rec[], int Total, double first, double last) आयतों की भुजाएँ लेता है और आयतों की संख्या की गणना इस तरह लौटाता है कि पक्षों का अनुपात [a,b] की सीमा में हो। -
प्रारंभिक गणना 0 के रूप में लें।
-
लूप के लिए i=0 से i
-
जोड़ी rec[i] में maxi =max(rec[i].first, rec[i].second) के रूप में बड़ा मान निकालें।
-
जोड़ी rec[i] में छोटा मान निकालें जैसे mini =min(rec[i].first, rec[i].second).
-
औसत =मैक्सी/मिनी की गणना करें।
-
यदि औसत का मान श्रेणी में है [ प्रथम, अंतिम ], तो वृद्धि गणना।
-
लूप के अंत में परिणाम के रूप में वापसी की गणना करें ..
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int ratio_sides(pair<int, int> rec[], int total, double first, double last){
int count = 0;
for (int i = 0; i < total; i++){
double maxi = max(rec[i].first, rec[i].second);
double mini = min(rec[i].first, rec[i].second);
double average = maxi/mini;
if (average >= first){
if(average <= last){
count++;
}
}
}
return count;
}
int main(){
pair<int, int> rec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}};
int total = 5;
double first = 1.0, last = 1.6;
cout<<"Count of number of rectangles such that ratio of sides lies in the range [a,b] are: "<<ratio_sides(rec, total, first, last);
return 0;
} आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा -
Count the number of rectangles such that ratio of sides lies in the range [a,b] are: 4