在指针数组中查找主要数字
Finding the primary numbers in an array of pointer
这段代码运行良好,它告诉你输入一个数字,然后它把
for 循环中的数字,它检查它是否可以被 i 整除,如果为真,则打印非素数,否则打印素数。
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
bool f = true;
for (int i = 2; i < x; i++) {
f = false;
if (i % x == 0)
f = true;
if (f)
cout << "not primary";
else
cout << "primary";
}
}
但是当我将它转换为数组时:
#include <iostream>
using namespace std;
int main() {
cout << "the number of array:" << endl;
int n;
cin >> n;
cout << "enter them = \n";
int *p = new int[n];
for (int i = 0; i < n; i++)
cin >> p[i];
bool f = true;
for (int i = 0; i < n; i++)
for (int j = 2; j < p[i]; j++) {
f = false;
if (p[i] % j == 0)
f = true;
if (f)
cout << "This is not a primary number!\n";
else
cout << "this is a primary number!\n";
}
delete p;
}
它只存储第一个数字,我明白了,但如何增加它
假设 n =3
所以 p[3] = {4,6,7};
我的问题是如何在 j 条件下告诉编译器
if (p[0] % j) then(p[1] %j) 似乎只需要 p[0]
这会更好
#include <iostream>
using namespace std;
int main() {
cout << "the number of array:" << endl;
int n;
cin >> n;
cout << "enter them = \n";
int *p = new int[n];
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < n; i++) {
bool f = false; // we set f to false for each number
for (int j = 2; j < p[i]; j++) {
if (p[i] % j == 0) {
f = true;
break; // we break the loop if it's a prime number
}
}
if (f)
cout << p[i] << " is not a primary number!\n";
else
cout << p[i] << " is a primary number!\n";
}
delete[] p; // Here you forget brackets [], when you use new[] you must use delete[].
}
Doc 删除运算符。
我让一些问题像 int
。您不应该使用带符号的数字进行迭代或储存尺寸。因为你是初学者,我不想混淆你。所以我让它。
这段代码运行良好,它告诉你输入一个数字,然后它把 for 循环中的数字,它检查它是否可以被 i 整除,如果为真,则打印非素数,否则打印素数。
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
bool f = true;
for (int i = 2; i < x; i++) {
f = false;
if (i % x == 0)
f = true;
if (f)
cout << "not primary";
else
cout << "primary";
}
}
但是当我将它转换为数组时:
#include <iostream>
using namespace std;
int main() {
cout << "the number of array:" << endl;
int n;
cin >> n;
cout << "enter them = \n";
int *p = new int[n];
for (int i = 0; i < n; i++)
cin >> p[i];
bool f = true;
for (int i = 0; i < n; i++)
for (int j = 2; j < p[i]; j++) {
f = false;
if (p[i] % j == 0)
f = true;
if (f)
cout << "This is not a primary number!\n";
else
cout << "this is a primary number!\n";
}
delete p;
}
它只存储第一个数字,我明白了,但如何增加它 假设 n =3 所以 p[3] = {4,6,7}; 我的问题是如何在 j 条件下告诉编译器 if (p[0] % j) then(p[1] %j) 似乎只需要 p[0]
这会更好
#include <iostream>
using namespace std;
int main() {
cout << "the number of array:" << endl;
int n;
cin >> n;
cout << "enter them = \n";
int *p = new int[n];
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < n; i++) {
bool f = false; // we set f to false for each number
for (int j = 2; j < p[i]; j++) {
if (p[i] % j == 0) {
f = true;
break; // we break the loop if it's a prime number
}
}
if (f)
cout << p[i] << " is not a primary number!\n";
else
cout << p[i] << " is a primary number!\n";
}
delete[] p; // Here you forget brackets [], when you use new[] you must use delete[].
}
Doc 删除运算符。
我让一些问题像 int
。您不应该使用带符号的数字进行迭代或储存尺寸。因为你是初学者,我不想混淆你。所以我让它。