| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
模拟队列出入队的c代码Memory: 932K Time: 16MS
#include<stdio.h>
#include<string.h>
#define MAX 100005
int a[MAX],i=0,j=0,v[MAX], x,y;
void in(int k)
{
a[j] = k, j = (j + 1) % MAX;
}
void out()
{
if(v[a[i]+1]==0&&a[i]+1<MAX) v[a[i] + 1] = v[a[i]] + 1,in(a[i] + 1);
if (v[a[i]*2] == 0 && a[i]*2<MAX) v[a[i]*2] = v[a[i]] + 1, in(a[i]*2);
if (v[a[i] - 1] == 0 && a[i] - 1>=0) v[a[i] - 1] = v[a[i]] + 1, in(a[i] - 1);
i = (i + 1)%MAX;
}
int main()
{ scanf("%d%d", &x, &y);
if (x >= y) printf("%d\n", x - y);
else {
in(x);
while (i != j&&v[y]==0)out();
printf("%d\n", v[y]);
}}
当做方便新手理解用吧 数据结构课的成果..循环队列加树的层次遍历 好像比c++写能优化一点点
顺便祝大家鸡年大吉吧~
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator