반응형
Count Derangements
1.소스
#include <iostream> using namespace std; int countDer(int n) { // Base cases if (n == 1) return 0; if (n == 0) return 1; if (n == 2) return 1; // countDer(n) = (n-1)[countDer(n-1) + der(n-2)] return (n-1)*(countDer(n-1) + countDer(n-2)); } // Driver program int main() { int n = 4; cout << "Count of Derangements is " << countDer(n); return 0; }
결과
hacker@ubuntu:~/c$ hacker@ubuntu:~/c$ hacker@ubuntu:~/c$ hacker@ubuntu:~/c$ g++ -o count_der1 count_der1.cpp hacker@ubuntu:~/c$ hacker@ubuntu:~/c$ hacker@ubuntu:~/c$ ./count_der1 Count of Derangements is 9 hacker@ubuntu:~/c$ hacker@ubuntu:~/c$
반응형
'C++' 카테고리의 다른 글
using for with a string (0) | 2016.07.29 |
---|---|
point to string (0) | 2016.07.29 |
pointer (0) | 2016.07.28 |
hash string (0) | 2016.07.12 |
데이터 비교해서 sink 맞추기 (0) | 2016.07.12 |