8.2 练习题

C题 HDU1995汉诺塔

其实 s=s1+s2+s3.....sn;   总移动次数=第一个盘子的移动次数+第二个盘子的移动次数+...+第n个盘子的移动次数
如总共有n个盘子,第k个盘子的移动次数   s(k)=2^(n-k);

//递归
#include<stdio.h>
long long int han(int n)
{
	if(n==0)
	return 1;
	return 2*han(n-1);
}
int main()
{
	int t,n,k;
	while(scanf("%d",&t)!=EOF)
	{
		while(t--)
		{
			scanf("%d%d",&n,&k);
			long long int sum=han(n-k);
			printf("%lld\n",sum);
		}
	}
return 0;
}

D题51nod 1091线段的重叠

排序前

排序后

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;
const int maxn=50005;//线段的数量 
struct region{
	int start;
	int end;
}a[maxn];
int ans=0;
int n;

bool cmp(region x,region y)
{
	if(x.start<y.start)  return true;
	if(x.start==y.start&&x.end>y.end)  return true;
	return false;
}
void solve()
{
    sort(a,a+n,cmp);//起点升序  若起点相同,终点降序 
    region m=a[0];
    for(int i=1;i<n;i++)
    {
    	if(a[i].end<=m.end) //线段i在线段i-1内 
		 ans=max(ans,a[i].end-a[i].start);
		else
		{
		    ans=max(ans,m.end-a[i].start); //线段i不在线段i-1内  
			m=a[i];  //选择最靠后的线段,更新
		}
    }
	printf("%lld\n",ans);	
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	  scanf("%lld%lld",&a[i].start,&a[i].end); 
	solve();
	return 0;
} 

G题(枚举)HDU6025

你知道什么是互质序列吗?是包含n个正整数,并且最大公约数是1的序列 。 
由于它的限制,互质序列很容易被找到。
我们可以通过删除一个整数,使 其它数的gcd最大化。 
输入:T组数据,n代表序列中数的个数,接下来输入这些数
输出:其它数的最大gcd 

#include<iostream>
#include<math.h>
using namespace std;

int a[100010],bef[100010],beh[100010],n,i;
int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
} 
//主要代码 
void solve()
{
		bef[1] = a[1]; 
		for (int i= 2; i<=n; i++)//前i个数的最大公约数 
		bef[i] = gcd(bef[i - 1], a[i]);
		
		beh[n] = a[n];
		for (int i= n- 1; i >= 1; i--)//后i个数的最大公约数 
		beh[i] = gcd(beh[i + 1], a[i]);
		 
		int ans = max(bef[n - 1], beh[2]);//bef[n - 1]去掉最后一个时的最大公约数,beh[2]去掉第一个
		
		for (int i = 2; i < n; i++) 
		ans=max(ans, gcd(bef[i - 1], beh[i + 1]));//gcd(bef[i - 1], beh[i + 1])=除第 i 个数之外的 n-1 个数的 GCD  
		
    cout<<ans<<endl;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		solve();
	}
	return 0;
} 
	
  a[1]=4 a[2]=2 a[3]=1 a[4]=6 a[5]=8  
从前向后 bef[1]=a[1]=4

bef[2]=

gcd(bef[1], a[2])=2

bef[3]=

gcd(bef[2], a[3])=1

bef[4]=

gcd(bef[3], a[4])=1

bef[5]=

gcd(bef[4], a[5])=1

 
  beh[1]= gcd(beh[2], a[1])=1

beh[2]=

gcd(beh[3], a[2])=1

beh[3]=

gcd(beh[4], a[3])=1

beh[4]=

gcd(beh[5], a[4])=2

beh[5]=a[5]=8 从后向前
bef[n - 1]=bef[4]=1,去掉最后一个时的最大公约数
beh[2]=1,去掉第一个时的最大公约数
ans= max(bef[n - 1], beh[2])= max(bef[4], beh[2])=1
i=2, ans=max(ans, gcd(bef[i - 1], beh[i + 1]))=max(ans, gcd(bef[1], beh[3]))=max(1,1)=1去掉第2个时的最大公约数
i=3,ans=max(ans, gcd(bef[i - 1], beh[i + 1]))=max(ans, gcd(bef[2], beh[4]))=max(1,2)=2 去掉第3个时的最大公约数
i=4,ans=max(ans, gcd(bef[i - 1], beh[i + 1]))=max(ans, gcd(bef[3], beh[5]))=max(2,1)=1 去掉第4个时的最大公约数
 
去掉第3个数,其它数的最大gcd ,为2
 

H题

http://acm.hdu.edu.cn/showproblem.php?pid=2099

I题

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1014

J题 HDU 4007

n个点,能不能包含在R为边长的正方形里

猜你喜欢

转载自blog.csdn.net/tingtingyuan/article/details/81353970
8.2
今日推荐