사자자리

[C언어] 백준 3448: 문자 인식 본문

C언어/C언어 문제

[C언어] 백준 3448: 문자 인식

renne 2022. 8. 21. 22:40

https://www.acmicpc.net/problem/3448

 

3448번: 문자 인식

동혁이는 새로운 이미지 문자 인식 프로그램을 만들었다. 이 프로그램은 종이에 쓰여 있는 글자를 스캔한 뒤, 텍스트 파일로 저장한다. 동혁이는 밤을 새며 열심히 프로그램을 만들었지만, 프로

www.acmicpc.net

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int n;
    char test;
    scanf("%d", &n);

    while(n--){
        int R = 0, A = 0, X, count = 0;

        while(1){
            test = getchar();

            if (test == '\n'){
                count++;
                if (count == 2) break;
                else continue;
            }
            else count = 0;

            A++;
            if (test != '#') R++;
        }

        X = (10000*R/A + 5) / 10;
        printf("Efficiency ratio is %d", X/10);
        X %= 10;
        if (X > 0) printf(".%d", X);
        printf("%%.\n");
    }
}

 

[코드 - 소숫점 계산]

예시) 100 * R/A = 95.56일 때

1. 100 * 100 * R/A = 9556

2. 10000 * R/A + 5 = 9561 (소수점 둘째 자리에서 반올림)

3. X = (10000 * R/A + 5) / 10 = 956

4. X/10 = 95 출력

5. X  = X%10 = 6 (소수점 둘째 자리에서 반올림했을 때 소수점 첫째 자리 수)

6. 소수점 첫째 자리수가 있으면(X > 0) 출력

Comments