如何打印矩阵? [英] How to print the matrix?

查看:151
本文介绍了如何打印矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要打印矩阵这样



考虑原始矩阵如下所示



< pre lang =text> 1 2 3
4 5 6
7 8 9





现在我想要打印如下



step1:打印1



step2:打印2



step3:打印3



step4:打印6



step5:打印9



step6:打印8



step7:打印7



step8:打印4



step9:打印5



 1 2 3 
6第一步
9

1 2 3
6秒步骤
8 9


1 2 3
6第3步
7 8 9

1 2 3
4 6第4步
7 8 9

1 2 3
4 5 6第5步
7 8 9





我尝试了什么:



i试图显示代码abo ve方式?

解决方案

首先......对不起但是请注意一下:

贵公司规则不允许显示代码...它是完全明白的,我发现你遵守规则非常好。

但是......

你正在寻求帮助,重复3次相同的句子没有澄清任何事情。

如果你不想显示公司的代码,那么就不要这样做,但至少你还可以使用伪代码来解释您正在执行的步骤以及您在哪里遇到问题

了解如何使用调试器。这是你应该能够自己快速解决的事情



请不要个人接受,这不是对你的攻击,只是一个哲学问:你确定你准备好做这个工作吗?

而且我不是在谈论技术内容,每个人都是初学者。我正在思考其他的能力......即好奇心,坚韧,逻辑思维和其他相关的东西。



我建议你先自己尝试并从中学习你的错误,这是你能得到的最好的学校。不要轻易放弃,来这里问。当然它很舒服,你可能总会找到一个能回答你的人。最重要的一点是......你不会学到很多东西,你会错过对你灵魂的巨大回报,那就是能够自己解决问题的满足感。



----------------------

现在到技术方面。我不打算给你一个现成的解决方案,也许有更好的方法可以解决这个问题,但是...它仍然是一种方法,你可以推断/猜测只是拿纸和铅笔做2或3个例子来搜索模式:



假设矩阵A(MxN)无论M和N值



1)考虑通过矩阵作为正方形/矩形,每个都是一个完整的迭代。你需要这么多迭代((最小索引/ 2)+(小索引%2))



A(2x6)= 2/2 + 2 %2 = 1个矩形或迭代

A(6x3)= 3/2 + 3%2 = 2个矩形或迭代

A(7x7)= 7/2 + 7 %2 = 4个矩形或迭代



2)你如何做每个方格?

第1行:从A(1 + squareNr,1 + squareNr)到A(1 + squareNr,N-squareNr)

第2行:从A(1 + squareNr) ,N-squareNr)到A(M-squareNr,N-squareNr)

第3行:从A(M-squareNr,N-squareNr)到A(M-squareNr,1 + squareNr)

第4行:从A(M-squareNr,1 + squareNr)到A(1+ (squareNr + 1),1 + squareNr)



3)当你到达最后一个元素时结束= M * N

A(2x6)= 12

A(6x3)= 18

A(7x7)= 49



4)现在你只需要弄清楚如何使用循环和条件来管理这个。要点2)正在考虑基于0的迭代,如果你更喜欢开始计数,那么请调整它。



编辑以纠正拼写并使句子更加连贯


这就是家庭作业的气味,所以没有代码!

这并不困难,只是很麻烦。

声明八个变量:X,Y,dX,dY,minX,maxX,minY,maxY。

分别将它们设置为0,0,1,0,0,宽度,0,高度。

现在设置一个循环来查看每个单元格:width * height。

在循环内部,打印X,Y单元格。

现在将dX添加到X并将dY添加到Y.

检查X:

如果它等于maxX则将其减1,将y递增1,递增minX并设置dX为0,dY为1.

如果它等于minX则将其递增1,将y递减1,递减maxY,并将dX设置为0,将dY设置为-1。

检查Y:

如果它等于maxY则将其减1,将x减1,减小maxX,将dX设置为-1,将dY设置为0.

