gpt4 book ai didi

c - 生成前 50 个可被 7 整除的正整数

转载 作者:行者123 更新时间:2023-11-30 18:10:19 25 4
gpt4 key购买 nike

我一直用来理解 loops 的逻辑看起来不一致。

代码:

#include "stdio.h"

int main() {
int n,i;
printf("\n Integer divisible by 7 \n");
n = 7;
for(i = 1; i<=50; i++) {
printf("%8d", n);
n = n + 7;
}
return 0;
}

结果:

Integers divisible by 
7 14 21 35 42 49 56 63 70
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - 343 350

号码7怎么样?仍在打印 if 语句 n = n +7已经增加n的值减 7?

最佳答案

你的代码是这样工作的:

n = 7;
for(i = 1; i<=50; i++) // is i less than 50? If yes go inside for
{
printf("%8d", n); // print n (the first time it's still 7)
n = n + 7; // increase n by 7
}

正如您所看到的,您在第一次增加 n 之前打印了 n 的值,这意味着由于 n 最初为 7,因此您打印了该值。

如果您想从打印中排除 7,您应该将打印移至 n = n + 7 下方,或使 n 等于 14 而不是 7,但仍会增加在 for 循环中它减 7。

关于c - 生成前 50 个可被 7 整除的正整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com