我如何在没有循环的情况下计算“点击数”?

问题描述 投票:0回答:1
#include <stdio.h>

int main()
{
    int ix;
    unsigned hits = 0;
    for (ix=0; ix < 128; ix++)
    {   
        if (ix % 4 == 0)
            continue;   

        hits++;
    }
    printf("%u hits\n", hits);
    return;
}

这不是编程问题,我没有这样的代码。但我对处理此类问题的数学方法感兴趣。printf返回“ 96次命中”我的问题是,有没有计算循环的“点击数”的公式?

c loops mod
1个回答
0
投票

此件:

if (ix % 4 == 0)
    continue;   

基本上是指“每四次跳过一次”。这意味着将迭代次数减少25%相同。因此,在这种情况下,由于操作hits++完全不依赖于ix的值,因此整个操作与:

unsigned hits = 0;
for (ix=0; ix < 128 * 3/4; ix++)
{   
    hits++;
}

并且由于唯一的操作是增量,所以您可以将所有内容更改为just

hits = 128*3/4;
© www.soinside.com 2019 - 2024. All rights reserved.