1309.txt

来自「北大ACM题目例程 详细的解答过程 程序实现 算法分析」· 文本 代码 · 共 49 行

TXT
49
字号


#include<stdio.h>
#include<math.h>
main()
{

	long N,i,j,t,ans;
	int flag=0;
	//FILE in,out
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	
	scanf("%ld",&N);
	while(N>0)
	{
		flag=0;
		for(j=N-1;j>1;j--)
		{
			
			i=0;
			t=N;
			while(t%j==1)
			{
				t-=(t/j+1);
				i++;
			}
			if(t%j==0&&i==j)
			{
				flag=1;
				ans=j;
				printf("%ld coconuts, %ld people and 1 monkey\n",N,ans);

				break;
			}
		}
		if(flag==0)
			printf("%ld coconuts, no solution\n",N);
	
		scanf("%ld",&N);
	}

	
	
//	fclose(stdin);
//	fclose(stdout);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?