Computer Science/C

서울대 프로그래밍연습 2020 기출 문제 풀이 | print, 피보나치 수열, 스택, 등차수열

토마토. 2021. 12. 18. 15:40

 

#include <stdio.h>
#include <stdlib.h>
#define EMPTY 0
#define FULL 10000

struct elem {
    char data;
    struct elem *next;
}; typedef struct elem elem;

struct stack {
    int cnt;
    elem *top;
}; typedef struct stack stack;


void print(void); // 1전
int max_2(void); // 2번
void fib(char, char, int); // 3번
int stack_deal(void); // 4번 push pop
void push_stk(int, stack*);
void print_stk(stack*);


int main(void){
/*
    print();
*/

/*
    printf("%d\n", max());
    printf("%d\n", max());
    printf("%d\n", max());
*/

    /*fib('a', 'b', 0);
    printf("\n");
    fib('a', 'b', 1);
    printf("\n");
    fib('a', 'b', 2);
    printf("\n");
    fib('a', 'b', 3);
    printf("\n"); 
    fib('c', 'd', 5);
    printf("\n");  */  


    stack stk;
    stk.cnt = 0;
    stk.top = NULL;
    for (int a = 0;a<10;a++){
        push_stk(a, &stk);
    }
    print_stk(&stk);
    return 0;
}

void print(void){
    // scan
    char *tmp = (char*)malloc(sizeof(char));
    char *str = tmp;
    char c;
    while ((c=getchar())!='\n'){
        printf("%c", c);
        *tmp = c;
        tmp++;
    }
    printf("\n");
    tmp = str;

    while (1){
        if (*tmp == '\0'){
            break;
        }
        if (*tmp < 'a' && *tmp >= 'A'){
            printf("%c", *tmp + 'a'-'A');
        }
        else if (*tmp >= 'a') {
            printf("%c", *tmp - ('a' - 'A'));
        }
        else {
            printf("%c", *tmp);
        }
        tmp++;
    }
    printf("\n");

    return;

}
int max_2(void){
    // input
    int a[10] = {0};
    int max = 1, tmp = 1;
    for (int i=0;i<10;i++){
        scanf("%d", &a[i]);
    }
 
    // maximum length of consecutively increasing numbers
    for (int j=0;j<9;j++){
        if (a[j+1]== a[j]+1){
            tmp++;
        }
        else {
            if (tmp>max){
                max = tmp;
            }
            tmp = 1;
            continue;
        }
    }
    return max;
}

void fib(char a, char b, int n){
    if (n==0) {
        printf("%c", a);
        return;
    }
    else if (n==1){
        printf("%c", b);
        return;
    }
    else {
        fib(a, b, n-2);
        fib(a, b, n-1);
        return;
    }

}
int stack_deal(void){
    
    // initialize

    // push three elements
    //push_stk(a);
    // deallocate
}

void push_stk(int a, stack *stk){
    elem *new = malloc(sizeof(elem));
    new->next = stk->top;
    new->data = a;
    stk->top = new;
    stk->cnt++;

}

void print_stk(stack *stk){
    elem *tmp = malloc(sizeof(elem));
    tmp = stk->top;

    while (tmp != NULL){
        printf("hi : %d\n", tmp->data);
        tmp = tmp->next;
    }

}