我需要有关此功能的帮助,对于仅由两个数字(1和数字本身)分开的数字,它是正确的。这是一个测试功能。因此,用户获得3个号码,如果他们是"只有两个"则必须写。我不确定如何调用该功能,因此用户可以使用"是" "否"
bool test_prastevil(int prastevilo) {
int c=0;
for(int i=1;i<=prastevilo;i++) {
if(prastevilo%i==0) {
c++;
}
}
if (c==2) {
return true;
//if(stevec==1)re++;}
} else {
return false;
}
}
答案 0 :(得分:0)
编辑:帖子的这部分内容可以删除。:)
/ * --------------------------------------------- ---
例如,可以通过以下方式编写函数。我假设1(或-1)满足条件。
bool test_prastevil(int prastevilo)
{
unsigned int value = std::abs( prastevilo );
unsigned int x = 1;
while ( x * x < value && value % x != 0 ) x++;
return value != 0 && x * x == value;
}
----------------------------------------------- - * /
如果你需要一个确定数字是否为prome(除了1)的函数,那么你可以写
bool test_prastevil( int prastevilo )
{
unsigned int x = std::abs( prastevilo );
bool prime = ( x == 2 ) || ( x % 2 != 0 && x != 1 );
for ( unsigned int i = 3; prime && i * i <= x; i += 2 )
{
prime = x % i != 0;
}
return prime;
}
如果您不想考虑签名值,则只需将参数定义为unsigned int