-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add week0002.cs #70
base: master
Are you sure you want to change the base?
add week0002.cs #70
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Week0002 | ||
{ | ||
public static class Week0002 | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
while (true) | ||
{ | ||
Console.Write("Input M: "); | ||
var m = Convert.ToInt32(Console.ReadLine()); | ||
Console.Write("Input N: "); | ||
var n = Convert.ToInt32(Console.ReadLine()); | ||
|
||
var arr = Cal(m, n); | ||
|
||
foreach (var i in arr) | ||
{ | ||
foreach (var j in i) | ||
{ | ||
Console.Write(Draw(j, m * n) + " "); | ||
} | ||
Console.WriteLine(); | ||
} | ||
Console.WriteLine(); | ||
} | ||
} | ||
|
||
public static List<List<int>> Cal(double m, double n) | ||
{ | ||
var array = new List<List<int>>(); | ||
|
||
//Allocate 2 dimension array | ||
for (var i = 0; i < m; i++) | ||
{ | ||
array.Add(new List<int>()); | ||
for (var j = 0; j < n; j++) | ||
{ | ||
array[i].Add(0); | ||
} | ||
} | ||
|
||
var pointer = new Cell(0, 0); | ||
|
||
//Change direction points | ||
var MinMin = new Cell(1, 0); | ||
var MinMax = new Cell(0, n - 1); | ||
var MaxMax = new Cell(m - 1, n - 1); | ||
var MaxMin = new Cell(m - 1, 0); | ||
|
||
var move = new Cell(0, 1); | ||
|
||
for (var i = 1; i <= m * n; i++) | ||
{ | ||
array[(int)pointer.X][(int)pointer.Y] = i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's confusion: In your case It is counter-intuitive and hard to read. I would suggest to swap X & Y so they carry default meaning: Y for row & X for column. |
||
|
||
if (pointer.Equal(MinMin)) | ||
{ | ||
move = new Cell(0, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are moving |
||
|
||
if (MinMin.X + 1 <= (m - 1) / 2 && MinMin.Y + 1 < (n - 1) / 2) | ||
{ | ||
|
||
MinMin.X++; MinMin.Y++; | ||
} | ||
} | ||
else if (pointer.Equal(MinMax)) | ||
{ | ||
move = new Cell(1, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are moving |
||
if (MinMax.X + 1 <= (m - 1) / 2 && MinMax.Y - 1 >= (n - 1) / 2) | ||
{ | ||
MinMax.X++; MinMax.Y--; | ||
} | ||
} | ||
else if (pointer.Equal(MaxMax)) | ||
{ | ||
if (MaxMax.X - 1 >= (m - 1) / 2 && MaxMax.Y - 1 >= (n - 1) / 2) | ||
{ | ||
MaxMax.X--; MaxMax.Y--; | ||
} | ||
move = new Cell(0, -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are moving |
||
} | ||
else if (pointer.Equal(MaxMin)) | ||
{ | ||
if (MaxMin.X - 1 > (m - 1) / 2 && MaxMin.Y + 1 <= (n - 1) / 2) | ||
{ | ||
MaxMin.X--; MaxMin.Y++; | ||
} | ||
move = new Cell(-1, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are moving |
||
} | ||
|
||
|
||
pointer += move; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are mixing up |
||
} | ||
|
||
return array; | ||
} | ||
public static bool Equal(this Cell cell, Cell obj) | ||
{ | ||
return cell.X == obj.X && cell.Y == obj.Y; | ||
} | ||
|
||
public static string Draw(int num, int max) | ||
{ | ||
var result = ""; | ||
var size = max.ToString().Count() - num.ToString().Count(); | ||
for (var i = 0; i < size; i++) | ||
{ | ||
result += " "; | ||
} | ||
return result += num; | ||
} | ||
} | ||
|
||
public class Cell | ||
{ | ||
public Cell(double x, double y) | ||
{ | ||
X = x; | ||
Y = y; | ||
} | ||
|
||
public double X { get; set; } | ||
public double Y { get; set; } | ||
|
||
public static Cell operator +(Cell c1, Cell c2) | ||
{ | ||
return new Cell(c1.X + c2.X, c1.Y + c2.Y); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename: