Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created March 17, 2015 08:11
Show Gist options
  • Save jsfaint/65c1e35c00666614641b to your computer and use it in GitHub Desktop.
Save jsfaint/65c1e35c00666614641b to your computer and use it in GitHub Desktop.
桃子的问题
/*桃子的问题
1毛钱可以买一个桃子,每3个桃核可以换一个桃
问n元钱总共可以吃到多少个桃子
Author:jason
Date:2006.12.23
来源于群中的一个脑筋急转弯的问题。可用递归算法实现
*/
#include <stdio.h>;
#include <math.h>;
int core(int n,int sum);
void main()
{
int n,money;
int sum;
printf("please input the number of the money: ");
scanf("%d",&money);
n=money*10;
sum=n;
sum=core(n,sum);
printf("The number of the peach:%d",sum);
getch();
}
int core(int n,int sum)
{
int tmp;
while(n>=3)
{
tmp=n/3;
if((n%3)==0)
n=tmp;
else
n=tmp+sum%3;
sum=sum+tmp;
core(n,sum);
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment