2021中国大学生程序设计竞赛(CCPC)- 网络选拔赛 HDU-1001 Cut The Wire
作者:互联网
Cut The Wire
Problem Description
In the country of Infinity , there is a strange road. This road only has a starting point, but no end. Since this road is infinite, there are also countless street lights. The street lights are numbered from 1(the starting point) to infinity. The street lights are connected by wires under a strange law:
For a street light x,
if x is even, then x is connected with x2 by a wire;
if x is odd, then x and 3x+1 is connected by a wire.
Now Kris is standing in the middle of street light n and n+1, and he is able to cut all wires passing by. That is, he will cut all wires connecting street lights a and b satisfying a≤n and b>n.
Now he wonders, how many wires he will cut. Please help him calculate.
Input
This problem contains multiple test cases.
The first line contains an integer T(1≤T≤105) indicating the number of test cases.
The next T lines each contains one integer n(1≤n≤109).
Output
For each test case, output one line of one integer indicating the answer.
Sample Input
2
12
60
Sample Output
10
50
题意描述:对于路灯X,如果 X 是偶数,那么 X 与 X2 通过电线;如果 X 是奇数,那么 X 和 3 x + 1连接。以n为分界线开始剪电线,其中a≤n, b>n,也就是说对于电线编号为偶数情况时,直接减掉n到n/2之间为偶数编号的电线就行了,同理,当电线编号为奇数时,剪掉n到(n-1)/3之间为奇数编号的电线就行了,不过此时会出现两种情况,比如说12,15:对于12,我们需要剪掉12到3之间的奇数编号电线,中间有9/2等于4个,不过对于15,我们需要剪掉15到5的奇数编号电线,此时是10/2等于5个,实际上我们需要剪掉6个电线(具体可以自己画画理解),然后我们只需要根据n是奇偶来分情况写就行了(具体看代码)
易错:a可以取到n,然后就是对奇数划分情况注意一下就行了
AC
#include<stdio.h>
int main(void)
{
int t,n,sum;
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d",&n);
sum=n-n/2;//偶数情况
if(n%2==0)//奇数情况
{
sum=sum+(n-(n-1)/3)/2;
}
else
{
sum=sum+(n-(n-1)/3+1)/2;
}
printf("%d\n",sum);
}
return 0;
}
标签:电线,Wire,奇数,sum,HDU,CCPC,lights,street,编号 来源: https://blog.csdn.net/m0_58245389/article/details/120884699