[백준] 1110: 더하기 사이클

dBoyeob·2022년 4월 16일
0

Problem-Solving

목록 보기
2/10
post-thumbnail

문제

[Bronze_I] 정답 비율 46%

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

생각

  1. 복잡한 연산 -> 함수로 만들어 코드를 분리하자
  2. (입력받은 수)와 (함수의 리턴 값)이 같을 때까지 반복:
    (입력받은 수)와 (함수의 리턴 값) 비교
    2-1. 다르면 cycle(반복횟수) ++
  3. (입력받은 수)와 (함수의 리턴 값)이 처음부터 같다면? (if (cycle==0) cycle = 1 )

배운점

문제가 쉬워서 크게 배운 점은 없었다. 복잡한 연산은 함수로 만들어 분리하면 가독성에 도움이 된다 정도 ?

코드

#include <stdio.h>

int calculate(int num){
    int new=0;
    if(num<10)
        new = num;
    else
        new = (num/10 + num%10)%10;
        new += (num%10)*10;
    return new;
}

int main(void){
    int cycle = 0;
    int input = 0;
    int temp=0;
    scanf("%d", &input);

    while(input != temp){
        if(cycle == 0)
            temp = calculate(input);
        else
            temp = calculate(temp);
        cycle++;
    } 
 
    if(cycle==0)
        cycle = 1;
     
    printf("%d", cycle);
    return 0;
}
profile
HGU Computer Science 21 && CRA 21-1 👨🏻‍💻

0개의 댓글