[๐Ÿ“ iOS TIL] #1 ... ๐ŸŽ Swift ๏ผ† Algorithm ๐Ÿ–ฅ๏ธ

TaeUkยท2024๋…„ 1์›” 23์ผ
0

๐ŸŽ iOS TIL

๋ชฉ๋ก ๋ณด๊ธฐ
1/18
post-thumbnail

0. ์‹œ์ž‘! ๐ŸŽ‰

์˜ค๋Š˜๋ถ€ํ„ฐ ๋งค์ผ๋งค์ผ ๊ณต๋ถ€ํ•œ Swift๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

์ฒ˜์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ์ ‘ํ•ด๋ณด์•˜๋‹ค! ๐Ÿ™‰


1. lv.0 ๊ฐ๋„๊ธฐ

  • ๋ฌธ์ œ ์„ค๋ช…
    ๊ฐ์—์„œ 0๋„ ์ดˆ๊ณผ 90๋„ ๋ฏธ๋งŒ์€ ์˜ˆ๊ฐ, 90๋„๋Š” ์ง๊ฐ, 90๋„ ์ดˆ๊ณผ 180๋„ ๋ฏธ๋งŒ์€ ๋‘”๊ฐ 180๋„๋Š” ํ‰๊ฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ angle์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ์˜ˆ๊ฐ์ผ ๋•Œ 1, ์ง๊ฐ์ผ ๋•Œ 2, ๋‘”๊ฐ์ผ ๋•Œ 3, ํ‰๊ฐ์ผ ๋•Œ 4๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์˜ˆ๊ฐ : 0 < angle < 90
์ง๊ฐ : angle = 90
๋‘”๊ฐ : 90 < angle < 180
ํ‰๊ฐ : angle = 180

๋ฌธ์ œ ํ’€์ด

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int angle) {
    int answer = 0;
    
    if (0 < angle && angle <= 180) {
        if (0 < angle && angle <90) answer = 1;
        else if (angle == 90) answer = 2;
        else if (90 < angle && angle < 180) answer = 3;
        else if (angle == 180) answer = 4;
    }
    
    return answer;
}

2. lv.0 ์ง์ˆ˜์˜ ํ•ฉ

  • ๋ฌธ์ œ ์„ค๋ช…
    ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ, n์ดํ•˜์˜ ์ง์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
๋ฌธ์ œ ํ’€์ด

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int n) {
    int answer = 0;
    
    for (int i = 1; i <= n; i++) {
        ((i%2) == 0) ? (answer += i) : 0;
    }
    
    return answer;
}

3. lv.0 ๋ฐฐ์—ด์˜ ํ‰๊ท ๊ฐ’

  • ๋ฌธ์ œ ์„ค๋ช…
    ์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์˜ ์›์†Œ์˜ ํ‰๊ท ๊ฐ’์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
๋ฌธ์ œ ํ’€์ด

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// numbers_len์€ ๋ฐฐ์—ด numbers์˜ ๊ธธ์ด์ž…๋‹ˆ๋‹ค.
double solution(int numbers[], size_t numbers_len) {
    double answer = 0;
    
    for (int i=0; i < numbers_len; i++) {
        answer += numbers[i];
    }
    
    answer = answer/numbers_len;
    
    return answer;
}

์œ„ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ๋ฌธ์ œ๋ฅผ ํ‘ผ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ, ์ƒ๊ฐํ•˜์ง€๋„ ๋ชปํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ ํ’€์ด๋ฅผ ํ•œ ์‚ฌ๋žŒ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ๋‚ด ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋จผ๊ฐ€ ์•„์‰ฌ์›€์„ ๋Š๋‚„ ์ˆ˜ ์žˆ์—ˆ๋‹ค... ๐Ÿ˜ฟ

๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , ๋‚ด๊ฐ€ ํ‘ผ ๋ฌธ์ œ์˜ ํ’€์ด๊ฐ€ ๋ง˜์— ์•ˆ๋“ ๋‹ค... ๐Ÿ™ˆ

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