2021中国大学生程序设计竞赛(CCPC)网选赛(第一场)部分题解

编程入门 行业动态 更新时间:2024-10-27 13:27:08

2021中国大学生程序设计竞赛(CCPC)网选赛(第一场)部分<a href=https://www.elefans.com/category/jswz/34/1769599.html style=题解"/>

2021中国大学生程序设计竞赛(CCPC)网选赛(第一场)部分题解

2021CCPC竞赛网络选拔赛(第一场)部分题解

  • 前言
  • 1001 Cut The Wire
    • 题意:
      • 思路:
        • 代码:
  • 1006 Power Sum
    • 题意:
      • 思路:
        • 代码:
  • 1009 Command Sequence
    • 题意:
      • 思路:
        • 代码:
  • 总结


前言

随着8月28号下午5点一到,今年ccpc选拔赛落下帷幕,自闭的五小时也就这样结束了
首先,打完这场比赛我深刻认识到自己的菜鸡程度,突然感觉回家种田也许是个不错的选择…

其次,这次的比赛可以说是我打的最艰辛的一场了,杭电oj炸了5小时,为了看有没有过代码就在那蒽刷榜单,给人整麻了,然后举办方成功的发现这比赛得重开了…自闭完5小时没想到还有第二次…


1001 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是奇数,那么他(x)和第 3x+1 盏路灯相连;如果x是偶数,那么他(x)和第 x / 2 盏路灯相连。
你现在站在第n和n+1的路灯之间,经过你的电线你可以一刀切断,也就是说当两盏能够相连的路灯a,b满足a<=n且b>n就算做一条经过你的电线,求你能斩断多少条电线

思路:

其实理解了题意就会了,以n为界限,如果i<n,那么i就不可能是偶数(i和i/2必定小于n,电线过不来),如果i>n,那么i就不能是奇数(i和3i+1必大于n,电线也过不来)
那么,这题就是求当i<n时,i为奇数的最小值到n之间的奇数个数;当i>n时,i为偶数到i*2之间的偶数个数。两者相加就是答案

代码:
#include <iostream>
#include <cmath>
#include <algorithm>#define ll long long
using namespace std;
const int INF=100000000;int main()
{int t;cin>>t;while(t--){int n;int ans=0;cin>>n;if(n%2==0)//分情况讨论,如果n是偶数的话{ans+

更多推荐

2021中国大学生程序设计竞赛(CCPC)网选赛(第一场)部分题解

本文发布于:2024-03-06 13:11:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1715416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:题解   第一场   程序设计   中国大学生   网选赛

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!