简体   繁体   English

这里的模式是什么? 我怎样才能在 c 中编写该代码?

[英]What is the pattern here? And how can i write that code in c?

But I have issues.但我有问题。 I don't understand the pattern very well.我不太了解这种模式。 I don't know how to make an even number part.我不知道如何制作偶数部分。 I have some guess.我有一些猜测。 This pattern has 2 parts and I don't know how to make this in c.这种模式有 2 个部分,我不知道如何在 c 中制作它。 What I've been able to do so far:到目前为止我能做的:

#include <stdio.h>
#include <stdlib.h>
    
int main(int argc, char *argv[])
{
    int a,n;

    printf("Enter a number:");
    scanf("%d",&a),

    n=0;
    while(n<=a)    
    {
        printf("%d|\n",n);
        n++;
    }
    
    return 0;
}

You need two loops for each row每行需要两个循环
One will print the even numbers (0,2,4,6..).一个将打印偶数(0,2,4,6..)。 The number of even numbers (if you count zero) is just the row number.偶数的数量(如果你算零)只是行号。 Don't print a new line yet.不要打印新行。

Then, at the end of the row, print numbers starting at the row number, and ending with (the last even number you printed)+1然后,在行的末尾,从行号开始打印数字,并以(您打印的最后一个偶数)+1 结尾

At the end of each row print a newline.在每一行的末尾打印一个换行符。

I hope this will give you a hint as to how to continue我希望这会给你一个关于如何继续的提示

Pseudocode of the algorithm:算法伪代码:

n = user_input()
for i in 0 .. n:
  for j in 0 .. i:
      print(2 * j)
  for j in 0 .. i:
      print(i + j)
  print newline

Converting that to C, we get something pretty similar.将其转换为 C,我们得到了非常相似的结果。

int main() {
  int i, j, n;
  printf("Enter a number:");
  scanf("%d",&n);

  for (i=0; i<n; i++) {
    for (j = 0; j < i; j++) {
      printf("%2d ", 2 * j);
    }
    for (j = 0; j < i; j++) {
      printf("%2d ", i + j);
    }
    printf("\n");
  }
}

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

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