• [๋ฐฑ์ค€] 1158๋ฒˆ ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

    2020. 2. 8.

    by. ํ•ด๋Š”์„ 

    ๋ฌธ์ œ

    ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ N๋ช…์˜ ์‚ฌ๋žŒ์ด ์›์„ ์ด๋ฃจ๋ฉด์„œ ์•‰์•„์žˆ๊ณ , ์–‘์˜ ์ •์ˆ˜ K(โ‰ค N)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์ œ ์ˆœ์„œ๋Œ€๋กœ K๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ œ๊ฑฐํ•œ๋‹ค. ํ•œ ์‚ฌ๋žŒ์ด ์ œ๊ฑฐ๋˜๋ฉด ๋‚จ์€ ์‚ฌ๋žŒ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์›์„ ๋”ฐ๋ผ ์ด ๊ณผ์ •์„ ๊ณ„์†ํ•ด ๋‚˜๊ฐ„๋‹ค. ์ด ๊ณผ์ •์€ N๋ช…์˜ ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ์ œ๊ฑฐ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค. ์›์—์„œ ์‚ฌ๋žŒ๋“ค์ด ์ œ๊ฑฐ๋˜๋Š” ์ˆœ์„œ๋ฅผ (N, K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (7, 3)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์€ <3, 6, 2, 7, 5, 1, 4>์ด๋‹ค.

    N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง€๋ฉด (N, K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

    ์ž…๋ ฅ

    ์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค K โ‰ค N โ‰ค 5,000)

    ์ถœ๋ ฅ

    ์˜ˆ์ œ์™€ ๊ฐ™์ด ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.


    ํ’€์ด

    ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์›ํ˜• ํ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  head๊ฐ€ NULL์ด ๋  ๋•Œ ๊นŒ์ง€ ๊ณ„์† ๋Œ๋ ค์ค€๋‹ค.

    #include <stdio.h>
    #include <stdlib.h>
    struct node { int key; struct node* next; };
    struct node* head=NULL;
    void in(int key) {
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    newnode->key = key;
    struct node* ptr = head;
    if (head == NULL) {
    head = newnode;
    newnode->next = newnode;
    }
    else {
    while (ptr->next != NULL && ptr->next != head) { ptr = ptr->next; }
    ptr->next = newnode;
    newnode->next = head;
    }
    }
    void out(struct node* ptr) {
    printf("%d", ptr->key);
    struct node* newptr = head;
    if (head->key == ptr->key && head->next == head) {
    head = NULL;
    return;
    }
    while (newptr->next->key != ptr->key)newptr = newptr->next;
    if (head->key == ptr->key) head = ptr->next;
    newptr->next = ptr->next;
    free(ptr);
    }
    int main(void) {
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
    in(i);
    }
    int check = 0;
    int once = 1;
    struct node* run = head;
    printf("<");
    while (head != NULL) {
    check++;
    if (check%k == 0 && check != 0) {
    if (once != 1) {
    printf(", ");
    }
    else once = 0;
    struct node* ptr = run;
    run = run->next;
    out(ptr);
    }
    else {
    run = run->next;
    }
    }
    printf(">");
    }

    ๊ทธ์ƒˆ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ๋งŒ๋“œ๋Š” ๋ฒ• ๊นŒ๋จน์–ด์„œ 2ํ•™๊ธฐ์— ์ˆ˜์—…ํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์™”๋‹ค.

    ์ถœ๋ ฅํ•  ๋•Œ, ', '์„ ๋งจ ์ฒ˜์Œ๊ณผ ๋์—๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š”๋ฐ ์ฒ˜์Œ์—๋Š” ์ถœ๋ ฅ์„ ์•ˆํ•˜๊ณ , ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „์— ์ถœ๋ ฅํ•˜๊ฒŒ ํ•˜๋ฉด์„œ ๋งจ ๋งˆ์ง€๋ง‰์—๋Š” ๋“ค์–ด๊ฐ€์ง€ ์•Š๊ฒŒ ํ–ˆ๋‹ค.

    ๋˜, count๋ฅผ ์…€ ๋•Œ out(), check++, run=run->next์˜ ์ˆœ์„œ๋ฅผ ์ž˜ ์กฐ์ ˆํ•ด ์ค˜์•ผ๊ฒ ๋‹ค.

    profile
    ํ•ด๋Š”์„ 

    ๊ธฐ๋ก์„ ๋‚จ๊ธฐ๋ ค๊ณ  ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค

    '๐Ÿ“šSTUDY > ๐Ÿ‘€ coding test๋Œ€๋น„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    ๋Œ“๊ธ€