2021中国大学生程序设计竞赛(CCPC)- 网络选拔赛 HDU-1001 Cut The Wire
2021/10/21 14:09:25
本文主要是介绍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; }
这篇关于2021中国大学生程序设计竞赛(CCPC)- 网络选拔赛 HDU-1001 Cut The Wire的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27Rocket消息队列资料:新手入门指南
- 2024-11-27rocket消息队资料详解与入门指南
- 2024-11-27RocketMQ底层原理资料详解入门教程
- 2024-11-27RocketMQ项目开发资料:新手入门教程
- 2024-11-27RocketMQ项目开发资料详解
- 2024-11-27RocketMQ消息中间件资料入门教程
- 2024-11-27初学者指南:深入了解RocketMQ源码资料
- 2024-11-27Rocket消息队列学习入门指南
- 2024-11-26Rocket消息中间件教程:新手入门详解
- 2024-11-26RocketMQ项目开发教程:新手入门指南