私が書いた次のコードがあり、完全に動作します。なぜそれが機能するのか理解に苦しむだけです。より具体的には、std::next_permutation を使用するために最初に配列をソートする必要があるのはなぜですか?どの構成からでも開始できません。
そして、私を最も悩ませているのは、なぜ sort(sides, sides+3) と next_permutation(sides, sides+3) を書かなければならないのか、なぜ「+3」! 配列に3つの要素があるからですか?任意の数の要素を使用していた場合はどうなりますか?
bool valid(int sides[], ofstream &outfile)
{
int i = 0;
for(; i < 3; i++) {
if(sides[i] <= 0) {
outfile << "This is not a valid triangle because it\n "
<< "contains negative sides or contains a\n"
<< "side length of 0\n\n";
return false;
}
}
do{
std::sort(sides,sides+3);
if(sides[0] + sides[1] > sides[2])
;
else{
outfile << "This is not a valid triangle because "
<< sides[0] << " + " << sides[1]
<< " is not greater than " << sides[2];
return false;
}
}while(std::next_permutation(sides,sides+3));
return true;
}