我必须编写一个程序来找到具有属性的有理数。我编写了代码来检查属性,但现在我不知道如何检查所有有理数。我试过了
float rat;
for (int i=1 ; i ; ++i) {
for (int j=1 ; j ; ++j) {
rat = (float)i/(float)j;
if goodRat(rat) then return rat;
}
}
但它永远不会结束!它错过了太多。所以我尝试了这个
float rat;
while {
int i = random(1000) + 1;
int j = random(1000) + 1;
rat = (float)i/(float)j;
if goodRat(rat)
return rat;
}
但这仅在某些时候有效。我该如何解决这个问题?