본문 바로가기
PROGRAMMING CODE/C

[BAEKJOON] No.8958 C언어

by daye_ 2021. 10. 18.

OX퀴즈

문자열 배열도 동적할당해야하는줄알고 사경을 헤매다가 다시생각해보니 필요없어서 호딱 해버림

사실 score동적할당도 필요없이 바로 출력해도 되는데 그냥 해놓음

 

 

#include <stdio.h>
#include <stdlib.h> //malloc을 위한 함수
#include <string.h> //strlen(길이)를 위한 함수

int main(){

    char arr[80]={0,}; //문자열 받아오는 배열
    int n,plus=0,*score; // n(입력할 문자열의 수), plus(O가 연달아 나오는 만큼 점수를 저장), score(총 점수 저장)
    
    scanf("%d",&n);
    score = (int*)malloc(sizeof(int)*n); //총 점수 배열 동적할당

    for(int i=0; i<n; i++){
        scanf("%s",arr); //문자열 입력

        for(int j=0; j<strlen(arr); j++){
            if(arr[j]=='O'){ //'O'일때 plus를 1씩 늘려가며 score에 저장
                plus++;
                score[i]+=plus;
            }
            else plus=0; //'O'가 아닌 'X'가 있으면 plus초기화
        }

        plus=0; //이전 문자열이 'X'로 끝나지 않는 경우를 위해 초기화
    }

    for(int i=0; i<n; i++){ // 점수 출력
        printf("%d\n",score[i]);
    }

    free(score);
    return 0;
}

'PROGRAMMING CODE > C' 카테고리의 다른 글

[BAEKJOON] No.11720 C언어  (0) 2021.10.18
[BAEKJOON] No.4344 C언어  (0) 2021.10.18
[BAEKJOON] No.3052 C언어  (0) 2021.10.18
[BAEKJOON] No.2577 C언어  (0) 2021.10.18
[BAEKJOON] No.15596 C언어  (0) 2021.10.18