gpt4 book ai didi

c - 基于结束条件的 C 格式输出

转载 作者:行者123 更新时间:2023-12-02 18:51:52 24 4
gpt4 key购买 nike

我正在迭代C中的一些计数器,以将结果打印在单行上,如012、013、014等......

我的代码根据某些条件而结束,并且正在由机器人进行更正。

如何在代码的最后一个值之后停止打印 , 。想法是获得一个仅包含唯一数字且按升序排列的 3 位数字以 789 结尾;

我的功能如下:

void my_print_comb(void){
for(int i = 47; i++ < 57 ; ){
for(int j = 48; j++ < 57 ; ){
for(int k = 49; k++ < 57 ; ){
if( i != j && i != k && j != k){
while( i < j && j < k){
char a = i, b = j, c = k;
my_putchar(a);
my_putchar(b);
my_putchar(c);
my_putchar(',');
my_putchar(' ');
break;
}
}
}
}
}
}

最佳答案

打印不带逗号的元素之一。打印第一个,然后迭代数组的其余部分,并使用 , 打印每个元素。在它前面;或使用 , 打印除最后一个元素之外的所有元素每一项之后,然后打印最后一项。我更喜欢第一种方法。

由于您没有显示任何代码,所以我只是给出一个示例。

void pretty_print_ints(const int *array, size_t count)
{
// Nothing to do.
if (count == 0 || array == NULL) return;

// Print the first element, without a `,` after it.
printf("%d", array[0]);

// If there are more elements in the list, print them all, but add a `, ` separator.
for (size_t i = 1; i < count; i++) printf(", %d", array[i]);

// Add a new line at the end.
printf("\n");
}

如果我们不知道必须打印多少个元素,它也可以工作。例如,当我们打印 count 时,而不是停止当我们遇到等于 0 的元素时,我们可以停止元素,我们唯一需要改变的是停止条件。如果我们采取相反的方式(使用 count - 1 打印 printf("%d, ", array[i]); 元素,我们将无法处理这种情况)。

关于c - 基于结束条件的 C 格式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66657489/

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