一根29cm长的尺子,只允许在它上面刻7个刻度。若要用它能测量出1~ 29cm的各种整长度,刻度应如何选择?
#include<stdio.h>
#include<math.h>
check(int b,int c,int d,int e,int f,int g,int h)
{
int refer[30] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
int a[9];
int j,k,l;
a[0] = 0;
a[1] = b;
a[2] = c;
a[3] = d;
a[4] = e;
a[5] = f;
a[6] = g;
a[7] = h;
a[8] = 29;
int beta[9];//构建二维图,计算出之间的差值。
for (int i = 0; i<9; i++)
beta[i] = a[i];
for (j = 0; j<9; j++)
for (k = j+1; k<9 ; k++)
for (l = 0; l<30; l++)
if(beta[k] - a[j] == refer[l])
refer[l] = 30;
int count = 0;
for (int m = 0; m<30; m++)
if(refer[m] == 30)
count += 1;
if (count == 29)
return 1;
else
return 0;
}
int main()
{
int a = 0,b,c,d,e,f,g,h,i = 29;
int count = 0;
for (b = a+1;b<29; b++)
for (c = b+1; c<29; c++)
for (d = c+1; d<29; d++)
for (e = d+1; e<29; e++)
for (f = e+1; f<29; f++)
for (g = f+1; g<29; g++)
for (h = g+1; h<29; h++) //循环太多,建议改进。。。
if (check(b,c,d,e,f,g,h) == 1)
{
printf("%d %d %d %d %d %d %d %d %d\n", a,b,c,d,e,f,g,h,i);
count += 1;
}
printf("add up to = %d", count);
}