如何打印数组中的回文数和质数
How to print the palindrome and prime numbers in an array
我正在尝试打印数组中的素数和回文数。它没有打印任何我错在哪里?!
#include <iostream>
#include <string>
using namespace std;
int main() {
int x, n, i, num, digit, rev=0;
int bar[100];
cout << "Cate numere trebuie sa fie in Array?:";
cin >> x;
for (i = 0; i < x; i++) {
cout << "bar[" << i << "]=";
cin >> bar[i];
}
for (i = 0; i < x; i++) {
num=bar[i];
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
}
if (num == rev)
cout << num;
int y;
bool prim = true;
for (y = 2; y <= bar[i] / 2; ++y)
{
if (bar[i] % y == 0)
{
prim = false;
}
}
if (prim)
cout << bar[i];
return 0;
}
你必须在循环内进行输出。但是例如这个输出是在循环之外进行的
for (i = 0; i < x; i++) {
num=bar[i];
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
}
if (num == rev) // <===
cout << num;
而且上面if语句中的比较是不正确的。 do-while 循环后 num 的值等于 0。你需要写
if (bar[i] == rev) // <===
cout << num;
此外,素数的输出也是在循环外进行的。此外,您错误地确定了质数。例如 1
根据您的算法是质数。
同样在第一个循环中,您必须将变量 rev 重新初始化为 0。
程序可以这样写
#include <iostream>
int main()
{
const size_t N = 100;
int bar[N];
cout << "Cate numere trebuie sa fie in Array?: ";
size_t n = 0;
std::cin >> n;
if ( N < n ) n = N;
for ( size_t i = 0; i < n; i++ )
{
std::cout << "bar[" << i << "] = ";
std::cin >> bar[i];
}
std::cout << '\n';
for ( size_t i = 0; i < n; i++ )
{
const int Base = 10;
int num = bar[i];
int rev = 0;
do
{
int digit = num % Base;
rev = rev * Base + digit;
} while ( num /= Base );
if ( bar[i] == rev ) std::cout << bar[i] << ' ';
}
std::cout << '\n';
for ( size_t i = 0; i < n; i++ )
{
bool prime = bar[i] % 2 == 0 ? bar[i] == 2 : bar[i] != 1;
for ( int j = 3; prime && j <= bar[i] / j; j += 2 )
{
prime = bar[i] % j != 0;
}
if ( prime ) std::cout << bar[i] << ' ';
}
std::cout << '\n';
return 0;
}
它的输出可能是
Cate numere trebuie sa fie in Array?: 10
bar[0] = 121
bar[1] = 11
bar[2] = 131
bar[3] = 13
bar[4] = 143
bar[5] = 14
bar[6] = 1551
bar[7] = 10
bar[8] = 1234321
bar[9] = 99
121 11 131 1551 1234321 99
11 131 13
我正在尝试打印数组中的素数和回文数。它没有打印任何我错在哪里?!
#include <iostream>
#include <string>
using namespace std;
int main() {
int x, n, i, num, digit, rev=0;
int bar[100];
cout << "Cate numere trebuie sa fie in Array?:";
cin >> x;
for (i = 0; i < x; i++) {
cout << "bar[" << i << "]=";
cin >> bar[i];
}
for (i = 0; i < x; i++) {
num=bar[i];
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
}
if (num == rev)
cout << num;
int y;
bool prim = true;
for (y = 2; y <= bar[i] / 2; ++y)
{
if (bar[i] % y == 0)
{
prim = false;
}
}
if (prim)
cout << bar[i];
return 0;
}
你必须在循环内进行输出。但是例如这个输出是在循环之外进行的
for (i = 0; i < x; i++) {
num=bar[i];
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
}
if (num == rev) // <===
cout << num;
而且上面if语句中的比较是不正确的。 do-while 循环后 num 的值等于 0。你需要写
if (bar[i] == rev) // <===
cout << num;
此外,素数的输出也是在循环外进行的。此外,您错误地确定了质数。例如 1
根据您的算法是质数。
同样在第一个循环中,您必须将变量 rev 重新初始化为 0。
程序可以这样写
#include <iostream>
int main()
{
const size_t N = 100;
int bar[N];
cout << "Cate numere trebuie sa fie in Array?: ";
size_t n = 0;
std::cin >> n;
if ( N < n ) n = N;
for ( size_t i = 0; i < n; i++ )
{
std::cout << "bar[" << i << "] = ";
std::cin >> bar[i];
}
std::cout << '\n';
for ( size_t i = 0; i < n; i++ )
{
const int Base = 10;
int num = bar[i];
int rev = 0;
do
{
int digit = num % Base;
rev = rev * Base + digit;
} while ( num /= Base );
if ( bar[i] == rev ) std::cout << bar[i] << ' ';
}
std::cout << '\n';
for ( size_t i = 0; i < n; i++ )
{
bool prime = bar[i] % 2 == 0 ? bar[i] == 2 : bar[i] != 1;
for ( int j = 3; prime && j <= bar[i] / j; j += 2 )
{
prime = bar[i] % j != 0;
}
if ( prime ) std::cout << bar[i] << ' ';
}
std::cout << '\n';
return 0;
}
它的输出可能是
Cate numere trebuie sa fie in Array?: 10
bar[0] = 121
bar[1] = 11
bar[2] = 131
bar[3] = 13
bar[4] = 143
bar[5] = 14
bar[6] = 1551
bar[7] = 10
bar[8] = 1234321
bar[9] = 99
121 11 131 1551 1234321 99
11 131 13