使用for循环分布值:

huangapple go评论57阅读模式
英文:

distribution of values ​with for loop

问题

这部分执行电子分布。如果vNum的值为32,将打印以下结果:[O]2 6 10 12 但我收到的是:

[O]2 6 10 12
[P]
[Q]

对于44,预期结果应为:

[O]2 6 10 14
[P]2 6 4

但我收到的是:

[O]2 6 10 14
[P]2 6 4
[Q]

为什么P和Q级别会打印出没有分配值的情况?
目标是只有在有值时才出现,就像0的情况一样。
我知道这与外部for循环有关(int i = 3; i > 0; i--),它将为每个级别运行。从i等于3(表示O能级)开始,然后降到i表示Q能级。但我不明白如何使3不立刻出现。

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

int main() {
    while (1) {
        int vNum;

        printf("vNum: ");
        scanf("%d", &vNum);

        if (vNum > 0) {
            for (int i = 3; i > 0; i--) {
                printf("[%c]", 82 - i);
                int limit = 2;
                int E = (limit <= vNum ? limit : vNum);

                for (int j = 0; j < i + 1 && E > 0; j++) {
                    printf("%d ", E);
                    usleep(50000);
                    vNum -= E;
                    E = (limit += 4) <= vNum ? limit : vNum;
                }
                printf("\n");
            }
        }

        printf("\n");
    }

    return 0;
}
英文:

This section does the electronic distribution. the results to be printed if the value of vNum is 32, will be [O]2 6 10 12
But I receive:

[O]2 6 10 12  
[P]  
[Q]  

expected for 44 would be

[O]2 6 10 14  
[P]2 6 4  

But I receive:

[O]2 6 10 14  
[P]2 6 4  
[Q]  

Why are the P and Q levels being printed with no allocated values?
The aim was to appears only when there are values ​​in it, as in the case of 0 that nothing appears.
I know it's something in the outer for loop (int i = 3; i > 0; i--) that will run for each level. Starting with i equal to 3 (representing the O energy level) and going down to i representing the Q energy level. But I don't understand how to make the 3 not appear at once.

#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;

int main() {
    while (1) {
        int vNum;

        printf(&quot;vNum: &quot;);
        scanf(&quot;%d&quot;, &amp; vNum);

        if (vNum &gt; 0) {
            for (int i = 3; i &gt; 0; i--) {
                printf(&quot;[%c]&quot;, 82 - i);
                int limit = 2;
                int E = (limit &lt;= vNum ? limit : vNum);

                for (int j = 0; j &lt; i + 1 &amp;&amp; E &gt; 0; j++) {
                    printf(&quot;%d &quot;, E);
                    usleep(50000);
                    vNum -= E;
                    E = (limit += 4) &lt;= vNum ? limit : vNum;
                }
                printf(&quot;\n&quot;);
            }
        }

        printf(&quot;\n&quot;);
    }

    return 0;
}

答案1

得分: 2

这个问题中的代码在外部循环中打印类似 [Q] 的标签,当内部循环还不清楚是否要打印任何内容时。

只有在有值需要打印时才打印标签有不同的方法。

一种方法是将 printf 移动到内部循环中,并使用条件仅在第一个值之前打印它。在下面的示例中,c 包含要打印的字符,并且设置为0以跳过后续内部循环周期中的 printf

我还修复了一些问题。

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

int main(void) {
    while (1) {
        int vNum;

        printf("vNum: ");
        int rc = scanf("%d", &vNum);
        if (rc != 1) break;

        if (vNum > 0) {
            for (int i = 3; i > 0; i--) {
                int c = 'R' - i;
                int limit = 2;
                int E = (limit <= vNum ? limit : vNum);

                for (int j = 0; j < i + 1 && E > 0; j++) {
                    if (c != 0) {
                        printf("[%c]", 82 - i);
                        c = 0;
                    }
                    printf("%d ", E);
                    usleep(50000);
                    vNum -= E;
                    E = (limit += 4) <= vNum ? limit : vNum;
                }
                printf("\n");
            }
        }
        else break;

        printf("\n");
    }

    return 0;
}

希望这可以帮助你理解代码中的更改。

英文:

The code in the question prints the labels like [Q] in the outer loop when it is not yet clear if the inner loop will print anything.

There are different ways to print the label only if there are values to be printed.

One way is to move the printf into the inner loop and use a condition to print it only before the first value. In the example below, c contains the character to be printed, and it is set to 0 to skip the printf in subseqent inner loop cycles.

I also fixed a few problems.

#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;

int main(void) {
    while (1) {
        int vNum;

        printf(&quot;vNum: &quot;);
        int rc = scanf(&quot;%d&quot;, &amp; vNum);
        if(rc != 1) break;

        if (vNum &gt; 0) {
            for (int i = 3; i &gt; 0; i--) {
                int c = &#39;R&#39; - i;
                int limit = 2;
                int E = (limit &lt;= vNum ? limit : vNum);

                for (int j = 0; j &lt; i + 1 &amp;&amp; E &gt; 0; j++) {
                    if(c != 0) {
                        printf(&quot;[%c]&quot;, 82 - i);
                        c = 0;
                    }
                    printf(&quot;%d &quot;, E);
                    usleep(50000);
                    vNum -= E;
                    E = (limit += 4) &lt;= vNum ? limit : vNum;
                }
                printf(&quot;\n&quot;);
            }
        }
        else break;

        printf(&quot;\n&quot;);
    }

    return 0;
}

huangapple
  • 本文由 发表于 2023年7月3日 23:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定