1110: Coconuts, Revisited
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 992 | 279 | Standard |
Soon a second man woke up and did the same thing. After dividing the coconuts into five piles, one coconut was left over which he gave to the monkey. He then hid his share and went back to bed. The third, fourth, and fifth man followed exactly the same procedure. The next morning, after they all woke up, they divided the remaining coconuts into five equal shares. This time no coconuts were left over.
An obvious question is ``how many coconuts did they originally gather?" There are an infinite number of answers, but the lowest of these is 3,121. But that's not our problem here.
Suppose we turn the problem around. If we know the number of coconuts that were gathered, what is the maximum number of persons (and one monkey) that could have been shipwrecked if the same procedure could occur?
Input
The input will consist of a sequence of integers, each representing the number of coconuts gathered by a group of persons (and a monkey) that were shipwrecked. The sequence will be followed by a negative number.Output
For each number of coconuts, determine the largest number of persons who could have participated in the procedure described above. Display the results similar to the manner shown below, in the Sample Output. There may be no solution for some of the input cases; if so, state that observation.Sample Input
25 30 3121 -1
Sample Output
25 coconuts, 3 people and 1 monkey 30 coconuts, no solution 3121 coconuts, 5 people and 1 monkey
#include<iostream>
using namespace std;
int main()
{
int n,i,j;
while(cin>>n)
{
if(n==-1) break;
for(i=n-1;i>1;i--)
// for(i=2;i<n;i++)
{
int t=n;
for(j=0;j<i;j++)
{
if(t%i==1) t=t/i*(i-1);
else break;
}
if(j==i&&t%i==0) break;
}
if(i==1) cout<<n<<" coconuts, no solution"<<endl;
else cout<<n<<" coconuts, "<<i<<" people and 1 monkey"<<endl;
}
return 0;
}
本文探讨了一个经典的数学问题——椰子分配问题。该问题源于一个短篇故事,讲述了五个男人和一只猴子在荒岛上如何公平地分配椰子的故事。文章详细介绍了问题背景,并提供了一段C++代码作为解决方案。
339

被折叠的 条评论
为什么被折叠?



