In: Computer Science
In C++!!!!
What do you do if you need to copy a 560x400mm image onto a standard sheet of US letter-size paper (which is about 216x280mm), while keeping the image as large as possible? You can rotate the image 90 degrees (so that it is in "landscape" mode), then reduce it to 50% of its original size so that it is 200x280mm. Then it will fit on the paper without overlapping any edges. Your job is to solve this problem in general.
Input: The input consists of one or more test cases, each of which is a single line containing four positive integers A, B, C, and D, separated by a space, representing an AxBmm image and a CxDmm piece of paper. All inputs will be less than one thousand. Following the test cases is a line containing four zeros that signals the end of the input.
Output: For each test case, if the image fits on the sheet of paper without changing its size (but rotating it if necessary), then the output is 100%. If the image must be reduced in order to fit, the output is the largest integer percentage of its original size that will fit (rotating it if necessary). Output the percentage exactly as shown in the examples below. You can assume that no image will need to be reduced to less than 1% of its original size, so the answer will always be an integer percentage between 1% and 100%, inclusive.
Example:
Example input: | Example output: |
---|---|
560 400 218 280 | 50% |
10 25 88 10 | 100% |
8 13 5 1 | 12% |
9 13 10 6 | 66% |
199 333 40 2 | 1% |
75 90 218 280 | 100% |
999 99 1 10 | 1% |
0 0 0 0 |
Hey there. This code will help you.
#include <iostream>
using namespace std;
int main(){
while(true)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
if(a==0 && b==0 && c==0 && d==0)
break;
//Finding which of the two dimensions of the paper is
greater
int lengthOfpaper, widthOfpaper;
if(c>d)
{
lengthOfpaper=c;
widthOfpaper=d;
}
else
{
lengthOfpaper=d;
widthOfpaper=c;
}
//Now it is ensured that lengthOfpaper has the greater value
//Similarly, we will do for the image
int lengthOfImage, widthOfImage;
if(a>b)
{
lengthOfImage=a;
widthOfImage=b;
}
else
{
lengthOfImage=b;
widthOfImage=a;
}
//Now it is ensured that lengthOfImage has the greater value.
//If the image needs to be rotated, the lengthOfImage having the greater value, will ensure it.
int lengthPercentage, widthPercentage; //To store percentage reduction required in length and width
//Calculating reduction required in length and width
lengthPercentage=(lengthOfpaper*100)/lengthOfImage;
widthPercentage=(widthOfpaper*100)/widthOfImage;
//Assigning the smaller value of the reduction to the answer
variable, because if it fits in, larger one will surely fit
in.
int reduction= lengthPercentage<widthPercentage?
lengthPercentage:widthPercentage;
if(reduction>100) //Reduction can not be more than
100%.
reduction=100;
cout<<reduction<<"%"<<endl;
}
}
What I've done is stored the larger dimension in length variable while smaller one in width variable. This will ensure that there is optimal rotation.
Then I've found ratio of both the dimensions and print the higher reduction value. Please note that, the smaller the numerical value stored in reduction variable, the higher the reduction is. The higher reduction will ensure that both dimensions completely fit.
Here's sample output :