Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

模拟队列出入队的c代码

Posted by 416221843 at 2017-01-29 22:17:54 on Problem 3278
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator