简体   繁体   English

为什么我的程序返回相同的字符?

[英]Why does my program return the same character?

This is a program that checks whether the input number is a product of two prime numbers ('Y') or not ('N'). 这是一个检查输入数字是否是两个质数('Y')或('N')的乘积的程序。

 #include <stdio.h>


// the function checks if the number is prime
int is_prime(int z) {
    int i;
    for(i=2; i<z; i++){
        if(z%i == 0){
            return 0;
    }
    return 1;
  }
}

/* the function checks if the given number is a
product of two prime numbers bigger than 2*/
int is_prime_product(int x) {
    int j;

    for(j=2; j<x; j++){
        if(x%j == 0){
            if(is_prime(x/j) && is_prime(j)){
                return 1;
                break;
            }else return 0;
        }
    }
 }

    int main() {
        int n=0;
        int c;

        do{
        c=getchar();
        if((c>='0') && (c<='9')){
            n= n*10+(c-'0');
        }
    } while (('0'<=c) && (c<='9'));


    if(is_prime_product(c)){
        putchar('Y');
    }else{
        putchar('N');
    }

        return 0;
}

I don't know why this program always returns 'Y' even when it should return 'N'. 我不知道为什么该程序即使应返回“ N”也总是返回“ Y”。 I just don't see where the mistake is. 我只是不知道错误在哪里。

Partial answer: Better version of is_prime: 部分答案:更好的is_prime版本:

int is_prime(int z) 
{
    if(z <= 1) return 0;
    if(z == 2) return 1;
    int i;
    for(i=3; i<sqrt(z); i+=2)
    {
        if(z%i == 0) return 0;
    }
    return 1;
 }

After the test for 2 it is enough to test for odd factors up to the square root of your test number. 测试2后,足以测试奇数因子,直到您的测试数的平方根。 (Also fixed the curly braces) (还固定了花括号)

Cause of your error: 错误原因:

if(is_prime_product(n)) ...

tests the input number n not the last character c 测试输入数字n而不是最后一个字符c


Edit 编辑

Some hints for better (more readable, more reliable, and so on) code: 有关更好(更易读,更可靠等)代码的一些提示:

  • Use types matching the problem (bool instead of int) 使用匹配问题的类型(布尔型而不是整数型)
  • Use good variable names (i only for loops, z only for floats) 使用好的变量名(i仅用于循环,z仅用于浮点)
  • use meaningful variable names (number instead of n) 使用有意义的变量名(用数字代替n)
  • consistent braces, spacing around operators 大括号一致,运算符周围有间距

These things make a difference! 这些事情有所作为!

Have a look: 看一看:

 bool is_prime(unsigned int number) { if(number <= 1) return false; if(number == 2) return true; for(unsigned int factor = 3; factor < sqrt(number); factor += 2) { if(number % factor == 0) return false; } return true; } 

Please cheak your is_prime() function. 请检查您的is_prime()函数。 The code return 1; 代码return 1; should be after the for loop.You can try the following: 应该在for循环之后for您可以尝试以下操作:

  int is_prime (int z)
    {
      int i;
      for (i = 2; i < z; i++)
      {
        if (z % i == 0)
        {
          return 0;
        }
      }
      return 1;
    }

and your function is_prime_product () should be written as follow: 并且您的函数is_prime_product ()应编写如下:

  int is_prime_product (int x)
{
  int j;
  for (j = 2; j < x; j++)
    {
      if (x%j==0&&is_prime (x / j) && is_prime (j))
        {
          return 1;
        }
    }
  return 0;
}

also you should use if (is_prime_product (n)) instead of if (is_prime_product (c)) . 您也应该使用if (is_prime_product (n))而不是if (is_prime_product (c))

I have modified some of your code from main function. 我已经从主要功能修改了一些代码。 Please try with below code once, 请尝试使用以下代码一次,

Instead of this main function code: 代替此主要功能代码:

int main() {
    int n=0;
    int c;

    do{
    c=getchar();
    if((c>='0') && (c<='9')){
        n= n*10+(c-'0');
    }
} while (('0'<=c) && (c<='9'));

if(is_prime_product(c)){
    putchar('Y');
}else{
    putchar('N');
}
    return 0;

} }

Use this code: 使用此代码:

int main() {
    int n=0;
    int c;

    scanf("%d",&c);
    do{
    if((c>='0') && (c<='9')){
        n= n*10+(c-'0');
    }
} while (('0'<=c) && (c<='9'));

if(is_prime_product(c)){
    putchar('Y');
}else{
    putchar('N');
}
    return 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM