gpt4 book ai didi

C Primer Plus 第 6 版第 6 章编程练习 4

转载 作者:行者123 更新时间:2023-11-30 21:44:34 26 4
gpt4 key购买 nike

使用以下代码,我能够更改每一行的第一个字母并打印所有字母,但不符合要求。任何帮助将不胜感激。预先感谢您的帮助。

//This is a program to print a pattern of the letters
//A to U. Printing a number of letters
//with the row number. First row/one letter, second row/
//two letters, etc.
//A
//BC
//DEF
//GHIJ
//KLMNO
//PQRSTU

#include <stdio.h>

#define ROWS 6
#define CHARS 21

int main(void)
{
int row;
char ch;

for(row = 0; row < ROWS; row++)
{
for(ch = ('A' + row); ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf("\n");
}

return 0;
}

最佳答案

你在这里:

#include <stdio.h>
#include <string.h>

int main( void )
{
const char *s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const size_t N = strlen(s);

while (1)
{
printf("Enter a non-negative number (0 - exit): ");

size_t n;
if (scanf("%zu", &n) != 1 || n == 0) break;

size_t k = 0;

for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j <= i; j++)
{
printf("%c", s[ k++ % N ]);
}
putchar('\n');
}
}

return 0;
}

程序输出可能如下所示:

Enter a non-negative number (0 - exit): 12
A
BC
DEF
GHIJ
KLMNO
PQRSTU
VWXYZAB
CDEFGHIJ
KLMNOPQRS
TUVWXYZABC
DEFGHIJKLMN
OPQRSTUVWXYZ
Enter a non-negative number (0 - exit): 0

关于C Primer Plus 第 6 版第 6 章编程练习 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964395/

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