您的位置:首页 > 其它

scau 10692 XYM-入门之道

2015-12-23 12:50 351 查看
题目:http://paste.ubuntu.com/14157516/

思路:判断一个西瓜,看看能不能直接吃完,如果能,就吃了。但是:如果不能,就要分成两半,就这样分割,不用以为要用到n维数组,用一个一维数组就够了,利用队列的特点,把分割了的入队,然后一直遍历整个队列。

#include <stdio.h>
#include <stdlib.h>
void work()
{
int n,k;
int que[100000]={0};
int head,tail;
int i_count=0;
head=tail=1;
scanf ("%d%d",&n,&k);
que[tail++]=n;
while (head<tail)
{
if (que[head]<=k)
{
i_count++;
head++;
}
else
{
if (que[head]%2==0)
{
que[tail++]=que[head]/2;
que[tail++]=que[head]/2;
head++;
}
else
{
que[tail++]=que[head]/2;
que[tail++]=que[head]/2+1;
head++;
}
}
}
printf ("%d\n",i_count);
return ;
}
int main()
{
int t;
scanf ("%d",&t);
while (t--)
{
work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: