TIL_231123

Z_์ œํŠธยท2023๋…„ 11์›” 23์ผ
0

TODAY I LEARNED

๋ชฉ๋ก ๋ณด๊ธฐ
19/88
post-thumbnail

to do_์˜ค๋Š˜ ํ•  ์ผ

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€๊ธฐ
  • Unity 2๊ธฐ 2~4์ฃผ์ฐจ ํŒ€ ๊ณผ์ œ ๋ฐœํ‘œ Time ~

retro_์˜ค๋Š˜ ํ•œ ์ผ(ํšŒ๊ณ )

[problem solved] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€๊ธฐ - ์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ ์— ๋Œ€ํ•˜์—ฌ

ํ•˜๊ณ ์‹ถ์—ˆ๋˜ ๋ฐฉ๋ฒ• :
// int ๋ฅผ string ์œผ๋กœ ๋ณ€ํ™˜
// string ์„ char[] ๋กœ ๋ณ€ํ™˜
// char[] ์„ int[] ๋กœ ๋ณ€ํ™˜


์‹œ๋„ํ•œ ์ฝ”๋“œ โ†“

using System;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        
        string str = n.ToString(); // int ๋ฅผ string ์œผ๋กœ ๋ณ€ํ™˜
        char[] char_array = str.ToCharArray(); // string ์„ char[] ๋กœ ๋ณ€ํ™˜
        int[] numbers = Array.ConvertAll(char_array, int.Parse); // char[] ์„ int[] ๋กœ ๋ณ€ํ™˜
        
        for (int i = 0; i < numbers.Length; i++){
            answer += numbers[i]; 
        }
        return answer;
    }
}

์–ด๋–ป๊ฒŒ๋“  ํ•ด๋ณด๋ ค๊ณ  1์‹œ๊ฐ„๋™์•ˆ ๊ณ ๋ฏผํ•œ ํ”์ .. ๋‚˜์ด์Šค ์ธ„๋ผ์ด but..
1์ฐจ์‹œ๋„ ์‹คํŒจ !
์ง€์‹์ด ๋ถ€์กฑํ•˜๋‹ค ๋ณด๋‹ˆ ์ •๋ง ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ์—ˆ๊ตฌ๋‚˜ ๊ณผ๊ฑฐ์˜ ๋‚˜ ์ž์‹ ..

์ถ”๊ฐ€ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉฐ
์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ์‚ฌ์‹ค !

string ์„ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์—ˆ๊ตฌ๋‚˜ ?! ๐Ÿ™Š
C# ๊ธฐ์ดˆํƒ„ํƒ„์ž‘์—…์„ ๋‹ค์‹œ ๋“ค์–ด๊ฐ€์•ผ๊ฒ ๋‹ค ํ•˜ํ•˜ํ•ซ ~_~

string ์„ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋˜๋ฉด char ๊ฐ€ return ๋œ๋‹ค.
"Gets the char object at a specified position in the current string object."


์ˆ˜์ • ์ฝ”๋“œ โ†“

using System;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        
        string str = n.ToString(); // int ๋ฅผ string ์œผ๋กœ ๋ณ€ํ™˜
        
        // string ์˜ index ๊ธธ์ด ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ๋‚˜์˜จ ๊ฐ’์ธ char ๋ฅผ string ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์ตœ์ข…์ ์œผ๋กœ int ๋กœ ๋ณ€ํ™˜ !
  		                                       
        for (int i = 0; i < str.Length; i++){            
            answer = int.Parse(str[i].ToString()) + answer;
        }
        return answer;
    }
}

//int ๋ฅผ string ์œผ๋กœ ๋ณ€ํ™˜
// string ์˜ index ๊ธธ์ด ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ๋‚˜์˜จ ๊ฐ’์ธ char ๋ฅผ string ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์ตœ์ข…์ ์œผ๋กœ int ๋กœ ๋ณ€ํ™˜ !


์ˆ˜์ • ์ฝ”๋“œ์—์„œ
`answer = int.Parse(str[i].ToString()) + answer;`
ToString() ์„ ์™œ ํ•ด์•ผํ•˜๋Š”๊ฐ€์— ๋Œ€ํ•œ ์˜๋ฌธ์ด ๋“ค์–ด ํŠœํ„ฐ๋‹˜ ์ฐฌ์Šค ์‚ฌ์šฉ !

์ •๋ฆฌํ•ด์ฃผ์‹  ๋‚ด์šฉ์€,
Parse ๋Š” ์ˆซ์ž(int)์˜ ๋ชจ์Šต์„ ํ•œ ๋ฌธ์ž์—ด(string)์„ ์ˆซ์ž(int)๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์ฃผ๋กœ ์“ฐ์ธ๋‹ค๊ณ  ํ•˜์…จ๋‹ค. (๋ฌผ๋ก  int.Parse() ๋ง๊ณ  bool.Parse() ๋„ ์žˆ์ง€๋งŒ !)

e.g.

string iStr = "10";
int x;
x = int.Parse(iStr); // x : 10

