链接:https://ac.nowcoder.com/acm/contest/897/B
来源:牛客网
Trial of Devil
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
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.
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
输出
复制1 2
题意:
给你一个t组数据,每一个数据给你一个整数n,
代表有一个n的全排列, 你可以做一些操作让他们的值都变成0,。
每次操作,你可以选择全排列中的一些数,然后再选择任意一个x,让那些数减去x。
现在问你最小的操作次数是多少?
思路:
对于每一个整数n的全排列,我们第一次就选择大于等于n/2的那些数,然后减去n/2。
例如8
1 2 3 4 5 6 7 8
8/2=4
减去4后就是
1 2 3 0 1 2 3 4
因为处理1 2 3 4 的时候,就可以顺带的把1 2 3 也给处理了,所以我们问题就转化为了 n/2的全排列的减为0的问题。
通过一直这样转为更小的全排列的操作,我们即可得到最优的解,当n=1的时候,只需要1步就可以减为0。
那么我们递归函数就可以写成:
int f(int x) { if(x==1) { return 1; }else { return 1+f(x/2); } }
AC代码:
int f(int x) { if(x==1) { return 1; }else { return 1+f(x/2); } } int main() { //freopen("D:\common_text\code_stream\in.txt","r",stdin); //freopen("D:\common_text\code_stream\out.txt","w",stdout); int t; gbtb; cin>>t; while(t--) { int n; cin>>n; if(n==1) { cout<<1<<endl; }else { cout<<f(n)<<endl; } } return 0; }