簡體   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