์ถ”๊ฐ€๋กœ,
unity ์—์„œ char ๋ฅผ int ๋กœ ๋ณ€ํ™˜ํ•  ์ผ์ด ์ž์ฃผ ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด ์—ฌ์ญค๋ดค๋Š”๋ฐ,
unity ์—์„œ char ๋ฅผ ๋‹ค๋ฃฐ ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค๊ณ  ํ•˜์…จ๋‹ค ^.^

char ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์‹œ๋ฉด์„œ
๋งํฌ_ASCII characters and their corresponding Decimal and Hex values ๋ฅผ
ํ•˜๋‚˜ ๊ณต์œ ํ•ด์ฃผ์…จ๋Š”๋ฐ

char ๋Š” ๋ณดํ†ต ์˜ˆ๋ฅผ ๋“ค์–ด,
char ch;
ch ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ํŠน์ • ๋ฌธ์ž์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž(์œ„ ๋งํฌ ์ฐธ๊ณ )๋ž‘ ์—ฐ๊ด€์ง€์–ด์„œ ๋‹ค๋ฃฌ๋‹ค๊ณ  ํ•˜์…จ๋‹ค.
e.g.
ch - '0';


์ผ๋‹จ์€ ์ด๋Ÿฐ๊ฒŒ ์žˆ๋‹ค ~ ๋ผ๊ณ ๋งŒ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ์ข‹๋‹ค๊ณ  ๋ง์”€ํ•˜์…จ๋‹ค.
ํŠœํ„ฐ๋‹˜์€ ๋งˆ์น˜ ์•„๋‚Œ์—†์ด ์ฃผ๋Š” ๋‚˜๋ฌด..S2 ์ตœ๊ณ ...๐Ÿ‘


์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ ๋ฌธ์ œ๋ฅผ Char.GetNumericValue() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์ด ver. โ†“

public int solutionWithGetNV(int n)
{
    int answer = 0;

    string str = n.ToString(); // int ๋ฅผ string ์œผ๋กœ ๋ณ€ํ™˜

    // string ์˜ index ๊ธธ์ด ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ..........
    for (int i = 0; i < str.Length; i++)
    {
        answer = (int)Char.GetNumericValue(str[i]) + answer;
    }
    return answer;

์ฐธ๊ณ ์‚ฌ์ดํŠธ


๋™์ผํ•œ ๋ฌธ์ œ๋ฅผ ํ˜•๋ณ€ํ™˜ ์—†์ด ์ˆซ์ž๋กœ ํ’€์ด ver. โ†“

using System;

public class Solution
{
    public int solution(int n)
    {
        int answer = 0;
        
        while (n > 0)
        {
            answer += n % 10;
            n = n / 10;
        }
        return answer;
    }
}

์œ ์—ฐํ•œ ์‚ฌ๊ณ ๋ฐฉ์‹ ์ •๋ง ์ค‘์š”..โ˜…
์ง„์งœ ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋“ค์„ ์ ‘ํ•ด๋ด์•ผ๊ฒ ๋‹ค ๐Ÿ”ฅ


[MEMO] ํ”„๋กœ์ ํŠธ ํ”ผ๋“œ๋ฐฑ

์šฐ๋ฆฌ ์กฐ ํ”„๋กœ์ ํŠธ ํ”ผ๋“œ๋ฐฑ

  • UI very good !
  • ๋ฆฌํŒฉํ† ๋ง๋„ ๊พธ์ค€ํžˆ ํŒŒ์ดํŒ… !
  • ํ”„๋กœ์ ํŠธ ๋ณต์žกํ•ด์ง€๋ฉด ๋†“์น˜๋Š” ๋ถ€๋ถ„ ์ƒ๊ธธ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ํ•ญ์ƒ ์กฐ์‹ฌ !

๋‹ค๋ฅธ ์กฐ์˜ ํ”ผ๋“œ๋ฐฑ ์ฅฝ์ฅฝ

  • ASCII ์•„ํŠธ ๊ตฌํ˜„ ํ•  ๋•Œ Window ์กฐ์ •ํ•˜๊ธฐ
  • ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜
  • "ํ™•์žฅ์„ฑ" ์„ ์—ผ๋‘ํ•ด๋‘์ž !
  • LINQ ๋Š” ๋จผ์ € ์กฐ๊ฑด&๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ธฐ๋Šฅ์„ ๋˜‘๊ฐ™์ด ๊ตฌํ˜„์„ ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ, ๊ทธ ๋•Œ LINQ ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ !
  • ๊ธฐ๋Šฅ๊ด€๋ จ ์ฝ”๋“œ๋Š” Utils(์œ ํ‹ธs) ํด๋”๋กœ ๋”ฐ๋กœ ๋นผ์„œ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ good ~!
  • git ์˜ ์ปค๋ฐ‹ ์ปจ๋ฒค์…˜ !
  • ์†Œํ†ต :-)

profile
trying to make the world a better place with a cool head and warm heart

0๊ฐœ์˜ ๋Œ“๊ธ€