#include <stdio.h>
int findnumb(int max)
{
static int total=2;
static int desk=1;
if(total>max)
{
return 0;
}
else
{
if(desk>=total)
{
total++;
desk=1;
return findnumb(max);
}
else
{
desk++;
int left=desk*(desk-1)/2;
int right=(total*(total+1)/2)-(desk*(desk+1)/2);
if(left==right)
{
printf("Desk number %d, number of participants %d\n", desk, total);
return findnumb(max);
}
return findnumb(max);
}
}
}
main()
{
int max;
printf("Enter the upper bound:\n");
scanf(" %d", &max);
printf("------------------------\n");
findnumb(max);
printf("Search is complete");
}
Run Code Online (Sandbox Code Playgroud)
这是一个像这样的谜语:
假设存在会议,我们不知道有多少人参加会议.但是,我们看到一个人退出会议.如果我们问这个人在会议上有多少人,他只回答"我不知道".尽管如此,他还是说"我正坐在一张编号为x的椅子上,而且我和我之上的数字总和相等.换句话说,如果椅子的数量是x,那么1,2,3 ...... .x-1 = x + 1 x + 2 …