<24년 3회 6번>

#include <stdio.h>
int func(){
    static int x = 0;
    x += 2;
    return x;
}

int main() {
    int x = 0;
    int sum = o;
    for(int i = 0; i < 4; i++){
    	x++;
        sum += func();
    }
    printf("%d", sum);
    return 0;
}

정답: 20

 

static 변수는 누적될 수 있습니다. func() 함수의 x는 0, 2, 4, 6, 8이 되고, sum은 0, 2, 6, 12, 20이 됩니다. (main() 함수의 x는 신경쓰지 말자)

 

 

 

<24년 3회 10번>

#include <stdio.h>
struct Node{
    int value;
    struct Node *next;
};

void func(struct Node* node){
    while(node != NULL && node -> next != NULL){
        int t = node -> value;
        node -> value = node -> next -> value;
        node -> next -> value = t;
        node = node -> next -> next;
    }
}

int main(){
    struct Node n1 = {1, NULL};
    struct Node n2 = {2, NULL};
    struct Node n3 = {3, NULL};
    n1.next = &n3;
    n3.next = &n2;
    func(&n1);
    struct Node* current = &n1;
    while(current != NULL){
        printf("%d", current -> value);
        current = current -> next;
    }
    return 0;
}

정답: 312

 

Node라는 구조체를 정의했습니다. 여기서 각 노드는 정수값 value와 다음 노드를 가리키는 포인터 *next를 가지고 있습니다.

func() 함수는 두 개씩 짝지어서 노드들의 value 값을 서로 교환합니다. (첫번째와 두번째, 세번째와 네번째..)

main() 함수에 next가 NULL인 노드 3개가 정의되어 있습니다. 연결 순서가 n1->n3->n2 이므로 연결 리스트는 1->3->2입니다.

func()를 호출 후 node = &n1이므로 n1.value = 2, n3.value = 1이 되고, 다음 노드는 n2이지만 그 다음 n2->next가 없으므로 종료하게 됩니다. 따라서 최종 구조는 3->1->2

 

 

<24년 3회 19번>

#include <stdio.h>
void func(int**arr, int size){
    for(int i = 0; i<size; i++){
    	*(arr+i) = (*(*arr+i)+i)%size;
    }
}

int main(){
    int arr[] = {3, 1, 4, 1, 5}
    int *p = arr;
    int**pp = &p;
    int num = 6;
    func(pp, 5);
    num = arr[2];
    printf("%d", num);
    return 0;
}

정답: 1

 

*(arr+i) = (*(*arr+i)+i)%size 는 arr[i] = (arr[i] + i)%size 와 동일합니다. 따라서 최종 배열은 {3, 2, 1, 4, 4}

 

 

<24년 2회 5번>

#include<stdio.h>
void swap(int a,int b){
    int t= a;
    a = b;
    b = t;
}

int main(){
    int a = 11;
    int b = 19;
    swap(a, b);
    switch(a){
        case 1:
            b += 1;
        case 11:
            b += 2;
        default:
            b += 3
            break;
    }
    printf("%d", a-b);
}

정답: -13

 

쉽게 생각했다간 함정에 빠질 수 있는 문제입니다. swap() 함수를 호출할 때 a, b 변수의 주소를 전달한 것이 아니므로 main() 함수에서의 a, b 변수는 영향을 받지 않습니다.

break문을 만날 때까지 모든 문장이 실행되므로, case 11에서 b=19+2=21, default에서 b=21+3=24이고 break를 만나 멈춥니다.

 

 

<24년 2회 6번>

void func(char *d, char *s){
    while(*s){
        *d = *s;
            d++;
            s++;
    }
    *d='\0';
}

int main(){
    char* str1 = "first";
    char str2[50] = "teststring";
    int result = 0;
    func(str2, str1);
    for(int i = 0; str2[i] != '\0'; i++){
        result += i;
    }
    printf('%d\n", result);
    return 0;
}

정답: 10

 

str2의 내용이 str1의 first로 덮어씌워지고, 따라서 ( f i r s t \0 ) 입니다. 총 5개의 문자이므로 0+1+2+3+4 = 10

 

 

<24년 2회 19번>

int main(){
    int arr[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    int *parr[2] = {arr[1], arr[2]};
    printf("%d", parr[1][1] + *(parr[1]+2) + **parr);
}

정답: 21

 

*(parr[1]+2)는 parr[1] 에 주소 2를 더한 값인 세 번째 열이므로 9, *parr = parr[0]이고 **parr = parr[0][0]이므로 4 입니다.

 

 

+ Recent posts