// () - () = 1
// () - () = 2
// () + () = 7
// () + () = 9
// () = 1, 2, 3, 4, 5, 6, 7, 8
//试图解决上面的问题,但是没有好的算法只好自己编了一个,这个算法真的能行吗?
//好幼稚的想法,好糟糕的算法,好讨厌的脑筋急转弯——网上查了一下居然是这样。
// () = 1, 2, 3, 4, 5, 7, 8, 9 把6翻转过来就行了。
#include <iostream>
#include <stdio.h>
using namespace std;
bool my1(const int *y, char *c);
bool my2(const int *y, char *c);
bool my7(const int *y, char *c);
bool my9(const int *y, char *c);
int cmp_c(char *cc);
int create_table(unsigned short *c0, unsigned short *cc, unsigned short *c1, unsigned short *c2, unsigned short *c7, unsigned short *c9);
int c_e_t(unsigned int *end_table, unsigned int *cc, unsigned int *c0);
int main(void) {
const int x[8] = {1, 2, 3, 4, 5, 6, 7, 8};
// const int x[8] = {1, 2, 3, 4, 5, 7, 8, 9}; 这样的组合形式与上面的不同,所以要改循环次数。
char c1[16] = {'1', '2', '3', '4', '\0'};
char c2[16] = {'A', 'B', 'C', 'D', 'E', 'F', '\0'};
char c7[16] = {'7', '8', '\0'};
char c9[16] = {'7', '\0'};
char c0[200];
char cc[200];
char end_table[7 * 6 * 4 * 3 * 8 + 4];
int i = 0;
// printf("%d\n", cmp_c(c1));
my1(x, c1);
my2(x, c2);
my7(x, c7);
my9(x, c9);
printf("%s\n", c1);
printf("%s\n", c2);
printf("%s\n", c7);
printf("%s\n", c9);
// printf("######################################\n");
create_table((unsigned short *)c0, (unsigned short *)cc, (unsigned short *)c1, (unsigned short *)c2, (unsigned short *)c7, (unsigned short *)c9);
// printf("######################################\n");
// printf("%s\n", cc);
// printf("######################################\n");
// printf("%s\n", c0);
c_e_t((unsigned int *)end_table, (unsigned int *)cc, (unsigned int *)c0);
// printf("%s\n", end_table);
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
for(i = 0; i < 7 * 6 * 4 * 3 * 8; i += 8) {
// printf("\\%d|%d|", i / 8 + 1, cmp_c(end_table + i));
if(cmp_c(end_table + i) == -2) {
printf("True!... %d", i);
break;
}
}
if(i == 7 * 6 * 4 * 3 * 8)
printf("Not found answer!!!\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
bool my7(const int *y, char *c) {
int i, j, tmp, k = 0;
i = 0, j = 1;
while(i <= 7) {
tmp = j;
while(j <= 7) {
if(y[i] + y[j] == 7) {
// printf("(%d) + (%d) = %d\n", y[i], y[j], y[i] + y[j]);
c[k] = (char)(y[i] + '0');
c[k + 1] = (char)(y[j] + '0');
k += 2;
}
j++;
}
j = ++tmp;
i++;
}
c[k] = '\0';
// printf("\n");
return 1;
}
bool my1(const int *y, char *c){
int i, j, tmp, k = 0;
i = 7; j = 6;
while(i >= 0) {
tmp = j;
while(j >= 0) {
if(y[i] - y[j] == 1) {
// printf("(%d) - (%d) = %d\n", y[i], y[j], y[i] - y[j]);
c[k] = (char)(y[i] + '0');
c[k + 1] = (char)(y[j] + '0');
k += 2;
}
j--;
}
j = --tmp;
i--;
}
c[k] = '\0';
// printf("\n");
return 1;
}
bool my2(const int *y, char *c){
int i, j, tmp, k = 0;
i = 7, j = 6;
while(i >= 0) {
tmp = j;
while(j >= 0) {
if(y[i] - y[j] == 2) {
// printf("(%d) - (%d) = %d\n", y[i], y[j], y[i] - y[j]);
c[k] = (char)(y[i] + '0');
c[k + 1] = (char)(y[j] + '0');
k += 2;
}
j--;
}
j = --tmp;
i--;
}
c[k] = '\0';
// printf("\n");
return 1;
}
bool my9(const int *y, char *c){
int i, j, tmp, k = 0;
i = 0, j = 1;
while(i <= 7) {
tmp = j;
while(j <= 7) {
if(y[i] + y[j] == 9) {
// printf("(%d) + (%d) = %d\n", y[i], y[j], y[i] + y[j]);
c[k] = (char)(y[i] + '0');
c[k + 1] = (char)(y[j] + '0');
k += 2;
}
j++;
}
j = ++tmp;
i++;
}
c[k] = '\0';
// printf("\n");
return 1;
}
int cmp_c(char *cc) {
int i, j;
for(i = 0, j = 1; i < 8; i++) {
for(j = i + 1; j < 8; j++) {
if((cc[i]) == (cc[j])) {
// printf(" %c %c ", cc[i], cc[j]);
return -1;
}
}
}
return -2;
}
int create_table(unsigned short *c0, unsigned short *cc, unsigned short *c1, unsigned short *c2, unsigned short *c7, unsigned short *c9) {
//形如这样的组合
// 1 2
// a b
// c
// D E
// 1acD, 1acE, 1bcD, 1bcE, 2acD, 2acE, 2bcD, 2bcE
//无法推算出四行字符排列组合的所有项的公式,所以这里就用两行的拼凑,然后下面的再拼凑。
int i = 0, j = 0, k = 0;
for(i = 0; i < 7; i++) {
for(j = 0; j < 6; j++) {
cc[2 * (6 * i + j)] = c1[i];
cc[2 * (6 * i + j) + 1] = c2[j];
}
}
for(i = 0; i < 3; i++) {
for(j = 0; j < 4; j++) {
c0[2 * (4 * i + j)] = c7[i];
c0[2 * (4 * i + j) + 1] = c9[j];
}
}
((unsigned char *)cc)[42 * 4 + 0] = 'E';
((unsigned char *)cc)[42 * 4 + 1] = 'N';
((unsigned char *)cc)[42 * 4 + 2] = 'D';
((unsigned char *)cc)[42 * 4 + 3] = '\0';
((unsigned char *)c0)[48] = 'E';
((unsigned char *)c0)[49] = 'N';
((unsigned char *)c0)[50] = 'D';
((unsigned char *)c0)[51] = '\0';
return 0;
}
int c_e_t(unsigned int *end_table, unsigned int *cc, unsigned int *c0) {
int i; int j;
for(i = 0; i < 42; i++) {
for(j = 0; j < 12; j++) {
end_table[2 * (12 * i + j)] = cc[i];
end_table[2 * (12 * i + j) + 1] = c0[j];
}
}
((unsigned char *)end_table)[42 * 12 * 8 + 0] = 'E';
((unsigned char *)end_table)[42 * 12 * 8 + 1] = 'N';
((unsigned char *)end_table)[42 * 12 * 8 + 2] = 'D';
((unsigned char *)end_table)[42 * 12 * 8 + 3] = '\0';
return 0;
}