본문 바로가기

Daily/TIL

20240303 정처기 공부 c언어 기출

20년1회 14번 출력결과

#include <stdio.h>

main() {
  int c=1;
  switch(3){
    case 1:c+=3;
    case 2:c++;
    case 3:c=0;
    case 4:c+=3;
    case 5:c-=10;
    default : c--;

  }
printf("%d",c);
  }

답 : -8

풀이: break 이 없어서 case 3 부터 시작해서 c=0, c+=3, c-=10, c-- 순으로 실행

20년 1회 20번 출력결과

 #include <stdio.h>
 void main(){
     int i,j;
    int temp;
    int a[5] = {75,95,85,100,50};

    for(i=0; i<4; i++){
        for(j=0; j<4-i; j++){
            if(a[j] > a[j+1]){
                temp=a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
             }
           }
        }

           for(i=0; i<5; i++){
            printf("%d", a[i]);
        }
  }

답: 50758595100

풀이: 배열을 오름차순으로 정렬하는 로직이다. 그래서 a = [50, 75, 85, 95, 100]

20년 3회 10번 출력 결과

#include <stdio.h>
int r1(){
  return 4;
}
int r10(){
  return(30+r1());
}
int r100(){
  return(200+r10());
}
int main(){
  printf("%d\n",r100());
}

답: 234

풀이: 4+30+200

\n는 개행

20년 4회 10번 출력 결과

#include <stdio.h>
 
 main() {
   char *p="KOREA";
   printf("%s\n",p);
   printf("%s\n",p+3);
   printf("%c\n",*p);
   printf("%c\n",*(p+3));
   printf("%c\n",*p+2);
   }

답: KOREA

EA

K

E

M

풀이: 

char형과 char*형 차이

char형은 문자로 하나의 문자를 담을 수 있다. 다만 char a[10];과 같이 배열로 선언할 수 있다.

char*형은 포인터를 통해 문자을 가리킬 수 있다.

 

char*형은 예를 들어 char* a = "Hello World"라고 선언하는 경우, 메모리에 저장된 Hello World의 첫 'H'가 담긴 첫 주소값을 가리키고 있게 된다.

 

%s와 %c의 차이

%s: char* 형을 필요로 한다. 즉, 문자열 포인터가 입력으로 들어가야 한다. 출력할 때는 지정된 주소부터 \0(NULL)이 나올 때까지 출력한다.

%c: char형을 필요로 한다.

21년 1회 15번 출력 결과

#include <stdio.h>

struct good {
	char name[10];
    int age;
 };
 
 void main(){
 	struct good s[] = {"Kim",28,"Lee",38,"Seo",50,"Park",35};
    
    struct good *p;
    p = s;
    p++;
    printf("%s\n", p-> name);
    printf("%d\n", p-> age);

답: Lee

38

풀이: p++로 인해 s[1]가 되면서 두번째 배열인 Lee와 38이 출력된다.

21년 2회 16번 출력 결과

	#include <stdio.h>
int main(){
   int res;
   res = mp(2,10);
   printf("%d",res);
   return 0;
}
 
int mp(int base, int exp) {
   int res = 1;
   for(int i=0; i < exp; i++){
      res *= base;
   }
   
   return res;
}

답: 1024

풀이: 2의 10제곱

21년 2회 18번 출력 결과

#include <stdio.h>
int main(){
 
int ary[3];
int s = 0;
*(ary+0)=1;
ary[1] = *(ary+0)+2;
ary[2] = *ary+3;
for(int i=0; i<3; i++){
  s=s+ary[i];
}
 
printf("%d",s);
 
}

 

답:8

풀이:  ary[0]=1, ary[1]=ary[0]+2=3, ary[2] =4

'Daily > TIL' 카테고리의 다른 글

20240317 정처기 SQL 기출 공부  (0) 2024.03.17
20240304 정처기 c언어 기출 공부  (0) 2024.03.04
20221114 자바의 특징  (0) 2022.11.15
20221014 HTTP와 WEBSOCKET  (0) 2022.10.14
20220608  (0) 2022.06.08