2015. 4. 27. 23:34 Programing/C++

함수 Template

 template <typename T [ parameters ]> [return type] Function ( T a [ arguments ] ) { }


template 키워드와 함께 <> 안에 템플릿 파라미터 목록을 적게된다.

템플릿 파라미터로는 'typename T'와 같은 데이터 타입이 올 수도 있지만, int N처럼 데이터 타입이 아닌 파라미터나 'template class Unit'과 같은 템플릿 템플릿 파라미터가 올 수도 있다.


다음은 큰 값을 리턴하는 템플릿의 예제이다.


#include <iostream> using namespace std;    

template <typename T>      T max(T const& a, T const& b)      {        return a < b ? b : a;      } int main() { cout<< max(3, 4) << endl; }



'Programing > C++' 카테고리의 다른 글

구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
이차원 동적 배열 입력 받아 생성하기  (0) 2015.02.10
Tree 구현하기  (0) 2015.02.10
C++ 문자형을 int형으로  (0) 2015.02.10
Posted by kimmayer

//

//  main.c

//  project euler

//

//  Created by 김일호 on 2015. 4. 25..

//  Copyright (c) 2015 김일호. All rights reserved.

//


#include <stdio.h>


int main(int argc, const char * argv[]) {

    int product = 0;

    

    for(int i =1; i<1000; i++)

        for (int j = 1; j<1000; j++){

            for (int k = 1; k<1000; k ++)

            {

                if( j < i && k < j)

                    break;

                if((i * i) + (j * j) == (k*k))

                {

                    product = i+j+k;

                    if(product == 1000)

                    {

                        printf("%d %d %d",i, j, k);

                        return 0;

                    }

                }

                else if (k+j>1000)

                {product = 0;break;}

                else if (k+i>1000)

                {product = 0;break;}

                product = 0;

            }

            if(i+j>1000){

                product = 0;

               break;

            }

        }

    return 0;

}



'Programing > Algorithm' 카테고리의 다른 글

Selection Sort  (0) 2015.09.27
프로젝트 오일러 10번  (0) 2015.07.29
Recursive Fibonacci  (0) 2015.04.06
순환/재귀 팩토리알 구현  (0) 2015.04.06
3개의 숫자를 내림차순으로 정렬  (0) 2015.04.06
Posted by kimmayer

#include <stdio.h>


int fibonacci(int n);

int main()

{

    int number;

    int total = 0;

    printf("input the number\n");

    scanf("%d", &number);

    printf("recursive fibonacci = %d \n", fibonacci(number));

}


int fibonacci(int n){

    if (n < 3)

        return 1;

    else return fibonacci(n-2) + fibonacci(n-1);

}


/*C++ 피보나치 합 구하기*/

#include <iostream>

using namespace std;


int fibonacci(int n);


int main()

{

int total = 0;

int n = 0;

for (int i = 0; i <= 5; i++)

{

n = fibonacci(i);

total += n;

}

cout << total << endl;

}


int fibonacci(int n)

{

if (n == 1 || n == 2)

return 1;

else if (n == 0)

return 0;

else

return fibonacci(n - 1) + fibonacci(n - 2);

}

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 10번  (0) 2015.07.29
프로젝트 오일러 9번  (0) 2015.04.25
순환/재귀 팩토리알 구현  (0) 2015.04.06
3개의 숫자를 내림차순으로 정렬  (0) 2015.04.06
프로젝트 오일러 7번  (0) 2015.02.10
Posted by kimmayer

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.4
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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함