4 점을 받았습니다. x 축과 y 축에 사각형이 있는지 확인해야합니다.주어진 점이 정사각형으로되어 있는지 C++로 확인하십시오.
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int wasIn(int x, int n[2])
{
for (int i=0; i<2; i++)
if (x==n[i]) return i;
return -1;
}
int main(int argc, char *argv[])
{
int x[4];
int y[4];
for (int i=0; i<4; i++)
cin>>x[i]>>y[i];
int was[2];
was[0]=-1001;
was[1]=-1001;
int countwas[2];
countwas[0]=0;
countwas[1]=0;
short old=0;
bool ok=true;
int tmp;
for (int i=0; i<4; i++)
{
if ((tmp=wasIn(x[i],was))==-1) {was[old]=x[i]; old++;} else countwas[tmp]++;
if ((tmp=wasIn(y[i],was))==-1) {was[old]=y[i]; old++;} else countwas[tmp]++;
if (old>2) { ok=false; break; }
}
if (ok && countwas[1]!=3 || countwas[0]!=3) ok=false;
//cout<<"C1: "<<countwas[0]<<endl; //debug
//cout<<"C2: "<<countwas[1]<<endl;
if (ok) cout<<"YES"; else cout<<"NO";
//system("PAUSE");
return EXIT_SUCCESS;
}
작동 원리 : 여기
내 코드입니다 단지에만 2 개 개의 다른 숫자가, 그리고 exacly이 4 (1 점 decrased의 3 프로그램 때문에) 같은 회 발생하는 경우 확인 .답장에 대한 고맙습니다. 때때로 충돌이 발생하고 잘못된 출력을줍니다 ... 범위를 벗어난 색인을 생성 할 수 있습니까?
언제 충돌합니까? 디버거에서 무엇을 볼 수 있습니까? – pmr
글쎄, 나도 몰라, 그 시스템에 의해 확인되지 않습니다. 내 테스트에서는 괜찮아. 숙제 태그보기 :) (btw, 이것을 추가하는 고맙습니다) –
@David Heffernan 광장은 유감스럽게 생각합니다. –