学习,代码变量换了一下!参考李晨经纪人的代码
程序代码:
#include <stdio.h>
/*float cards[7];
int playcards(float n, int o);
int main()
{
int i, n;
for (int i = 0; i < 7; i++)
{
scanf("%f", &cards[i]);
}
for (i = 7; i > 0; i--)
{
n = playcards(13, i);
if (n == 1)
{
break;
}
}
return 0;
}
int playcars(float n, int o)
{
if (o > 1)
{
if (playcards(n - cards[o-1], o-1))
{
printf(" + %.f", cards[o-1]);
return 1;
}
if (playcards(n + cards[o-1], o - 1))
{
printf(" - %.f", cards[o-1]);
return 1;
}
if (playcards(n * cards[o-1], o - 1))
{
printf(" / %.f", cards[o-1]);
return 1;
}
if (playcards(n / cards[o-1], o - 1))
{
printf(" * %.f", cards[o-1]);
return 1;
}
else
{
return 0;
}
}
if (o == 1 && n == cards[0])
{
printf("%.f", cards[0]);
return 1;
}
else
{
return 0;
}
}
*/
#define NUM 7
float cards[NUM];
bool calculateCardSum(float result, int count);
int main(int argc, char argv[])
{
int i = 0;
for (int i = 0; i < NUM; i++)
{
scanf("%f", &cards[i]);
}
for (int i = 7; i > 0; i--)
{
if (calculateCardSum(13, i))
{
printf("\n");
break;
}
}
return 0;
}
bool calculateCardSum(float result, int count)
{
if (count > 1)
{
if (calculateCardSum(result - cards[count-1], count - 1))
{
printf(" + %.f", cards[count-1]);
return true;
}
if (calculateCardSum(result + cards[count-1], count - 1))
{
printf(" - %.f", cards[count-1]);
return true;
}
if (calculateCardSum(result * cards[count-1], count - 1))
{
printf(" / %.f", cards[count-1]);
return true;
}
if (calculateCardSum(result / cards[count-1], count - 1))
{
printf(" * %.f", cards[count-1]);
return true;
}
}
else
{
if (result == cards[0])
{
printf("%.f", cards[0]);
return true;
}
}
return false;
}
[此贴子已经被作者于2018-3-11 22:47编辑过]