Zn likes collecting coins.What he likes most is the ‘one yuan’coin which was made in1996 Jesus, no one knows why he is so wild about it .these days he was not happy ,because there was a fake coin among his coins .Zn want to find it out,and what he has is an old balance .since Zn is very lazy man,he want to know how many times at the most are needed to find the fake coin in the worst situation.The fake coin is lighter than others and the rest have the same weight.
輸入格式:
輸入一行,為一個整數
輸出格式:
輸出一行,為一個整數
樣例輸入:
2
樣例輸出:
1
樣例輸入:
100
樣例輸出:
5
我的代碼
#include <stdio.h>
int main()
{
int n;
int time=0;
scanf("%d",&n);
for(;;)
{
if(n==2&&n==3)
{
time++;
printf("%d",time);
break;
}
else
{
if(n%3==0)
{
time++;
n=n/3;
}
else
{
time++;
n=((n-(n%3))/3)+(n%3);
}
}
}
}
uj5u.com熱心網友回復:
供參考:#include <stdio.h>
int main()
{
int n;
int time=0;
scanf("%d",&n);
for(;;)
{
if(n==2 || n==3)//if(n==2&&n==3)
{
time++;
printf("%d",time);
break;
}
else
{
if(n%3==0)
{
time++;
n=n/3;
}
else
{
time++;
n=((n-(n%3))/3)+(n%3);
}
}
}
return 0;
}
uj5u.com熱心網友回復:
謝謝!我這屬于低級錯誤。。。。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270286.html
標籤:C語言
