现在的位置: 首页 > 综合 > 正文

13/05/12 HDU chapter one 简单题

2012年11月27日 ⁄ 综合 ⁄ 共 613字 ⁄ 字号 评论关闭

1.2.6 find your present(2)

hdu 2095

用位运算 ^    =>    1^3^7^3^1 = 7

#include<iostream>
using namespace std;
int main()
{
    int n,s,a;
    while(scanf("%d",&n)&&n) 
    {
        s = 0;
        while(n--) 
        {
            cin>>a;
            s ^= a;
        }
        cout<<s<<endl;
    }
    return 0;
}

1.3.1 moving tables

hdu 1050:

可以用贪心来做;

按房间开始号排序;

提供另一种的方法:

使用一个数组 id[ ]存放房间在走廊的位置(房间号)被经历的次数;

n对应的房间号是 (n+1)/2

#include<iostream>
using namespace std;
int main()
{  
	int M;
	cin>>M;
	while(M--)
	{
		int i,n,k,l;              //l个需要搬动的桌子,起始n,中止k
		cin>>l;
		int id[201]={0};//走廊号 
		while(l--)
		{
			cin>>n>>k;
			if(n>k)//交换小值n在前
			{
				n = n ^ k;
				k = n ^ k; 
				n = n ^ k;
			}             
		   
 			for(i=(n+1)/2;i<=(k+1)/2;i++)  //奇数偶数全加一然后除以2就是走廊号。
				id[i]++;		               
		}       
		int max=0;
		for(i=0;i<201;i++)//找最大值
 		{
		 	max = (max<id[i])?id[i]:max;
		}	        
		cout<<max*10<<endl;  
	}
	return 0;
}

抱歉!评论已关闭.