简体   繁体   English

如何在 C 中的嵌套 for 循环构造中处理 printf?

[英]How do I handle printf in a nested for Loops construct in C?

I need to implement a function using nested for loops in C.我需要在 C 中使用嵌套的 for 循环来实现一个函数。

The function has the following declaration:该函数具有以下声明:

void nest_loop(int n);

and should print the following for the value n = 5并且应该为值 n = 5 打印以下内容

A B C D E
  A B C D
    A B C
      A B
        A

However I need to implement the function so it produces output for any unsigned int n from the following Intervall [0,9]但是我需要实现该函数,以便它为以下 Intervall [0,9] 中的任何 unsigned int n 生成输出

In the case of n = 5在 n = 5 的情况下

From my understanding there are 5 for loops.根据我的理解,有 5 个 for 循环。

for_loop1 prints 5 times the char 'A' for_loop1 打印 5 次字符 'A'

for_loop2 prints 4 times the char 'B' for_loop2 打印 4 次字符 'B'

for_loop3 prints 3 times the char 'C' for_loop3 打印 3 次字符 'C'

for_loop4 prints 2 times the char 'D' for_loop4 打印 2 次字符 'D'

for_loop5 prints 1 time the char 'E' for_loop5 打印 1 次字符 'E'

Also from my understanding the nested loop construct should look something like this:同样根据我的理解,嵌套循环结构应该是这样的:

int n = 5;
int c = n - 1;

for(int i = c ; i < 0; i--){ // loop for 'E'
  c--;
  for(int j = c; j < 0; j--){ // loop for 'D'
    c--;
   for(int k = c; k < 0; k--){ // loop for 'C'
     c--;
    for(int l = c; l < 0; l--){ // loop for 'B'
      c--;
     for(int p = c; p < 0; p--){ // loop for 'A'
     }
    }
   }
  }
}

Is my nested for loop construct correct?我的嵌套 for 循环构造是否正确? If yes, then how do I now implement printf to produce the output mentioned?如果是,那么我现在如何实现 printf 来产生提到的输出?

As explained by @Jonathon Reinhart, you only need two loops in a construct similar to:正如@Jonathon Reinhart 所解释的,您只需要在类似于以下结构的两个循环:

for (int i = n; i > 0; i--) {
  for (int j = 0; j <= i; j++) {
    [...]
  }
}

You know in book also we write left to right not top to bottom so your understanding is wrong above about your loops.你也知道在书中我们是从左到右而不是从上到下写的,所以你对循环的理解是错误的。 Feel free to ask any clarifications thank you.请随时提出任何澄清谢谢。

 #include<stdio.h>
    void nest_loop(int n)
    {
        int i,j;
        char k = 'A';
        for(i=n;i>0;i--)
        {
            for(j=n;j>i;j--)
            {
                printf(" ");//print required spaces.
            }
            for(j=0;j<i;j++)
            {
                printf("%c",k+j);//initial char value of k is A and will be incremented by one as looping continues.
            }
            printf("\n");
        }
    }
    int main()
    {
        int n;
        printf("Enter the row number\n");
        scanf("%d",&n);
        nest_loop(n);
        return 0;
    }

Here is the functional code.这是功能代码。

#include <stdio.h>
#include <ctype.h>

void nest_loop(int n) {
    for(int i = 0; i < n; i++){
        for (int j = 0; j < i; j++)
            printf("  ");
        char init_char = 'A';
        for(int j = 0; j < n-i; j++, init_char++)
            printf("%c ", init_char);
        printf("\n");
    }
}

int main()
{
    int num;
    printf("input number: ");
    scanf("%d", &num);
    nest_loop(num);
    return 0;
}

The output is输出是

input number: 5
A B C D E 
  A B C D 
    A B C 
      A B 
        A 

Here you have functional code for your purpose在这里,您有用于您的目的的功能代码

#include <stdio.h>
#include <ctype.h>

int main()
{
    int num;
    scanf("%d", &num);
    char init_char = 'A';

    for(int i = 0; i < num; i++, init_char++){
        for(int j = num; j > i; j--)
            printf("%c", init_char);
        printf("\n");
    }

        return 0;
}

the answer is well explained in comments, good luck :) *I'd add that in C language you can also increment chars, read what is ASCII table and how chars are encoded as numbers答案在评论中得到了很好的解释,祝你好运:) *我想补充一点,在 C 语言中,你还可以增加字符,阅读什么是 ASCII 表以及字符如何编码为数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM