20250708 baekjoon
This commit is contained in:
parent
81296c1bb2
commit
029bff8073
65
code_study/Baekjoon/c/10845.c
Normal file
65
code_study/Baekjoon/c/10845.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int opcodeToNum(char* opcode);
|
||||
|
||||
int main() {
|
||||
int N;
|
||||
scanf("%d\n",&N);
|
||||
int queue[10000];
|
||||
int front = 0, rear = 0, size = 0;
|
||||
|
||||
while(N--){
|
||||
char opcode[6];
|
||||
int operend;
|
||||
scanf("%s",opcode);
|
||||
|
||||
switch(opcodeToNum(opcode))
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
scanf("%d\n",&operend);
|
||||
queue[rear++] = operend;
|
||||
size++;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
printf("%d\n", size ? queue[front++] : -1);
|
||||
size = size ? size - 1 : 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
printf("%d\n",size);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
printf("%d\n", size ? 0 : 1);
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
printf("%d\n", size ? queue[front] : -1);
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
printf("%d\n", size ? queue[rear-1] : -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int opcodeToNum(char* opcode){
|
||||
if(!strcmp(opcode, "push")) return 0;
|
||||
else if(!strcmp(opcode, "pop")) return 1;
|
||||
else if(!strcmp(opcode, "size")) return 2;
|
||||
else if(!strcmp(opcode, "empty")) return 3;
|
||||
else if(!strcmp(opcode, "front")) return 4;
|
||||
else if(!strcmp(opcode, "back")) return 5;
|
||||
return -1;
|
||||
}
|
||||
35
code_study/Baekjoon/c/11866.c
Normal file
35
code_study/Baekjoon/c/11866.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int nextIndex(int current_idx, int* arr, int n, int k);
|
||||
|
||||
int main() {
|
||||
int n, k;
|
||||
scanf("%d %d",&n, &k);
|
||||
int* list = (int*)malloc(sizeof(int)*(n+1));
|
||||
for(int i=0; i<=n; i++) {
|
||||
list[i] = i;
|
||||
}
|
||||
|
||||
printf("<");
|
||||
int len = n, current_idx=1;
|
||||
while(len!=0) {
|
||||
current_idx = nextIndex(current_idx, list, n, len==n ? k-1 : k);
|
||||
if(len==1) printf("%d>\n", list[current_idx]);
|
||||
else printf("%d, ",list[current_idx]);
|
||||
list[current_idx] = 0;
|
||||
len--;
|
||||
}
|
||||
free(list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nextIndex(int current_idx, int* arr, int n, int k){
|
||||
while(k--){
|
||||
do{
|
||||
if(current_idx==n) current_idx = 1;
|
||||
else current_idx++;
|
||||
}while(arr[current_idx]==0);
|
||||
}
|
||||
return current_idx;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user