It's a Python question

Asked 2 years ago, Updated 2 years ago, 94 views

Write a program that receives two integers N and M and outputs numbers from 1 to N in the form of an M * M matrix.

[Input Example 1]

2
3

[Output Example 1]

121
212
121

[Input Example 2]

3
5

[Output Example 2]

12312
31231
23123 
12312
31231

[Input Example 3]

4
4

[Output Example 3]

1234
1234
1234
1234

(In the output example, the space where the space is written is a line change.))
Even if you use a double for statement, it only initializes the number, but it is not printed in order I'm going to ask the masters what to do ㅠ<

for

2022-09-22 16:18

1 Answers

1. If it were me, I would go around while rather than for. Because you have to keep doing something annoying until you write M * M letters.

2. What is that bothersome thing? Let's write it by hand.If the input example is:

3 #For convenience, I will call it N
5 # I'll call you M

First, let's write it in the pattern of 1, 2, 3, 1, 2, 3, 1,... It's a logic that starts with 1 and then starts with 1 again after +1 until N. It's going to be like this.

12312

What if I write 3 next? No! We already wrote M on this line, right?
You have to go over a line.

12312
3

Let's just write it without thinking.

12312
31231

What if I write 2 next? No! I already wrote M on this line!
We have to move on to another line.

I'll try until I'm tired of this bother.

12312
31231
23123
12312
31231

What if I write 2 on the next line? You can't! I already printed out a total of M*M characters!!!
If you stop here and return the result, you'll get the answer you want.

Can I change this procedure to a program? There are parts where you process movements when the conditions are true, there are parts where you count things, there are parts where you repeat certain movements indefinitely until the conditions are good, and you will remember what you learned. Give that a try.


2022-09-22 16:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.