すべてのx値またはy値がすべて同じかどうかを "YES"と出力するプログラムを作成しようとしました。それ以外の場合、出力は「NO」になります。論理は、すべてのx値の最大値がすべてのx値の最小値と同じ場合、この値は変更されていないため、すべてのx値は同じです。 yの値と同じです。同じプログラムが実行するたびに異なる出力を与えます
しかし、出力によっては正しい結果が得られることがあり、時には(同じ入力に対して)出力されないこともあります。さらに、出力は規則的ではない。 (例えば、2正しい、間違って3、5正しい、間違った1など)ここで
は私のコードです:
#include <iostream>
#include <climits>
using namespace std;
int main(){
int n;
int minX,minY=INT_MAX;
int maxX,maxY=INT_MIN;
cin>>n;
while(n--){ //for the next n line
int x,y;
cin>>x>>y;
maxX=max(maxX,x);
//cout<<maxX<<" "; //comments I write to find out what the heck is happening
minX=min(minX,x); // This value changes irregularly, which I suspect is the problem.
//cout<<minX<<" ";
maxY=max(maxY,y);
//cout<<maxY<<" ";
minY=min(minY,y);
//cout<<minY<<endl;
}
if(maxX==minX||maxY==minY){ //If the x values or the y values are all the same, true
cout<<"YES";
}
else{
cout<<"NO";
}
return 0;
}
入力:それは作品
5
0 1
0 2
0 3
0 4
0 5
出力(とcouts私は)コメント:私はコメントしcouts)
と、それは動作しません出力(の0 0 1 1
0 0 2 1
0 0 3 1
0 0 4 1
0 0 5 1
YES
ワン
0 -1319458864 1 1 // Not all the wrong outputs are the same, each wrong output is different than the other wrong output.
0 -1319458864 2 1
0 -1319458864 3 1
0 -1319458864 4 1
0 -1319458864 5 1
NO
どうしてあなたが使用していないものがたくさん含まれていますか? – Rakete1111
'minX'と' maxX'を初期化することはありません。 – Cornstalks
^これは、プログラムを読んでもプログラムが未定義の動作をすることを意味します。 – StoryTeller