0
我寫下面的代碼,但它給出了重複的輸出。如3,4,5和4,5,3和5,4,3。它顯示了相同的三聯體。我怎樣才能防止這一點?如何從此畢達哥拉斯三元組代碼過濾重複三元組?
#include <stdio.h>
int main(void){
int side1=1;
int side2=1;
int hypotenus=1;
int till;
int count=0;
printf("Till what number do you want to find triplets?");
scanf("%d",&till);
for(side1=1;side1<=till;side1++){
for(side2=1;side2<=till;side2++){
for(hypotenus=1;hypotenus<=till;hypotenus++){
if(hypotenus*hypotenus==side1*side1+side2*side2){
count++;
printf("%5d %5d %5d is a triple \n",side1,side2,hypotenus);
}
}
}
}
printf("\n");
printf("%d triplets found.",count);
return 0;
}
我會很驚訝地看到這個程序產生「4,5,3和5,4,3「作爲輸出。 「斜角」的值不能小於「side」或「side2」。 – 2013-02-27 14:08:45
你是對的,我只是給他們作爲例子。它只給出3 4 5和4 3 5.不是第三個 – Lyrk 2013-02-27 14:12:24