簡體   English   中英

為什么我的程序沒有將偶數除以二?

[英]Why is my program not dividing even numbers by two?

它應該掃描10個int數,然后向后顯示它們,將偶數除以2,但它只顯示它們而不除。

es:
10 9 8 7 6 5 4 3 2 1 ==> 1 2 3 2 5 3 7 4 9 5
但是我的卻是:10 9 8 7 6 5 4 3 2 1 ==> 1 2 3 4 5 6 7 8 9 10

#include <stdio.h>

int main(void)
{
    int a[10];

    for(int i = 0; i < 10; i++)
        scanf("%d", &a[i]);

    for (int i = 0; i < 10; i++) {
        if (a[i] % 2 == 0 ) {
            a[i] = a[i] / 2; i++;
        }
        else
            i++;
    }

    for(int i = 9; i > -1; i--)
        printf("%d\n", a[i]);

    return 0;
}

中間循環在每次迭代中錯誤地將i遞增兩次:

for (int i = 0; i < 10; i++) { // <<== One increment
    if (a[i]%2 == 0 ) {
        a[i] = a[i]/2; i++;    // <<== Another increment - first branch
    }
    else
        i++;                   // <<== Another increment - second branch
}

在您的情況下,所有偶數都恰好存儲在循環跳過的偶數位置。

注意:更好的解決方案是完全放棄中間循環,並在打印時進行分割。

我的第二個for循環的主體前進i 由於在循環的子句中也進行了高級處理,因此它進行了兩次高級處理,從而有效地跳過了其他任何元素。 刪除這些進步,您應該可以:

for(int i=0; i<10; i++) {
    if (a[i] % 2 == 0)  {
        a[i] /= 2;
    }
}

在您的程序中,您在循環內將for循環變量i遞增兩次,循環也將值遞增一次,因此跳過這些值,這就是輸出錯誤的原因。我已將更正后的程序及其輸出附加在一起。您了解這個概念。謝謝

#include <stdio.h>
int main(void)
 {
   int a[10];
   printf("\n Given Values are");
   printf("\n-----------------");

   for(int i = 0; i < 10; i++)  
      scanf("%d", &a[i]);

   for (int i = 0; i < 10; i++) 
     {
        if (a[i] % 2 == 0 ) 
          {
           a[i] = a[i] / 2;

           }
      }
  printf("\n After dividing the even numbers by 2 and print in reverse  order");
  printf("\n ----------------------------------------------------------------\n");
  for(int i = 9; i > 0; i--)
    printf("%d\n", a[i]);
return 0;
}

輸出量

Given Values are                                                                                                                                                                            
-----------------                                                                                                                                                                            
1                                                                                                                                                                                            
2                                                                                                                                                                                            
3                                                                                                                                                                                            
4                                                                                                                                                                                            
5                                                                                                                                                                                            
6                                                                                                                                                                                            
7    
8                                                                                                                                                                                            
9                                                                                                                                                                                            
10                                                                                                                                                                                           

After dividing the even numbers by 2 and print in reverse order                                                                                                                             
----------------------------------------------------------------
5                                                                                                                           
9                                                                                                                                                                                            
4                                                                                                                                                                                            
7      
3                                                                                                                                                                                            
5                                                                                                                                                                                            
2                                                                                                                                                                                            
3                                                                                                                                                                                            
1                                                                                                                                                                                            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM