主要思想:
严教算牌本质上是对数组进行处理;
实际上总可以通过排除相等的元素,使得剩下的数字互不相等;
每找到一对相等的元素,原来位置的元素清零;
对剩下的数字进行递减排序后再进行递增排序,可以把进行相等替换的0排到最后,非0数字排到前面;
对子数组元素进行验证,如果满足a=b+c,则用b c代替a,a返回原数组;(未完成)
对非0数字进行b+c=a循环验证,b 跟c元素进入一个子数组,a进入另一个子数组;
注:
扑克牌只有13个数字,不用考虑过于复杂的情况;
如5+9=3+4+7这种情况是验证不出来的(如果数组中就这5个元素);
a+b=c+d验证就比较繁琐了,一种好的办法是验证a-c=d-b;在严格递增的情况下相邻位置做减法(这样可以产生两个数组),如果产生的差额数组有两个差额元素相等,意味着有四个元素可以被移动到子数组中;
更新:
写了个对子数组进行a=b+c验证的函数;
#include
#include
#define length 10
void exchange(short*, int,int);
void rankup(short*, int);
void rankdown(short*, int);
int equal1(short*,short*,short*, int);
_Bool equal2(short*,short*,short*, int,int,int);
void pirnt(short*, int);
void print(short* array, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
} //定义打印数组函数
int main(void)
{
int length_z = 0;
int length_sgs, length_sgs1, length_sgs2;
short sgs[length] = { 1,1,3,6,8,9,11,10,4,3 };
short sgs1[length] = { 0 };
short sgs2[length] = { 0 };
rankup(sgs, length); //对数组做升序处理
length_z=2*equal1(sgs, sgs1, sgs2, length);
rankdown(sgs, length); //对数组做降序处理,把0排到后面
rankup(sgs, (length - length_z)); //对非0元素做升序处理
length_sgs = length - length_z;
length_sgs1 = length_z/2;
length_sgs2 = length_z/2;
while (equal2(sgs, sgs1, sgs2, length_sgs, length_sgs1, length_sgs2)) {
length_sgs -= 3;
length_sgs1 += 2;
length_sgs2++;
rankdown(sgs, length);
rankup(sgs, length_sgs); //整理sgs数组
}
print(sgs, length);
printf("\n");
print(sgs1, length);
printf("\n");
print(sgs2, length);
printf("\n");
return 0;
}
void exchange(short* array, int m, int n) {
short temp;
temp = *(array + m);
*(array + m) = *(array + n);
*(array + n) = temp;
}
void rankup(short*array, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (*(array + j) > * (array + j + 1)) {
exchange(array, j, j + 1);
}
}
}
}
void rankdown(short* array, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (*(array + j) < * (array + j + 1)) {
exchange(array, j, j + 1);
}
}
}
}
int equal1(short* array, short* array1, short* array2, int n) {
int equal = 0;
for (int i = 0; i < n- 2; i++) {
if (*(array + i) == *(array + i + 1)) {
equal++;
*(array1+ equal-1) = *(array + i);
*(array + i) = 0;
*(array2+ equal-1) = *(array + i + 1);
*(array + i + 1) = 0;
}
}
return equal; //返回其中相等的对数,0的个数是其二倍
}
_Bool equal2(short* array, short* array1, short* array2, int m, int n1,int n2) {
_Bool equal =false;
int i, j, z;
for (i = 0; i < m - 2; i++) {
for (j = i + 1; j < m - 1; j++) {
for (z = i + 2; z < m; z++) {
if (*(array + i) + *(array + j) == *(array + z)) {
*(array1 + n1) = *(array + i);
*(array + i) = 0;
*(array1 + n1+1) = *(array + j);
*(array + j) = 0;
*(array2 + n2) = *(array + z);
*(array + z) = 0;
equal = true;
break;
}
else
continue;
}
break;
}
}
return equal;
}
//以下为新代码
#include
#include
#define length 20
void exchange(short*, int,int);
void diplay(int length_sgs, int length_sgs1, int length_sgs2, short sgs[20], short sgs1[20], short sgs2[20]);
void rankup(short*, int);
void rankdown(short*, int);
int equal1(short*,short*,short*, int);
_Bool equal2(short*,short*,short*, int,int,int);
_Bool equal3(short*, short*, int, int);
void pirnt(short*, int);
void print(short* array, int n) {
for (int i = 0; i < n; i++) {
printf("%2d ", array[i]);
}
} //定义打印数组函数
void diplay(int length_sgs, int length_sgs1, int length_sgs2, short sgs[20], short sgs1[20], short sgs2[20])
{
printf("%2d %2d %2d\n", length_sgs, length_sgs1, length_sgs2);
printf("原数组: ");
print(sgs, length);
printf("\n新数组1:");
print(sgs1, length);
printf("\n新数组2:");
print(sgs2, length);
printf("\n");
}
int main(void)
{
int length_z = 0;
int length_sgs = length;
int length_sgs1 = 0;
int length_sgs2 = 0;
short sgs[length] = { 1,3,2,8,6,8,9,11,12,11,4,3,3,10,4,3,12,13,7,5,};
short sgs1[length] = { 0 };
short sgs2[length] = { 0 };
diplay(length_sgs, length_sgs1, length_sgs2, sgs, sgs1, sgs2);//打印初始数组信息
rankup(sgs, length); //对数组做升序处理
diplay(length_sgs, length_sgs1, length_sgs2, sgs, sgs1, sgs2); //打印做升序后数组信息
length_z=2*equal1(sgs, sgs1, sgs2, length); //对数组做相等替换;
rankdown(sgs, length); //对数组做降序处理,把0排到后面
rankup(sgs, (length - length_z)); //对非0元素做升序处理
length_sgs = length - length_z;
length_sgs1 = length_z/2;
length_sgs2 = length_z/2;
diplay(length_sgs, length_sgs1, length_sgs2, sgs, sgs1, sgs2); //打印对sgs数组做相等替换处理后信息
while (equal2(sgs, sgs1, sgs2, length_sgs, length_sgs1, length_sgs2)) {
length_sgs -= 3;
length_sgs1 += 2;
length_sgs2++;
rankdown(sgs, length);
rankup(sgs, length_sgs); //整理sgs数组
}
diplay(length_sgs, length_sgs1, length_sgs2, sgs, sgs1, sgs2); //打印对sgs做b+c=a替换后信息
while (equal3(sgs, sgs2, length_sgs, length_sgs2)) {
length_sgs--;
length_sgs2++;
rankdown(sgs, length);
rankup(sgs, length_sgs); //整理sgs数组
}
diplay(length_sgs, length_sgs1, length_sgs2, sgs, sgs1, sgs2); //打印对sgs2做b+c=a替换后信息
return 0;
}
void exchange(short* array, int m, int n) {
short temp;
temp = *(array + m);
*(array + m) = *(array + n);
*(array + n) = temp;
}
void rankup(short*array, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (*(array + j) > * (array + j + 1)) {
exchange(array, j, j + 1);
}
}
}
}
void rankdown(short* array, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (*(array + j) < * (array + j + 1)) {
exchange(array, j, j + 1);
}
}
}
}
int equal1(short* array, short* array1, short* array2, int n) {
int equal = 0;
for (int i = 0; i < n- 2; i++) {
if (*(array + i) == *(array + i + 1)) {
equal++;
*(array1+ equal-1) = *(array + i);
*(array + i) = 0;
*(array2+ equal-1) = *(array + i + 1);
*(array + i + 1) = 0;
}
}
return equal; //返回其中相等的对数,0的个数是其二倍
}
_Bool equal2(short* array, short* array1, short* array2, int m, int n1,int n2) {
_Bool equal =false;
int i, j, z;
for (i = 0; i < m - 2; i++) {
for (j = i + 1; j < m - 1; j++) {
for (z = i + 2; z < m; z++) {
if (*(array + i) + *(array + j) == *(array + z)) {
*(array1 + n1) = *(array + i);
*(array + i) = 0;
*(array1 + n1+1) = *(array + j);
*(array + j) = 0;
*(array2 + n2) = *(array + z);
*(array + z) = 0;
equal = true;
break;
}
else
continue;
}
break;
}
}
return equal;
}
_Bool equal3(short*array, short*array1, int m, int n) {
_Bool equal = false;
short temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 1; j++) {
if (*(array1 + i) == *(array + j) + *(array + j + 1)) {
temp = *(array1 + i);
*(array1 + i) = *(array + j);
*(array + j) = temp;
*(array1 + n) = *(array + j + 1);
*(array + j + 1) = 0;
equal = true;
break;
}
else
continue;
}
}
return equal;
}