gpt4 book ai didi

c - 同时打印向上和向下的数字

转载 作者:行者123 更新时间:2023-11-30 20:50:54 25 4
gpt4 key购买 nike

我需要同时打印上字和下字数字,但使用单变量和单循环。

例如。

If I give input 5 then output should be
5 & 1
4 & 2
3 & 3
2 & 4
1 & 5

但主要条件是单循环、单变量

谢谢。

最佳答案

您可以使用结构使单个变量具有多个数据。

#include <stdio.h>

int main(void) {
struct data {
int n, i;
} d;
if (scanf("%d", &d.n) != 1) return 1;
for (d.i = 1; d.i <= d.n; d.i++) {
printf("%d & %d\n", d.n - d.i + 1, d.i);
}
return 0;
}

关于c - 同时打印向上和向下的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36918941/

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