Last active
August 29, 2015 14:01
-
-
Save cuber/71a551fca8bdd7b0a729 to your computer and use it in GitHub Desktop.
矩阵法
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
<?php | |
$e = array( | |
array('a1', 'a2'), | |
array('b1', 'b2', 'b3', 'b4'), | |
array('c1', 'c2'), | |
); | |
$N = count($e); | |
$r = array(); | |
$n = array_fill(0, $N, 1); | |
for ($i = 1; $i < $N; $i++) | |
for ($j = $i; $j < $N; $j++) { | |
$cnt = count($e[$j]); | |
$n[$i - 1] *= $cnt; | |
} | |
$h = $n[0] * count($e[0]); | |
for ($i = 0; $i < $h; $i++) | |
for ($j = 0; $j < $N; $j++) { | |
$m = intval($i / $n[$j]) % (count($e[$j])); | |
$r[$i][$j] = $e[$j][$m]; | |
} | |
echo "n:", PHP_EOL, implode(" -> ", $n), PHP_EOL; | |
echo "res:", PHP_EOL; | |
foreach ($r as $v) echo implode(" -> ", $v), PHP_EOL; | |
/* | |
output: | |
-------------- | |
n: | |
8 -> 2 -> 1 | |
-------------- | |
res: | |
a1 -> b1 -> c1 | |
a1 -> b1 -> c2 | |
a1 -> b2 -> c1 | |
a1 -> b2 -> c2 | |
a1 -> b3 -> c1 | |
a1 -> b3 -> c2 | |
a1 -> b4 -> c1 | |
a1 -> b4 -> c2 | |
a2 -> b1 -> c1 | |
a2 -> b1 -> c2 | |
a2 -> b2 -> c1 | |
a2 -> b2 -> c2 | |
a2 -> b3 -> c1 | |
a2 -> b3 -> c2 | |
a2 -> b4 -> c1 | |
a2 -> b4 -> c2 | |
-------------- | |
*/ |
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 <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdint.h> | |
#include <iostream> | |
#include <string> | |
#include <map> | |
#include <vector> | |
using namespace std; | |
static const char | |
*a[] = {"a1", "a2"}, | |
*b[] = {"b1", "b2", "b3", "b4"}, | |
*c[] = {"c1", "c2"}; | |
static const struct { | |
const char **s; int n; | |
} e[] = { | |
{ a, sizeof(a) / sizeof(char *) }, | |
{ b, sizeof(b) / sizeof(char *) }, | |
{ c, sizeof(c) / sizeof(char *) } | |
}; | |
static const int N = 3; | |
static int h = 1; | |
static vector<int> n(N, 1); | |
static void init(vector<vector<string> > & v) | |
{ | |
for (int i = 0; i < N; i++) { | |
int n = e[i].n; h *= n; | |
const char ** s = e[i].s; | |
v.push_back(vector<string>(s, s + n)); | |
if (i) for (int j = i; j < N; j++) ::n[i - 1] *= e[j].n; | |
} | |
} | |
int main() | |
{ | |
vector <vector<string> > v; init(v); | |
vector <vector<string> > r(h, vector<string>(N, "")); | |
for (int i = 0; i < h; i++) | |
for (int j = 0; j < N; j++) r[i][j] = e[j].s[i / n[j] % e[j].n]; | |
for (size_t i = 0; i < r.size(); i++) { | |
for (size_t j = 0; j < r[i].size(); j++) { | |
if (j) printf(" -> "); | |
printf("%s", r[i][j].c_str()); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
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 <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdint.h> | |
#include <string.h> | |
static const char | |
*a[] = {"a1", "a2"}, | |
*b[] = {"b1", "b2", "b3", "b4"}, | |
*c[] = {"c1", "c2"}; | |
typedef struct { | |
const char **s; int n; | |
} vector; | |
static vector | |
e[] = { | |
{ a, sizeof(a) / sizeof(char *) }, | |
{ b, sizeof(b) / sizeof(char *) }, | |
{ c, sizeof(c) / sizeof(char *) } | |
}, | |
*r = NULL; | |
static const int N = sizeof(e) / sizeof(vector); | |
static int h = 1, n[N]; | |
static void init(void) | |
{ | |
for (int i = 0; i < N; i++) { | |
n[i] = 1; if (i) for (int j = i; j < N; j++) n[i - 1] *= e[j].n; | |
} | |
h = n[0] * e[0].n; | |
r = malloc(sizeof(vector) * h); | |
for (int i = 0; i < h; i++) { | |
r[i].n = N; | |
r[i].s = malloc(sizeof(char *) * N); | |
} | |
} | |
static void destroy(void) | |
{ | |
for (int i = 0; i < h; i++) free(r[i].s); | |
free(r); r = NULL; | |
} | |
int main() | |
{ | |
init(); | |
for (int i = 0; i < h; i++) | |
for (int j = 0; j < N; j++) r[i].s[j] = e[j].s[i / n[j] % e[j].n]; | |
for (size_t i = 0; i < h; i++) { | |
for (size_t j = 0; j < N; j++) { | |
if (j) printf(" -> "); | |
printf("%s", r[i].s[j]); | |
} | |
printf("\n"); | |
} | |
destroy(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment