#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
using namespace std;
int main() {
ofstream myfile;
myfile.open("square.out");
freopen("square.in", "r", stdin);
vector<int> inputl; int temp;
while (cin >> temp) inputl.push_back(temp);
int x = 0, y = 0;
for(int i = 0;i < 2;i++) {
for(int j = 0;j < 2;j++) {
if(abs(inputl[i*2] - inputl[j*2 + 4]) > x) {
x = abs(inputl[i*2] - inputl[j*2 + 4]);
}
}
}
for(int i = 0;i < 2;i++) {
for(int j = 0;j < 2;j++) {
if(abs(inputl[i*2 + 1] - inputl[j*2 + 5]) > y) {
y = abs(inputl[i*2 + 1] - inputl[j*2 + 5]);
}
}
}
if(x > y) {
myfile << pow(x, 2);
} else {
myfile << pow(y, 2);
}
Idk why but my code isn’t working. I solved this easily in python with the same logic but it doesn’t work in c++ for some reason.
Make sure to read this for next time: How to ask for help on a problem
First off, idk why you are using arrays for a problem that has set input. For this problem, you only need to input 8 numbers, and you don’t need to use the while(cin >> temp)
. Secondly, you also have a syntax error at the end, there is no closing brace for main.
As for your approach to the problem, there’s a corner case that produces WA with your code. If you coded the exact same thing in python, then idk how you got accepted for it. I think you’re over-complicating this problem by a lot, I’m not sure why you’re brute forcing all of this. Try to figure out the corner case by yourself, but if you can’t, then I can give it to you.
I would think over your solution one more time, and think of an easier way to implement it. Hint: You don’t need iteration.
- Oh ok I will do that in the future sorry bout that
- Oh yeah I forgot to copy and paste the brace
- Wait is the corner case when one rectangle is completely inside of the other rectangle?
Also I just started comp coding like a few days ago so I still dont know much about algorithms and stuff, but thanks for the help
Yup, you got it right. Try to implement it now, and you should get accepted. Your code only checked the distance between the two rectangles, but if the distance between one rectangle was greater than the other one, then it would enclose the entire thing.