题目描述:
将1~100的自然数存到一个有100个元素的整型数组中(数组的下标为0~99),请在屏幕上输出如下数据:1)在屏幕上输出存储的100个自然数,每行输出10个,各数值之间用空格隔开。2)将序号是素数的数据输出到屏幕上,格式要求:每行输出10个,各数值之间用一个空格隔开。(提醒:结尾无空格)
#include <iostream>
using namespace std;
//判断素数
bool prime(int n)
{
for(int i = 2; i < n; i++)
{
if(n % i == 0)
{
return false;
break;
}
}
return true;
}
int main()
{
//赋值
int a[100];
for(int i = 0; i < 100; i++)
{
a[i] = i + 1;
}
//输出1
for(int x = 0; x < 10; x++)
{ int y=0;
for(y; y < 9; y++)
{
cout << a[x * 10 + y] << " ";
}
cout << a[x * 10 + y] << endl;
}
//输出2
int n = 0;
for(int j = 0; j < 97; j++)
{
if(prime(j) && j != 0 && j != 1)
{
n += 1;
if(n == 10)
{
cout << a[j] << endl;
n = 0;
}
else
cout << a[j] << " ";
}
}
cout << a[97];
return 0;
}
知识点:
素数是指在大于1的整数中,除了 1 和它本身以外,不能被任何整数整除的数