2019长安大学ACM校赛网络同步赛 Trial of Devil

本文探讨了一道算法题目,目标是找到将序列中所有元素通过最少操作次数变为零的方法。具体而言,每次操作可以选择序列中的任意元素,并选择一个正整数x,从所选元素中减去x。文章提供了简化思路及代码实现,即通过减去中间值来最优化操作次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://ac.nowcoder.com/acm/contest/view-submission?submissionId=40669755

来源:牛客网

题目描述

    As an acmer, Devil Aguin particularly loves numbers. This time, with a sequence consisting of n elements 1∼n initially, Devil Aguin asks you to process the sequence until all the elements in it turn to zero. What you can do in one operation are as following :

    1. Select some elements from the sequence arbitrarily.

    2. Select a positive integer x arbitrarily.

    3. Subtract x from the elements you select.

    It is obvious that there are various methods to make elements of the sequence turn to zero. But Devil Aguin demands of you to use the minimum operations. Please tell him how many operations you will use.


输入描述:
The first line contains an integer number T, the number of test cases.

    ithith of each next T lines contains one integer n(1≤n≤1001≤n≤100), the number of sequence.

输出描述:
For each test case print a number, the minimum number of operations required.

示例1

输入

2

1

2

输出

1

2

思路

这道题的大概意思是输入t组数据,每组数据有一个数n,代表1~n的数,每次操作可以对任意数减去其中的某个数,直到所有数为0;这道题我们可以简化成将其最大的数n变为0当然我们不能直接减n,通过找规律发现每次减去中间值数是最划算的

代码:

#include<bits/stdc++.h>

using namespace std;

int main()

{

       int t;

       cin>>t;

       while(t--)//对于t组数据

       {

              int n;

              cin>>n;

              int s=0;

              while(n)//当最大值n为0时跳出来

              {

                     s++;

                     if(n%2==0)//每次减去中间值

                     n=n/2;

                     else

                     n=(n-1)/2;

              }

              cout<<s<<endl;

       }

 }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值