Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 게임만들기
- 그림자 효과
- 렌더몽키
- 화살표 메서드
- 화살피하기
- Unity
- swipe
- 파이썬
- visualstudio2022
- 이득우언리얼
- 셰이더
- 공부
- rendermonkey
- IMGUI
- 유니티
- premake5
- 화살표 함수
- uidesign
- 이득우
- 표창던지기
- 다중상속
- 가변배열
- python
- C++
- c++class
- 비주얼스튜디오
- 3차원배열
- 언리얼
- c#
- 배열문제
Archives
- Today
- Total
신입 개발자 공부 과정
c# 재귀함수 본문
재귀함수=
함수에서 함수 자신을 호출하는 것을 재귀(recursion)이라고 한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(4*3*2*1);
System.Console.WriteLine(FactorialFor(4));
System.Console.WriteLine(Factorial(4));
System.Console.WriteLine(Fact(4));
}
//3항 연산자를 사용한 팩토리얼구하기
static int Fact(int n)
{
return (n>1) ? n * Fact(n-1) : 1; //해당 식이 True이면 n * Fact(n-1) False이면 1
}
//재귀 함수를 사용한 팩토리얼 함수 만들기: 재귀 함수는 트리 구조 탐색에 유리
static int Factorial(int n)
{
//종료
if (n == 0 || n == 1)//n이 0 또는 1이면 return 1
{
return 1;
}
return n * Factorial(n-1);//재귀 호출
}
//단순한 팩토리얼
static int FactorialFor(int n)
{
int result =1;
for (int i = 1; i <= n; i++)
{
result *= i; //((((1*1)*2)*3)*4)
}
return result;
}
}
}
'C# > 모르는 내용들 공부' 카테고리의 다른 글
c# Struct 구조체 (0) | 2022.01.13 |
---|---|
c# 화살표 함수 => (0) | 2022.01.13 |
C# Callback ,Json 공부 (0) | 2022.01.05 |
1월3~4일 추가로 공부한 것 (0) | 2022.01.04 |
1월1일~1월2일 C# 공부한 부분들 (0) | 2021.12.29 |