在指针数组中找到主要数字
Finding the primary numbers in an array of pointer
这件代码正常,它告诉您输入一个数字,然后将其放置for循环中的数字,并检查它是否可以由i划分,如果TUE,如果不打印质量,则不会打印出来。
#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条件下告诉编译器如果(p [p [0]%j),则(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[].
}
删除操作员的文档。
我让诸如int
之类的问题。您不应将签名的号码用于迭代或库存尺寸。因为您是初学者,所以我不想让您感到困惑。所以我让它。
相关文章:
- cout 指针时的实际数字
- 使用指针选择长无符号变量中的数字
- 如果堆栈在数字较低的地址增长,为什么指针比较会逆转这种情况?
- 带有反向数字的指针
- 使用指针重新排列数组中的数字
- 从数组指针中获取怪异的数字
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 如何仅将数字值存储在给定输入文件中的2D数组中?(没有指针)
- 为什么要使用指针来交换数字(初学者)
- 为什么将指针铸成数字类型是不安全的
- 在指针数组中找到主要数字
- 将指针投射到一个数字
- 使用指针为文件C++中的数字动态分配存储
- C++ 将数字转换为类型的指针
- 十六进制数字系统中的指针值
- 取消引用类型punned指针将打破严格的混叠规则:将字节数组转换为数字
- 使用指针和线性搜索在数组中找到较大和最小的数字
- 从函数返回指针给出奇怪的数字
- 用数字填充数组,然后返回指向该数组的指针
- 指针+数字