본문 바로가기

프로그래밍/C, C++21

[C언어] 팩토리얼을 반환하는 함수 만들기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #define _CRT_SECURE_NO_WARNINGS #include int factorial(int n); int main(void) { int n; scanf("%d", &n); printf("%d ", factorial(n)); return 0; } int factorial(int n) { if (n == 1) { return 1; } return n * factorial(n - 1); } cs 설명이 필요 없을 정도로 간단한 코드이다. n은 1까지 재귀적으로 호출 된 후, n*n-1 을 하면서 return 된다. 2021. 4. 6.
[알고리즘] Merge 정렬 #include void mergeSort(int* arr, int left, int right); void merge(int* arr, int left, int mid, int right); int main(void) { int arr[10] = { 1, 3, 5, 9, 7, 8, 2, 4, 6, 0 }; for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); } printf("\n"); mergeSort(arr, 0, 9); for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } void mergeSort(int* arr, int left, int right) { if .. 2021. 4. 2.
[C언어] Lable을 선언 후 goto label로 반복문 없이! 예제 : codeup.kr/problem.php?id=1079 코드 : #include int main(void) { char a; test: scanf(" %c", &a); printf("%c\n", a); if (a != 'q') { goto test; } return 0; } 이런 방식이 아니면, Flag로도 할 수 있다. 생각보다 저런 형식이 필요할 때가 있었던것 같다. 2021. 4. 1.
[C언어] %lf, %lld를 사용해서 큰 숫자를 입력, 출력하는 경우 예제 : codeup.kr/problem.php?id=1029 코드 : #include int main(void) { double a; scanf("%lf", &a); printf("%.11lf", a); return 0; } double 형은 %lf로 받은 후 %lf로 출력하면 된다. 예제 : codeup.kr/problem.php?id=1030 코드 : #include int main(void) { long long a; scanf("%lld", &a); printf("%lld", a); return 0; } long long int 형 같은 경우는 %lld로 입력, 출력을 하면 된다. 2021. 4. 1.
[C언어] 실수(소수)를 정수부분, 소수점 부분으로 쪼개서 정수형 2개로 받아보자 hyun222.tistory.com/106 [C언어] 날짜, 연도 형식으로 입력받고 출력하기 예제 : codeup.kr/problem.php?id=1019 솔루션 : #include int main(void) { int a, b, c; scanf("%d.%d.%d", &a, &b, &c); printf("%04d.%02d.%02d", a, b, c); return 0; } 입력 받을 때는 . 으로 정수를 구분해.. hyun222.tistory.com 이걸 살짝쿵 응용한 꼼수라고 보면 된다. 코드를 보자. #include int main(void) { int a, b; scanf("%d.%d", &a, &b); printf("%d.%d", a, b); return 0; } 분명히 3.123 같은 실수를 입.. 2021. 4. 1.
[C언어] fgets()를 이용해서 여러줄 문장을 입력받자 예제 : codeup.kr/problem.php?id=1022 해결방안 : 1 2 3 4 5 6 7 8 9 #include int main(void) { char a[2001]; fgets(a, 2000, stdin); printf("%s", a); return 0; } cs fgets는 무엇이며 어떻게 사용하는 것일까? 우선 C언어에서 문자열을 입력 받으려면 조금 짜증나는것을 알 수 있다. 아래 예시를 잠깐 살펴보자. 1 2 3 4 5 6 7 8 9 10 11 #include int main(void) { char a[50]; scanf("%s", a); printf("%s", a); return 0; } cs Hello World! 를 입력하고 엔터를 쳐보면, Hello 만 받는걸 알 수 있다. 그 .. 2021. 4. 1.