-
[C++] ๊ฒฝ์ฐ์ ์์ฝ๋ฉํ ์คํธ/์ฝ๋ฉํ ์คํธ ์ฐ์ต 2020. 1. 25. 23:42728x90
1. 1~n๊น์ง ์นด๋๋ฅผ ๋์ดํ๋ ๊ฒฝ์ฐ์ ์
2. 1~n๊น์ง ์นด๋ ์ค ์ค๋ณต๋ ์๊ฐ ์๋ ์นด๋๋ฅผ ๋์ดํ๋ ๊ฒฝ์ฐ ์
1. 1~n๊น์ง ์นด๋ ์ค m๊ฐ์ ์นด๋๋ฅผ ๋์ดํ๋ ๊ฒฝ์ฐ์ ์- swap ์ดํด ์ํ๊ธฐ
#include <iostream> #include <time.h> using namespace std; int arr[7] = { 1,2,3,4,4,6,7 }; int ans[7]; int Cnt = 0; void recursive(int size) { if (size == 7) { for (int i = 0; i < 7; i++) { cout << ans[i]; } cout << endl; Cnt++; return; } for (int i = 0; i < 7; i++) { if (arr[i] == 0) continue; ans[size] = arr[i]; arr[i] = 0; recursive(size+1); arr[i] = ans[size]; } } void swap(int index1, int index2) { int temp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = temp; } // swap์ ํตํ ๋ฐฉ๋ฒ void permutation(int arrSize) { if (arrSize == 1) { for (int i = 0; i < 7; i++) { cout << arr[i]; } cout << endl; Cnt++; return; } for (int i = 0; i < arrSize; i++) { swap(i, arrSize - 1); permutation(arrSize - 1); swap(i, arrSize - 1); } } // ์์๊ฐ ์๊ด์์ ๊ฒฝ์ฐ /*void permutation(int index, int arrSize) { if (arrSize == 1) { for (int i = 0; i < 7; i++) { cout << arr[i]; } cout << endl; Cnt++; return; } for (int i = index; i < arrSize; i++) { swap(i, arrSize - 1); permutation(index, arrSize - 1); swap(i, arrSize - 1); } }*/ int main() { time_t start, end; double result; start = time(NULL); recursive(0); //permutation(7); //swap์ ์ด์ฉํจ cout << "count : " << Cnt << endl; end = time(NULL); result = (double)(end - start); cout << result << endl; return 0; }
728x90'์ฝ๋ฉํ ์คํธ > ์ฝ๋ฉํ ์คํธ ์ฐ์ต' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] ํ๋ ธ์ด์ ํ (0) 2020.03.08 [C++] ์ ์ ์ ๋ ฌํ๊ธฐ (0) 2020.02.06 [C++] ํ๋ฆฐํฐ (0) 2019.09.21 [C++] ๊ฐ์ฅ ํฐ ์ (0) 2019.09.21 [C++] K๋ฒ์งธ์ (0) 2019.09.21