小编Ste*_*ady的帖子

while循环结束时n ++和++ n之间的区别?(ANSI C)

这可能是一个愚蠢的问题,但我无法弄清楚。它与n ++和++ n之间的差异有关(我以为我理解但显然不是)。

#include <stdio.h>
#include <math.h>

long algorithmA(int n);
long algorithmB(int n);

int main(){
    long A, B;
    A = B = 0;
    int n = 1;
    while(A >= B){
        A = algorithmA(n);
        B = algorithmB(n);
        n++;
    }
    printf("At n = %d, Algorithm A performs in %ld seconds & "
           "Algorithm B performs in %ld seconds.", n, A, B);

}

long algorithmA(int n){
    return pow(n,4) * 86400 * 4;
}

long algorithmB(int n){
    return pow(3,n);
}
Run Code Online (Sandbox Code Playgroud)

在这里您可能会告诉我,我正在尝试看算法A在什么时候优于算法B。功课中的功能和时间单位是给我的。

无论如何,我一直认为在while循环结束时“ ++”的顺序无关紧要。但是,如果我使用++ n而不是n …

c increment pre-increment post-increment

0
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×1

increment ×1

post-increment ×1

pre-increment ×1