您好,如何防止此函数使用质数数组打印 1
Hello how to prevent this func from print 1 with the prime numbers array
你好,我写这个函数来打印 2D 数组中的质数 当我输入例如 1 2 3 4 5 6 7 8 它打印 1 2 3 5 7 我怎样才能把它编辑成不
将外部if
语句更改为
if (count == 0 && o[i][j] != 1) {
c[I] = o[i][j];
I++;
p++;
}
编写一个函数来检查数字是否为素数并为每个矩阵元素调用该函数会更容易。还有更有效的方法来确定一个数字是否是素数。
我添加了一些注释来解释发生了什么,但事先,您将忽略结果列表中的"1",所以我添加了"if(o[i][j]!=1)",另一方面,为了找到素数列表,如果第一个数字是可除的,则无需计算其他数字,因此您可以在for(...
void FindPrime(int o[5][5]){
int c[100];int I=0 ,count=0,p =0;
int i,j,h;
for(i =0;i<5;i++)
{
for(j =0;j<5;j++)
{
count =0;
for(int k =2;k<o[i][j];k++)
{
if(o[i][j]% k == 0)
{
if(o[i][j]!=1)// Do not add 1 to the array
{
count++;
// To make the function faster you can add "break;" here. If find the first dividable number, don't continue for
}
}
}
if(count == 0)
{
c[I] = o[i][j];
I++;
p++;
}
}
}
for(int I =0;I<p;I++)
{
h=c[I];
cout<<" "<< h <<" ";
}
cout<<"nn";
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 添加两个二进制数(整数数组)
- 分段故障 - 数边数
- 使用 for 循环更改二进制数>十进制数
- 斐波那契数 - 动态数组
- 添加二进制数(作为数组)C++
- 任意但编译时已知的类型数的元组
- 计算加泰罗尼亚数模数
- 具有相同指数的数的和为相同数的数
- 数一数用给定的零钱兑换金额的方法
- 在vector中存储不同维数的数组
- 初始化未知维数的数组
- 找出GSL矩阵中的行数/列数