앞의 예에서 get_cycle_number(int n) 함수를 순환을 이용하여 작성하고 main함수로 테스트하라.
get_cycle_number 함수 안에서 지역변수를 사용하지 말라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int get_cycle_number(int n) {
if (n == 1) {
printf("%d ", n);
return 1;
}
else if (n % 2 == 0) {
printf("%d ", n);
return 1 + get_cycle_number(n / 2);
}
else {
printf("%d ", n);
return 1 + get_cycle_number(n * 3 + 1);
}
}
int main()
{
int num;
printf("숫자를 입력하세요:");
scanf("%d", &num);
printf("\n%d", get_cycle_number(num));
}
|
cs |
전역변수 x static x
'대학교 2-1 > 문해기' 카테고리의 다른 글
카카오 입사시험_문자열 압축: 문제 1-1 (0) | 2024.05.16 |
---|---|
LAB(함수의 값 출력) (0) | 2024.03.17 |
Lab(조합출력) (0) | 2024.03.17 |
Warming Up Exercise 8 (0) | 2024.03.10 |
Warming Up Exercise 6 (0) | 2024.03.10 |