Hduoj2607【位运算】

/*Let the Balloon Rise II 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 0   Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
Problem Description
Contest will be end at 17:00! How excited it is to see balloons floating around.

I knew you had solved the HDOJ 1004 Let the Balloon Rise already, so please settle the another version quickly. 
I have a lot of balloons and each has a color and I give each of them a number, same color has the same number. 
For example, red balloon is No.1, pink is No.2, black is No.3 . etc. I also have many rooms to store all the balloons.
There are some rules :
1. Every room stores several balloons but no two have the same color.
2. Collect all the balloons, we can find each color has even number of times of balloons except one.

Your task is to find which is the odd color and calculate its number of times.

Input
Input file consists from multiple data sets separated by one or more empty lines.
Each data set represents a sequence of 32-bit (positive) integers (references) which are stored in compressed way.
Each line of input set consists from three single space separated 32-bit (positive) integers X Y Z and they represent following sequence 
of No.X, X+Z, X+2*Z, X+3*Z, …, X+K*Z, …(while (X+K*Z)<=Y). This line represents that in this room there exists (K+1) balloons whose No. 
is No.X, No.X+Z, No.X+2*Z, No.X+3*Z, …, No.X+K*Z, …etc.

Output
For each input data set you should print to standard output new line of text with two integers separated by single space (first one is No. 
that occurs odd number of times and second one is count of that kind of balloon).
If all have even number of times output “None.”

Sample Input
1 10 1
1 10 1

1 5 1
6 10 1
1 10 1
4 4 1

1 5 1
2 5 1
2 5 1
2 5 1

Sample Output
None.
4 3
1 1

Author
WhereIsHeroFrom 
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest 
*/ 
#include<stdio.h>
#include<string.h>
int a[50010], b[50010], c[50010];
char s[100];
int main()
{
	int i = 0, j, k, n, m = 0, flag = 0;
	while(gets(s) != NULL)
	{
		if(s[0] >='0' && s[0] <= '9')
		{
			flag = 1;
			sscanf(s, "%d %d %d", &a[i], &b[i], &c[i]);
			for(j = a[i]; j <= b[i]; j += c[i])
			m ^= j;
			++i;
		}
		else//empty line
		{
			if(flag == 1)//printf
			{
				if(m == 0)
				printf("None.\n");
				else
				{
					n = i;
					k = 0;
					for(i = 0; i < n; ++i)
					{
						for(j = a[i]; j <= b[i]; j += c[i])
						if(j == m)
						k++;
					}
					printf("%d %d\n", m, k);
				}
				i = 0;
				m = 0;
				flag = 0;
			}
		}
	}
	if(flag == 1)//printf
	{
		if(m == 0)
		printf("None.\n");
		else
		{
			n = i;
			k = 0;
			for(i = 0; i < n; ++i)
			{
				for(j = a[i]; j <= b[i]; j += c[i])
				if(j == m)
				k++;
			}
			printf("%d %d\n", m, k);
		}
	}
	return 0;
}


题意:气球分为不同的颜色,每一种颜色都有一个标号,现在给出一组数据,每行三个数a,b,c,表示标号从a到b,每间隔c的都有一个标号(包括ab)。

当输入空行是输出标号为奇数个的标号以及它的个数。(题目提示:标号为奇数个的有且仅有一个,都则标号全部为偶数个)

思路:这题根据提示可以想到用异或运算去做,由于标号为偶数个的异或之后必为0,所以一旦出现标号为奇数个的,那么异或之后所保留的就是该奇数个的标号。剩下的就是去找该标号的个数。其次就是输入和输出:输入要求以空行为输出标志,并且在2组数据之间有可能有多个空行,要注意过滤,其次结束输入的标记即为文件结束符。

难点:此题的难点就在于要想到用异或运算符去做,其次就是输入和输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值