您的位置:首页 > 理论基础

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

2015-11-30 09:17 423 查看
<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;">2
4
5</div>


Sample Output

0
2



#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^k
int 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: