虽然我的代码没有错误,但我很难理解逻辑中的奇怪输出和错误。 我的想法是将素数与输入分开并将它们存储在数组和数组中。然后按降序排列它们。我无法确定输入中是否没有整数,或者输入是否小于特定的输入数量。这是我输入4个输入。
#include<stdio.h>
int main()
{
int i,j,p[3],n[3], count = 0;
printf("Enter 4 integers:");
scanf("%d,%d,%d,%d",&n[0],&n[1],&n[2],&n[3]);
if(scanf("%d,%d,%d,%d",&n[0],&n[1],&n[2],&n[3]) != 4);
printf("enter only 4 inputs\n");
if(scanf("%d,%d,%d,%d",&n[1],&n[2],&n[3],&n[4]) < 4);
printf("enter only integers\n");
for(j=0;j<4;j++)
{
for (i = 2; i <= n[j] / 2; i++)
{
if (n[j] % i == 0)
{
count++;
break;
}
if (count == 0)
p[j] = n[j];
else
p[j]=0;
}
}
for(j=0;j<4;j++) // to display prime array
{
printf("%d\n",p[j]);
}
if(p[0] == p[1] == p[2] == p[3] ==0 )
printf("No prime number is present\n");
for(i=1;i<4;++i) // Loop to store largest number to p[0]
{
if(p[0]<p[i])
p[0]=p[i];
}
printf("Largest element = %d",p[0]);
return 0;
}
答案 0 :(得分:0)
我希望这会有所帮助。我尽可能多地记录下来。
#include<stdio.h>
#include<iostream>
using namespace std;
int main() {
//int i,j,p[3],n[3], count = 0;
// When possible try to place declarations
//close to where they are going to be used.
int n[4] = {0,0,0,0}; //Declare the array, we won't need the other no more.
printf("Enter 4 integers:");
//I would put this in a loop.
std::cin >> n[0]; cout << " next \n";
std::cin >> n[1]; cout << " next\n";
std::cin >> n[2]; cout << " next\n";
std::cin >> n[3]; cout << " next\n";
//also this computer was acting weird
//with scanf so I had to switch to cin.
//weird way to check for four numbers
//if(scanf("%d,%d,%d,%d",&n[0],&n[1],&n[2],&n[3]) != 4);
// printf("enter only 4 inputs\n");
//if(scanf("%d,%d,%d,%d",&n[1],&n[2],&n[3],&n[4]) < 4);
// printf("enter only integers\n");
//Lets declare a place to store the largest prime.
int largest_prime = -1; //-1 to detect errors.
//initiate j here.
for(int j=0;j<4;j++)//Iterate through all the numbers.
{
if(n[j] % 2 == 1){
if(n[j] > largest_prime)
{
largest_prime = n[j];
}
}
// for (i = 2; i <= n[j] / 2; i++)
// {
// if (n[j] % i == 0)
// {
// count++;
// break;
// }
// if (count == 0)
// p[j] = n[j];
// else
// p[j]=0;
}
//for(j=0;j<4;j++) // to display prime array
//{
//printf("%d\n",p[j]);
//}
//
//if(p[0] == p[1] == p[2] == p[3] ==0 )
// printf("No prime number is present\n");
//
//for(i=1;i<4;++i) // Loop to store largest number to p[0]
// {
// if(p[0]<p[i])
// p[0]=p[i];
// }
// printf("Largest element = %d",p[0]);
std::cout << "\nThe highest prime number: " << largest_prime << " Thanks!";
return 0;
}
答案 1 :(得分:0)
使用此代码
#include <stdio.h>
int main(void)
{
int i,j, n[4] = {0,};
int max_prime_val = -1;
int check_prime;
//int n, i, j, check;
printf("Enter four integers:\n");
for(j=0; j<4; j++)
{
scanf("%d", &n[j]);
}
for (j = 0; j < 4; j++)
{
check_prime = 0;
//Use sqrt(n[j]) in place of n[j]/2 for increase time efficiency
//temp = (int)sqrt(n[j]);
//for (i = 2; i < temp ; i++)
for (i = 2; i < n[j]/2 ; i++)
{
if (n[j] % i == 0)
{
check_prime = 1;
break;
}
}
if ( (check_prime == 0) && (n[j] > max_prime_val) )
{
max_prime_val = n[j];
}
}
printf("The biggest prime number is [%d]",max_prime_val);
return 0;
}