본문 바로가기

C#/Problems

2020-05-03 2048 과제중 실수한것.

하나씩 만들어보려던 도중,

1차원배열값을 왼쪽으로 옮기는 연습을 먼저 해보았다.

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

App.Class

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
 
namespace _2048_test
{
    class App
    {
        int[] arrInt;
        public App()
        {
            arrInt = new int[4] { 0,0,0,2};
 
            Console.WriteLine("집에서 짠 Sort 로직");
 
            this.Sort1();
 
            Console.WriteLine("학원에서 짠 Sort 로직");
 
            this.Sort2();
        }
 
        public void Print()
        {
            for (int i = 0; i < this.arrInt.Length; i++)
            {
                Console.Write("[{0}]"this.arrInt[i]);
            }
            Console.WriteLine();
 
        }
        public void Sort1()
        {
            for (int i = 0; i < this.arrInt.Length; i++)
            {
                if (this.arrInt[i] == 2)
                {
                    for (int j = 0; j < this.arrInt.Length; j++)
                    {
                        this.arrInt[i-j] = 2;
                        this.Print();
                    }
                }
            }
        }
 
 
        public void Sort2()
        {
            for (int i = 0; i < this.arrInt.Length; i++)
            {
                for (int j = 0; j < this.arrInt.Length; j++)
                {
                    if (this.arrInt[i] == 2 && this.arrInt[0== 0)
                    {
                        this.arrInt[i - 1= 2;
                        this.arrInt[i] = 0;
                    }
                }
            }
        }
 
 
 
 
    }
}
 
 

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

집에서 배열옮기는 로직 구상

 

학원에서 짠 Sort 로직

집에서 짠 Sort 로직

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

왜대체 한꺼번에 묶어서 키입력값 하나만 받으면 옆으로 한번에 옮길라고 생각했을까?

순차적으로 생각했을때 0보다 큰 배열을 찾고 한칸씩 옆으로 그리고 그 인덱스만큼 반복하면 된다는걸 생각조차 못했다.

여기저기 찾아보고 고민해봤는데, 아침에 일어나서 이것때매 몇시간동안 머리싸매고, 순서도 그리고, 별짓거리 다해가면서 생각했는지 모르겠다.