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

0ms...C++,注意数组和边界问题,开大开小都会RE,水过80题纪念!

Posted by wtl666wtl at 2016-02-26 11:08:22 on Problem 3278 and last updated at 2016-02-26 11:10:04
#include <cstdio>
#include <algorithm>
using namespace std;
int ans,n,m,k,z,q[1000001],d[1000001];
bool f[1000001];
void gao (int x)
{
	int h=0,t=1;
	q[1]=x;
	f[x]=1;
	d[x]=0;
	while(h<t) {
		h++;
		k=q[h]+1;
		if (f[k]==0&&k<=100000) {
			t++;
			q[t]=k;
			f[k]=1;
			d[k]=d[q[h]]+1;
		}
		k=q[h]-1;
		if (f[k]==0&&k>=0) {
			t++;
			q[t]=k;
			f[k]=1;
			d[k]=d[q[h]]+1;
		}
		k=q[h]*2;
		if (f[k]==0&&k<=100000) {
			t++;
			q[t]=k;
			f[k]=1;
			d[k]=d[q[h]]+1;
		}
		if(f[m]==1)return;
	}
}
int main ()
{
	scanf("%d%d",&n,&m);
	gao(n);
	printf("%d\n",d[m]);
	return 0;
}

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