求最大公约数

<pre name="code" class="cpp">//求x和y的最大公约数 x<y
	//辗转相除法O(log(n))
	int susu(int x, int y)
	{		
		if(x>y)
		{
			int t = x;
			x = y;
			y = t;
		}
		if(x==0)
		{
			return y;
		}
		return susu(y%x, x);
	}
	//暴力搜索O(n)
	int susu1(int x, int y)
	{		
		if(x>y)
		{
			int t = x;
			x = y;
			y = t;
		}
		int result = 1;
		for(int i=1; i<=x ;i++)
		{
			if(x%i==0 && y%i==0)
			{
				result = i;
			}
		}
		return result;
	}



   

posted on 2016-03-28 14:58  长456风  阅读(229)  评论(0编辑  收藏  举报

导航