The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
题意:首先有一堆硬币,面值是3的n次方:1,3,9,27……,然后给你一个数n,让你用硬币去换,条件是:
你用硬币要尽可能的大于n并且最接近n;
你用的硬币数量要尽可能的多;
问要多少个硬币换取。
题解:首先1面值的不能用,然后找到第一个不能整除3的面值,用除数+1就是结果。
#include
#include
#include
#include
#include
using namespace std;
int main()
{
long long int n,t;
long long int ans;
while(~scanf("%lld",&n))
{
if(n%3!=0)
{
ans=n/3+1;
}
else if(n%3==0)
{
t=3*3;
while(t)
{
if(t==n)
{
ans=1;
break;
}
else if(n%t!=0)
{
ans=n/t+1;
break;
}
else
t=t*3;
}
}
printf("%lld\n",ans);
}
}