私は4点を与えられていますが、x軸とy軸に正方形とその担保があるかどうかをチェックする必要があります。ここで与えられた点が正方形であるかどうかをチェックする
は私のコードです:
#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(第一の点でdecrased、そのプログラムでその3)同じ出現箇所があります。
任意の応答のためのThanx。 時々クラッシュし、間違った出力を出します...インデックスが範囲外になっていますか?
どの時点でクラッシュしますか?あなたはデバッガで何が見えますか? – pmr
まあ、私は知らない、そのシステムで私のチェックではない。私のテストでは大丈夫です。宿題タグを参照してください:)(btw、これを追加するには高価なものです) –
@David Heffernan広場は申し訳ありません... –