-
๋ฌธ์
์์ธํธ์ค ๋ฌธ์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
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์ ์์๋ฅผ ์ ์กฐ์ ํด ์ค์ผ๊ฒ ๋ค.
ํด๋์๊ธฐ๋ก์ ๋จ๊ธฐ๋ ค๊ณ ๋ ธ๋ ฅํฉ๋๋ค
'๐STUDY > ๐ coding test๋๋น' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 2. Add Two Numbers (0) 2022.01.15 [LeetCode] 59. Spiral Matrix II (0) 2022.01.15 [LeetCode] 328. Odd Even Linked List (0) 2022.01.15