程序代码:
//.c文件
#include <stdio.h>
#include <malloc.h>
//待处理数据组
int question_set[3][7] ={0};
//答案集合数组
int answer_set[13][5] =
{
{0},
{1, 13, 25, 37, 49},
{2, 14, 26, 38, 0},
{3, 15, 27, 39, 0},
{4, 16, 28, 40, 0},
{5, 17, 29, 41, 0},
{6, 18, 30, 42, 0},
{7, 19, 31, 43, 0},
{8, 20, 32, 44, 0},
{9, 21, 33, 45, 0},
{10, 22, 34, 46, 0},
{11, 23, 35, 47, 0},
{12, 24, 36, 48, 0}
};
//判断是否在某个数组集中
int isAnswer(int data)
{//是其中一员则返回序号 否则返回0
int index = 0;
int i = 0;
for (index=1; index<13; ++index)
{
for (i=0; i<5; ++i)
{
if (answer_set[index][i] == data)
{
return index;
}
}
}
return 0;
}
//初始化待处理的数据组
void init_Q_Set()
{
int index = 0;
int dim = 0;
for (dim=0; dim<3; ++dim)
{
printf("输入第%d组7个数据\n", dim+1);
for (index=0; index<7; ++index)
{
scanf("%d", &question_set[dim][index]);
}
}
}
//把字符加入到字符串当中
char * itos(char *string, int index, char src)
{
string[index] = src;
return string;
}
//判断由公式得出是否第二组也满足此公式
int do_2_3(char *string)
{
int index=0;
int i=0;//
int sum = 0;
while(string[index] != 0)
{
i = question_set[1][string[index]-48];
sum = question_set[2][string[index]-48];
++index;
if (string[index] == '1')
{
++index;
if (string[index++] == '-')
{
i = i%10 - (string[index] - 48);
sum = sum%10 - (string[index] - 48);
}
else
{
i = i%10 + (string[index] - 48);
sum = sum%10 + (string[index] - 48);
}
}
else if (string[index] == '2')
{
++index;
if (string[index++] == '-')
{
i = i/10 - (string[index] - 48);
sum = sum/10 - (string[index] - 48);
}
else
{
i = i/10 + (string[index] - 48);
sum = sum/10 + (string[index] - 48);
}
}
else if (string[index] == '3')
{
++index;
if (string[index++] == '-')
{
i = i%10 + i/10 - (string[index] - 48);
sum = sum%10 + sum/10 - (string[index] - 48);
}
else
{
i = i%10 + i/10 + (string[index] - 48);
sum = sum%10 + sum/10 + (string[index] - 48);
}
}
else if (string[index] == '4')
{
++index;
if (string[index++] == '-')
{
i = i - (string[index] - 48);
sum = sum - (string[index] - 48);
}
else
{
i = i + (string[index] - 48);
sum = sum + (string[index] - 48);
}
}
++index;
if (i && (question_set[2][6]-1)%12+1 == isAnswer(i))
{
if ( sum>0 && sum <=49 )
{
printf("\n%s\n", string);
i = 0;
printf("第%c位数据", string[i]+1);
++i;
switch (string[i])
{
case '1':
printf(" 只用其低位数据");
break;
case '2':
printf(" 只用其高位数据");
break;
case '3':
printf(" 用其高低数据相加");
break;
case '4':
printf(" 用其数据");
break;
}
++i;
if (string[i] == '-')
{
++i;
printf("再减掉%c\n", string[i]);
}
else
{
++i;
printf("再加上%c\n", string[i]);
}
return 1;//表示符合
}
}
}
return 0;//表示不符合
}
//获取第一组满足要求的公式串
int do_1_2(int index_array, int index)
{
int x = 0;
int sum = 0;
char *string = (char*) malloc (20*sizeof(char));
int indexStr = 0;
int flag = 0;
//采用加上一个数(1~10)的格式
for (x=1; x<=10; ++x)
{
flag = 0;
indexStr = 0;
string = itos(string, indexStr++, (char)(48+index_array));
//方案1拆开数据用其数据值的低位 加上某个数
sum = question_set[index-1][index_array]%10 + x;
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+1);
flag = 1;
}
//方案2拆开数据用其数据值的高位 加上某个数据
if (!flag)
{
sum = question_set[index-1][index_array]/10 + x;
}
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+2);
flag = 1;
}
//方案3拆开数据 高位和低位都用上 加上某个数据
if (!flag)
{
sum = question_set[index-1][index_array]/10 +
question_set[index-1][index_array]%10 + x;
}
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+3);
flag = 1;
}
//方案4不拆开数据 直接用数据加上某个数据
if (!flag)
{
sum = question_set[index-1][index_array] + x;
}
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+4);
flag = 1;
}
if (flag)
{
string = itos(string, indexStr++, '+');
string = itos(string, indexStr++, (char)(48+x));
string = itos(string, indexStr++, '\0');
//return string;
if (sum && do_2_3(string))
{
return 1;
}
}
///////////////////////////////////////////////////////////////////
indexStr = 0;
if (!flag)
{
string = itos(string, indexStr++, (char)(48+index_array));
}
//方案1拆开数据用其数据值的低位 加上某个数
sum = question_set[index-1][index_array]%10 - x;
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+1);
flag = 1;
}
//方案2拆开数据用其数据值的高位 加上某个数据
if (!flag)
{
sum = question_set[index-1][index_array]/10 - x;
}
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+2);
flag = 1;
}
//方案3拆开数据 高位和低位都用上 加上某个数据
if (!flag)
{
sum = question_set[index-1][index_array]/10 +
question_set[index-1][index_array]%10 - x;
}
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+3);
flag = 1;
}
//方案4不拆开数据 直接用数据加上某个数据
if (!flag)
{
sum = question_set[index-1][index_array] - x;
}
if (!flag && (question_set[index][6]-1)%12+1 == isAnswer(sum))
{//
string = itos(string, indexStr++, 48+4);
flag = 1;
}
if (flag)
{
string = itos(string, indexStr++, '-');
string = itos(string, indexStr++, (char)(48+x));
string = itos(string, indexStr++, '\0');
if (sum && do_2_3(string))
{
return 1;
}
//return string;
}
}
return 0;
}
//计算第一组
void cal_1_2()
{
int index = 0;
for (index=0; index<7; ++index)
{
if (do_1_2(index, 1))
{
return;
}
}
return;
}
void deal()
{
init_Q_Set();//
cal_1_2();//
}
int main(void)
{
deal();
return 0;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册