hdu计算机学院大学生程序设计竞赛(2015’11)1001 搬砖

来源:互联网 发布:c语言实例100 编辑:程序博客网 时间:2024/05/17 21:45
<h1 style="color: rgb(26, 92, 200); text-align: center; font-family: 'Times New Roman'; margin-top: 20px;">搬砖</h1><span style="font-family:Arial;font-size: 14px; text-align: -webkit-center;"><strong><span style="font-size: 12px; color: green;">Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4785    Accepted Submission(s): 1132</span></strong></span><br style="font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center;" /><br style="font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;">  小明现在是人见人爱,花见花开的高富帅,整天沉浸在美女环绕的笙歌妙舞当中。但是人们有所不知,春风得意的小明也曾有着一段艰苦的奋斗史。  那时的小明还没剪去长发,没有信用卡没有她,没有24小时热水的家,可当初的小明是那么快乐,尽管甚至没有一把破木吉他…  之所以快乐,是因为那时的小明心怀逆袭梦想。有一天,小明为了给他心目中的女神买生日礼物,来到了某建筑工地搬砖挣钱。就在这个时候,工地上又运来了一卡车的砖,包工头让小明把卡车卸下来的那堆砖分成一块一块的(要求任何2块转都要分开)。作为资深搬运工,小明总是每次将一堆砖分为两堆,这时候,所消耗的体力是分完之后两堆砖数目的差值。  现在,已知卡车运来的砖的数目,请告诉小明最少要花费多少体力才能完成包工头所要求的任务呢?</div><div class="panel_bottom" style="height: auto; margin: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"> </div><br style="font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;">输入数据第一行是一个正整数T(T<=100),表示有T组测试数据。接下来T行每行一个正整数N(N<=10000000),表示卡车运来的砖块的数目。</div><div class="panel_bottom" style="height: auto; margin: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"> </div><br style="font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;">对于每组数据,请输出小明完成任务所需的最少体力数。</div><div class="panel_bottom" style="height: auto; margin: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"> </div><br style="font-family: 'Times New Roman'; font-size: 14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="word-wrap: break-word; width: inherit; font-family: 'Courier New', Courier, monospace; white-space: pre;">245</div>
 

Sample Output
02

#include <cstdio>using namespace std;int cou;//判断是不是2^k,如果是的话那么以后肯定能平均分完耗费体力为0//不是的话下面进行递归bool check(int n) {while (n > 1) {if (n % 2 != 0)return false;else {n >>= 1;}}return true;}void f(int n) {if (check(n)) return;   //递归出口,n == 1或者n是2^kint t1, t2;   //用来存储分成的两堆if (n % 2 == 0) {t1 = t2 = n / 2;}else {t1 = n / 2 + 1;t2 = n / 2;cou++;   //分成的两堆一奇一偶,差一,积累一体力}f(t1);   //开始递归这两堆,其实为偶数的时候只要一堆的体力乘以二就行了,没有优化f(t2);}int main(){int T, N;scanf("%d", &T);while (T--) {scanf("%d", &N);cou = 0;f(N);printf("%d\n", cou);}return 0;}

0 0
原创粉丝点击