如果它等于minY则将其递增1,将x递增1,递增minX,并将dX设置为1,将dY设置为0。 >


应该这样做。


I wants to print matrix such that

Consider that original matrix look like following

1 2 3 
4 5 6
7 8 9



Now I want to print like following

step1: print 1

step2: print 2

step3: print 3

step4: print 6

step5: print 9

step6: print 8

step7: print 7

step8: print 4

step9: print 5

1 2 3
    6  first step
    9

1 2 3
    6 second step
  8 9


1 2 3
    6 3rd step
7 8 9

1 2 3
4   6 4th step
7 8 9

1 2 3
4 5 6  5th step 
7 8 9



What I have tried:

i have tried to display the code above manner?

解决方案

First of all... sorry but one note aside:
That your company rules don't allow to show code... it is perfectly understable and I find it very good that you follow the rules.
But...
You are asking for help, repeating 3 times the same sentence doesn't clarify anything.
If you don't want to show the code of the company, then don't do it, but at least you still can use pseudo-code to explain the steps you are doing and where are you getting problems
Learn how to use the debugger. This is something you should be able to solve on your own actually pretty quick

Please don't take it personally, it is not an attack against you, just a philosophical question: Are you sure you are prepared to do this job?
And I am not speaking about the technical content, everyone is a newbie at the beggining. I am thinking on other habilities... i.e. curiosity, tenacity, logical thinking and others related.

I would recommend you to try it first on your own and learn from your mistakes, this is the best school you can get. Don't give up easily and come here to ask. Of course it is confortable and you will probably always find someone that answers you. The most important point is... you won't learn so much and you will miss a very big reward for your soul, that is the satisfaction of being able to solve things on your own.

----------------------
Now to the technical side. I am not going to give you a ready solution, and probably there are better ways to solve this, but... it is still a way that you can infere / guess just taking paper and pencil and doing 2 or 3 examples to search for a pattern:

Assuming a Matrix A(MxN) no matter M and N values

1) Think on going through your matrix as squares / rectangles, each of them is one full iteration. You will need so many iterations as the ((smallest index / 2) + (smalles index % 2))
i.e.
A(2x6) = 2/2 + 2%2 = 1 rectangle or iteration
A(6x3) = 3/2 + 3%2 = 2 rectangles or iterations
A(7x7) = 7/2 + 7%2 = 4 rectangles or iterations

2) How do you do each square?
Line 1: From A(1+squareNr, 1+squareNr) to A(1+squareNr, N-squareNr)
Line 2: From A(1+squareNr, N-squareNr) to A(M-squareNr, N-squareNr)
Line 3: From A(M-squareNr, N-squareNr) to A(M-squareNr, 1+squareNr)
Line 4: From A(M-squareNr, 1+squareNr) to A(1+(squareNr+1), 1+squareNr)

3) You end when you reach the last element = M*N
A(2x6) = 12
A(6x3) = 18
A(7x7) = 49

4) Now you only have to figure out how to use the loops and the conditions to manage this. Point 2) is considering 0-based iterations, adapt it if you prefer start counting in 1

edits to correct spelling and make sentence more coherent


This smells of homework, so no code!
It's not difficult, it's just cumbersome.
Declare eight variables: X, Y, dX, dY, minX, maxX, minY, maxY.
Set them to 0, 0, 1, 0, 0, width, 0, height respectively.
Now set up a loop to look at each cell: width * height.
Inside the loop, print the X,Y cell.
Now add dX to X and dY to Y.
Check X:
If it's equal to maxX then reduce it by one, increment y by one, increment minX, and set dX to 0, dY to 1.
If it's equal to minX then increment it by one, reduce y by one, decrement maxY, and set dX to 0, dY to -1.
Check Y:
If it's equal to maxY then reduce it by one, decrement x by one, decrement maxX, and set dX to -1, dY to 0.
If it's equal to minY then increment it by one, increment x by one, increment minX, and set dX to 1, dY to 0.

That should do it.


这篇关于如何打印矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