本文介绍了方形图案问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 1  5  9 13
 4  8 12 16
 7 11 15 19
10 14 18 22







已添加代码块 - OriginalGriff [/ edit]



我尝试过:



i只能按行和列功能打印方形图案中的自然数字,例如




[edit]Code block added - OriginalGriff[/edit]

What I have tried:

i can only able to print natural numbers in square pattern by row and column feature like

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15




#include<stdio.h>
#include<conio.h>

int main()
{
    int i, j, diff, value, N;

    printf("Enter rows: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        diff = N-1;
        value = i;
        for(j=1; j<=i; j++)
        {
            printf("%-3d", value);

            value += diff;
            diff--;
        }

        printf("\n");
    }

    return 0;
}

推荐答案

int data[4][4];

填充完成后,打印它的两个嵌套循环很简单:

Once filled, it's a simple matter of two nested loops to print it:

for (y = 0; y < size; y++)
   {
   for (x = 0; x < size; x++)
      {
      printf("%-3d ", data[x][y]);
      }
   printf("\n");
   }

填充更复杂。在您显示的数据的情况下,它并不简单,因为它不是简单序列:2,3,6,17,20和21是缺失,并且没有明显的顺序为什么。

如果是这样的话:

Filling is more complex. In the case of the data you show, it's not simple, because it isn't a "simple sequence": 2, 3, 6, 17, 20, and 21 are "missing" and there is no obvious order as to why.
If it was like this:

1  3  6 10
2  5  9 13
4  8 12 15
7 11 14 16

然后它更简单:每个数字进入一个单元格,然后重新计算下一个单元格的x和y,从单元格(0,0)开始在数字增加之前。

如果x在行的末尾,x变成y加1,y变成最后一行。

否则,如果y开启在第一行,y变为x加1,x变成第一列。

否则,x增加,y减少。

Then it's a lot simpler: each number goes into the a cell, and then the x and y for the next cell are recalculated, starting from cell (0, 0) before the number is incremented.
If x is at the end of the row, x becomes y plus one and y becomes the last row.
Otherwise, if y is on the top row, y becomes x plus one and x becomes the first column.
Otherwise, x is increased and y is decreased.


n(r,c) = n = 1 + 3 * r + 4 * c

因此

Hence

#include <stdio.h>
int main()
{
  int r, c;
  for (r=0; r<4; ++r)
  {
    for (c=0; c<4; ++c)
    {
      int n = 1 + 3 * r + 4 * c;
      printf("%3d", n);
    }
    printf("\n");
  }
  return 0;
}


引用:

我只能通过行和列功能打印正方形图案中的自然数字

i can only able to print natural numbers in square pattern by row and column feature like



作为程序员,您的工作是创建算法来解决特定问题。如果我们只是给你答案,你永远不会学会如何自己找到它。

如果只给出输出,你必须找到用于构建输出的模式,换句话说,这就像找到第五列或第五行的值是什么?


As programmer, your job is to create algorithms that solve specific problems. If we just give you the answer, you will never learn how to find it yourself.
If you are only given the output, you have to find the pattern used to build the output, in other words, it is like finding "what would br the values of a fifth column or row?"

1  5  9 13 ??





我们不做你的家庭作业。

HomeWork不会在乞求别人做你的工作时测试你的技能,它会让你思考并帮助您的老师检查您对所学课程的理解,以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请展示您的代码并解释这个问题,我们可能会提供帮助。



We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.


这篇关于方形图案问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:21