关于m个桃子n只猴子分桃问题

img


请问这个题的思路该怎么去想
请问下面这个代码或者逻辑上哪里写的不对



#include <stdio.h>
int main()
{
    int m = 0,k = 0,n = 0;
    printf("输入 n k:");
    scanf("%d %d",&n,&k);
    int i = 0,j = 0;
    for(i = 0;;i++)
    {
        m = i;
        for(j = 0;m%n==k&&m>0&&j<n;j++)
        {
            m = m-m/n-k;
        }
        if(j==n&&m>0)
        {
            printf("%d ",i);
            break;
        }
    }
    return 0;
}


#include <iostream>
using namespace std;
bool left(int x,int ntimes,int nMonkeys);
 
void main()
{
    cout<<"Please input the monkeys"<<endl;
    int nMonkeys;
    cin>>nMonkeys;
    
    int x = 2;
    while( !left(x,nMonkeys,nMonkeys))
        x++;
    cout<<"The number of peach is :"<<x<<endl;
}
 
bool left(int x,int ntimes,int nMonkeys)
{
    if (ntimes ==0)
        return true;
    else
    {
        if( (x-1)%nMonkeys !=0 )
            return false;
        else
            return left( (x-1)*(nMonkeys-1)/nMonkeys,ntimes-1,nMonkeys);
    }
        
}