2019长安大学ACM校赛网络同步赛 BTrial of Devil (递归)
生活随笔
收集整理的這篇文章主要介紹了
2019长安大学ACM校赛网络同步赛 BTrial of Devil (递归)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:https://ac.nowcoder.com/acm/contest/897/B
來源:牛客網
空間限制: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.
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,
代表有一個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; }?
轉載于:https://www.cnblogs.com/qieqiemin/p/10931300.html
總結
以上是生活随笔為你收集整理的2019长安大学ACM校赛网络同步赛 BTrial of Devil (递归)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 淦ORB-SLAM2源码 09--SIM
- 下一篇: A-priori算法的简单实现