'전체 글'에 해당되는 글 276건

  1. 2020.12.08 [영리한 프로그래밍을 위한 알고리즘 강좌] - 순환(Recursion)의 개념과 기본 예제 1
  2. 2020.11.09 [Docker] Solved Error : Cannot enable Hyper-V service
  3. 2020.04.02 백준 2941 크로아티아 알파벳

[영리한 프로그래밍을 위한 알고리즘 강좌] - 순환(Recursion)의 개념과 기본 예제 1

Online Learning 2020. 12. 8. 18:46

순환(Recursion), 재귀 함수 또는 메서드

 

자기 자신을 다시 호출하는 함수 또는 메서드를 Recursion이라고 한다.

 

종료 조건을 넣지 않는다면 계속 실행하므로 무한루프에 빠지게 된다.

 

github.com/VontineDev/OnlineLearning_Algorithm/tree/main/S02_Recursion

 

VontineDev/OnlineLearning_Algorithm

영리한 프로그래밍을 위한 알고리즘 강좌 예제연습. Contribute to VontineDev/OnlineLearning_Algorithm development by creating an account on GitHub.

github.com

01. n까지의 합을 계산하는 Recursion

#include <iostream>

using namespace std;
//func(int n)은 음이 아닌 정수  n에 대해서 0에서 n까지의 합을 계산
static int func(int n)
{    
    if(n==0)
        {
            return 0;
        }
    else
        {
            return n + func(n-1);
        }    
}

int main()
{
    int n = func(10);  
    cout << n << endl;
    return 0;
}

 

 

02. n!을 계산하는 Recursion

#include <iostream>

using namespace std;

//factorial(int n)은 Factorial을 계산
static int factorial(int n)
{    
    if(n==0)
        {
            return 1;
        }
    else
        {
            return n * factorial(n-1);
        }    
}

int main()
{
    int n = factorial(4);
    cout << n << endl;
    return 0;
}

 

03. 피보나치 수열을 계산하는 Recursion

#include <iostream>

using namespace std;

//fibonacci(int n)은 피보나치 수열을 계산
static int fibonacci(int n)
{    
    if(n<2)
        {
            return n;
        }
    else
        {
            return fibonacci(n-1) + fibonacci(n-2);
        }    
}

int main()
{
    int n = fibonacci(4);
    cout << n << endl;
    return 0;
}

 

04. 최대공약수 gcd를 구하는 Recursion

#include <iostream>

using namespace std;

//gcd(int n)은 피보나치 수열을 계산
static int gcd(int m, int n)
{    
    if(m<n)
        {
            int tmp=m;
            m=n;
            n=tmp;
            //m<n 인 경우 두 수를 바꿈
        }
    
    if(m%n==0)
        {
            return n;
        }
        else
        {
            return gcd(m, m%n);
        }
}

static int gcd_simple(int p, int q)
{
    if(q==0)
        {
            return p;
        }
    else
        {
            return gcd_simple(q, p%q);
        }
}

int main()
{
    int n = gcd(3,12);
    cout << "gcd " << n << endl;
    int k = gcd_simple(3,12);
    cout << "gcd_simple " << k << endl;
    return 0;
}

 

:

[Docker] Solved Error : Cannot enable Hyper-V service

WEB 2020. 11. 9. 21:56

Docker 설치 후 실행시 발생한 에러이다.

 

이 에러를 검색해보니

 

작업표시줄의 돋보기를 누르고 검색하면 나오는 Windows 기능 켜기/끄기에서 Hyper-V 항목이 켜져 있는지 확인하고

 

꺼져있다면 켜고,  Hyper-V항목 자체가 없다면 설치하면 된다고 해서 따라해 보았지만 같은 에러가 발생했다.

 

Hyper-V 설치법

docs.microsoft.com/ko-kr/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v

 

다른 블로그

blog.gaerae.com/2019/04/hyper-v-troubleshooting.html

에서 찾아봤던 시스템 요구사항에서 Hyper-V 요구 사항 란의 펌웨어에 가상화 사용이라는 부분이 아니오로 되어있던게 문제가 되었다고 인식하게 되었다.

 

내가 쓰는 메인보드의 바이오스에 진입하고 AMD프로세서를 사용중이므로 가상화가 어디에 있나 잘 찾아보니

고급설정에서 CPU Configuration의 SVM이 가상화에 대한 항목이었다. Enable로 바꾸고 나니 에러가 해결되었다.

 

'WEB' 카테고리의 다른 글

[Error] Express app crashed  (0) 2023.05.09
[Laravel] Laravel + Bitnami wamp 7.3.29  (0) 2021.08.06
:

백준 2941 크로아티아 알파벳

Console Programming/C# Console 2020. 4. 2. 17:15

문자열을 돌면서 하나의 알파벳만을 검색할 것인지, 여러 알파벳을 다 검색해볼 것인지 생각하다가 하나만 하는게 쉬울것 같아서 이렇게 코드를 작성했다. 그리고 여러 알파벳을 검색하는 것은 하나의 알파벳을 검색하는 것을 여러번 실행하는 것으로 대체했다. 알파벳을 검색하면 검색된 이후 인덱스부터 다시 검색을 이어나가는 부분을 구현하기 위해   index = str.IndexOf(alpha, index) + 2; 이렇게 만들었다.

 

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _2941
{
    class Program
    {
        static void Main(string[] args)
        {
            var str = Console.ReadLine();
 
            int len = str.Length;
 
            int sum = 0;
 
            string[] alpha = { "c=""c-""dz=""d-""lj""nj""s=""z=" };
 
            for (int i = 0; i < alpha.Length; i++)
            {
                sum += FindCroatiaAlphabet(str, alpha[i]);
            }
 
 
            Console.WriteLine(len - sum);
        }
        static int FindCroatiaAlphabet(string str, string alpha)
        {
            int index = 0;
            int count = 0;
            while (index < str.Length - 1)
            {
                if (str.IndexOf(alpha, index) != -1)
                {
                    index = str.IndexOf(alpha, index) + 2;
                    count++;
                }
                else
                {
                    break;
                }
 
            }
            return count;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

'Console Programming > C# Console' 카테고리의 다른 글

백준 2908 상수  (0) 2020.04.02
백준 1152 단어의 개수  (0) 2020.04.02
백준 10872 팩토리얼  (0) 2019.12.20
4948 베르트랑 공준  (0) 2019.12.04
1929 소수 구하기 에라토스테네스의 체  (0) 2019.12.04
: