Last active
August 29, 2015 13:56
-
-
Save rozag/9305260 to your computer and use it in GitHub Desktop.
Написать рекурсивную программу, которая печатает по одному разу все k^n последовательностей длины n, составленные из чисел 1...k.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void generate(int n, int k, int p) { | |
static int a[1000000]; | |
static int tmp = 1; | |
if (p == n) { | |
if (tmp == 1) { | |
for (int i = 0; i < n; i++) { | |
cout << a[i] + 1 << " "; | |
} | |
cout << endl; | |
} | |
if (tmp < k) { | |
tmp++; | |
} else { | |
tmp = 1; | |
} | |
} else { | |
for (int i = 0; i < k; i++) { | |
p++; | |
a[p] = i; | |
generate(n, k, p); | |
p--; | |
} | |
} | |
} | |
int main (int argc, char *argv[]) { | |
int n, k; | |
cin >> n >> k; | |
generate(n, k, -1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment