繁体   English   中英

我如何打印所有的奇数?

[英]How do i print all the odd numbers?

我想使用无限循环打印从 14 到 156 的所有奇数,中断并继续。 但是,当我运行它不显示任何东西!

int main()
{
    int x;
    int y = 14;
    while(x) {
        if(y % 2 == 0) {
            continue;
        }
        else if(y % 3 == 0) {
            printf("%d\n", y);
        }
        if(y == 156) {
            break;
        }
        y++;
    }
    return 0;
}

您的代码的问题在于它使用了具有不可预测结果的操作。 具体来说,声明int x; 没有初始化它,然后将它用作while(x)中的终止条件是问题所在。 在许多平台和编译器上,这将保留x占用的内存中已经存在的任何值。 在这种情况下,您可能看不到打印语句,因为x以零值开始并且循环永远不会运行。

你应该让你的循环变成无限循环:

int main()
{
    int x;
    for(x = 14; ; x++) {
        if(x % 2 == 0) {
            continue;
        }
        if(x >= 156) {
            break;
        }
        printf("%d\n", x);
    }
    return 0;
}

[IDE一个链接]

这将打印不包括156本身的值。 要包括156 ,请将break条件放在printf调用之后:

printf("%d\n", x);
if(x >= 156) {
    break;
}

或者,您可以将>=更改为>

如果您有breakcontinue则不需要else

如果必须使用 while 循环,情况会稍微复杂一些,因为在continue避免无限循环之前必须增加:

int main() {
    int x = 14;
    while(1) {
        if(x % 2 == 0) {
            x++;
            continue;
        }
        if(x >= 156) {
            break;
        }
        printf("%d\n", x++);
    }
    return 0;
}

[IDEOne 链接]

如果您可以放弃continue则可以避免额外的增量:

int main() {
    int x = 14;
    while(1) {
        if(x % 2) {
            printf("%d\n", x);
        }
        if(x++ == 156) {
            break;
        }
    }
    return 0;
}

[IDEOne 链接]

我还删除了您对x % 3 == 0检查,因为不清楚在您的问题的限制范围内这样做的目的是什么。

无限循环:

#include <stdio.h>
#include <stdlib.h>

int main() {

int y=14;
while(1){
    if(y & 1){
        printf("%d\n",y);
    }

    if(y>=156){
        break;
    }

    y++;
}

return 0;
}

首选方法,使用 for 循环:

#include <stdio.h>
#include <stdlib.h>

int main() {

int start=14;
start |= 1; //This will increment by one if your start value is even
for (start; start<=146; start+=2) {
    printf("%d\n", start);  
}

return 0;
}

带有中断的无限循环不是执行此操作的正确方法。 使用“for”循环,您的代码将更加清晰。 从 15 开始,然后增加 2 直到 156。

你的逻辑有几个问题。

1. Variable `x` is of no use. You can use variable `y` to terminate the loop.
2. No need to check if the number is a multiple of 3.
3. No need to check for even numbers is either.

我已经修改了代码,它给出了正确的结果,但我会敦促你阅读以上几点并尝试正确地获得代码。

int main()
{
    int y = 14;
    while(y) {
        if (y==156)
            break;
        if(y % 2 != 0) {
            printf("%d ",y);
        }
        y++;
    }
    return 0;
}

更好更快的方法是从 15 开始并将变量y的值增加 2 直到它 <=156。

int main()
    {
        int y = 15;
        while(y) {
            if (y==157)
                break;
            printf("%d ",y);
            y+=2;
        }
        return 0;
    }

您的代码具有未定义的行为,因为变量x未初始化并用作while语句中的条件。 您可以使用while (1)解决此问题,但您的程序中确实会出现无限循环,因为均匀性测试发生在156终止测试之前。

使用标准的for循环就可以解决这个问题:

#include <stdio.h>

int main(void) {
    for (int y = 14; y < 156; y++) {
        if (y % 2 != 0) {
            printf("%d\n", y);
        }
    }
    return 0;
}

可以简化为这样,只枚举奇数:

#include <stdio.h>

int main(void) {
    for (int y = 15; y < 156; y += 2) {
        printf("%d\n", y);
    }
    return 0;
}

如果您需要使用breakcontinue和一个无限循环,您确实可以使用经典的永远 C 循环,一个没有终止条件的for循环:

#include <stdio.h>

int main(void) {
    for (int y = 14;; y++) {
        if (y == 156)
            break;
        if (y % 2 == 0)
            continue;
        printf("%d\n", y);
    }
    return 0;
}

嗯...

#include <stdio.h>
#include <stdint.h>
#include <errno.h>

/* Prints even or add numbers between to and from.
   Works for negative numbers.
   Works up and down.
 */
int print_even_or_odd(long long from, long long to, int even)
{
  if (((INT32_MAX < from) || (INT32_MIN > from)) ||
      ((INT32_MAX < to) || (INT32_MIN > to)))
  {
    fprintf(stderr, "Invalid input.\n");
    errno = EINVAL;
    return -1;
  }

  printf("from=%d to %d\n", (int) from, (int) to);

  int sign = (to < from) ?-1 :1;

  /* Adjust "from" to next even/odd number. */
  if ((!even && !(from & 1)) || (even && (from & 1)))
  {
     from += sign;
  }

  /* Adjust "to" to the previous even/odd number. */
  if ((!even && !(to & 1)) || (even && (to & 1)))
  {
    to -= sign;
  }

  {
    size_t steps = (size_t) (sign * ((to - from) / 2)) + 1;

    if (0 == steps)
    {
      printf("Nothing to do.\n");
      return 0;
    }

    while (1)
    {
      if (0 >= steps)
      {
        break;
      }

      --steps;

      printf("%d\n", (int) (to - sign * 2 * (int) steps));

      continue; /* as having continue is a requirement ... ;-) */
    }
  }

  return 0;
}

int main(void)
{
  print_even(14, 156, 0);
  print_even(156, 14, 0);
  print_even(-14, -156, 0);
  print_even(-156, -14, 0);
}

暂无
暂无

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

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