我有一个像这样的c printf程序
for (r = 0; r <= 100 ; r++) {
for (c = 0; c <= 100 ; c++) {
printf("A");
}
}
如何编辑代码以便打印5&#34; A&#34;每行如
&#34; AAAAA&#34;但多行
答案 0 :(得分:2)
检查除法c / 5是否以0的余数返回。
for (r = 0; r <= 20 ; r++) {
for (c = 0; c <= 20 ; c++) {
if (c%5 == 0)
printf("\r\n");
printf("A");
}
}
答案 1 :(得分:1)
你只需要将列数减少到5并在每个列周期结束后打印换行符如下:
for (int row = 0; row < 20; ++row) {
for (int column = 0; column < 5; ++column) {
printf("A");
}
printf("\n");
}
答案 2 :(得分:0)
如果打印的字母数是5的倍数,请打印'\n'
void NuBePrint(int rows, int cols, int period, char letter) {
long count = 0;
// OP may really want `r < rows` and `c < cols` to print 100x100 rather than 101x101
for (int r = 0; r <= rows ; r++) {
for (int c = 0; c <= cols; c++) {
putchar(letter);
count++;
if (count%period == 0) putchar('\n');
}
}
if (count%period != 0) putchar('\n'); // Cope with last line
}
// sample usage
NuBePrint(100, 100, 5, 'A');