#include<iostream>
using namespace std;
void main(){
int n=0,a[100],b[100];
for (int i = 0; i < 100; i++)
{a[i]=i+1;
}
for (int i = 1; i < 100; i++)
{ for (int j = 2; j < i; j++)
{
if (i%j==0)
{
break;
}
if(j==i-1)
{b[n]=i;
n++;}
}
}
for (int i = 0; i < n; i++)
{cout<<"\t"<<b[i];}
system("pause");}
为什么我列出来少了2?
3 个解决方案
#2
你应该把 这个if判断往外放一层{b[n]=i; n++;},效率也更高
判断改成if (j == i)这样才对
判断改成if (j == i)这样才对
#include<iostream>
using namespace std;
void main(){
int n = 0, a[100], b[100];
for (int i = 0; i < 100; i++)
{
a[i] = i + 1;
}
int i, j;
for (i = 1; i < 100; i++)
{
for (j = 2; j < i; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
b[n] = i;
n++;
}
}
for (int i = 0; i < n; i++)
{
cout << "\t" << b[i];
}
system("pause");
}
#3
2,应该一开始就加进去,因为这里走不到2为素数的情况
for (int j = 2; j < i; j++)
#1
#2
你应该把 这个if判断往外放一层{b[n]=i; n++;},效率也更高
判断改成if (j == i)这样才对
判断改成if (j == i)这样才对
#include<iostream>
using namespace std;
void main(){
int n = 0, a[100], b[100];
for (int i = 0; i < 100; i++)
{
a[i] = i + 1;
}
int i, j;
for (i = 1; i < 100; i++)
{
for (j = 2; j < i; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
b[n] = i;
n++;
}
}
for (int i = 0; i < n; i++)
{
cout << "\t" << b[i];
}
system("pause");
}
#3
2,应该一开始就加进去,因为这里走不到2为素数的情况
for (int j = 2; j < i; j++)