H - Hamming Code URAL - 1792

题目
Let us consider four disks intersecting as in the figure. Each of the three shapes formed by the intersection of three disks will be called a petal.
Write zero or one on each of the disks. Then write on each petal the remainder in the division by two of the sum of integers on the disks that contain this petal.

For example, if there were the integers 0, 1, 0, and 1 written on the disks, then the integers written on the petals will be 0, 1, and 0 (the disks and petals are given in the order shown in the figure).

This scheme is called a Hamming code. It has an interesting property: if you enemy changes secretely any of the seven integers, you can determine uniquely which integer has been changed. Solve this problem and you will know how this can be done.
Problem illustration
在这里插入图片描述

Input
The only line contains seven integers separated with a space, each of them being zero or one. The first four integers are those written on the disks in the order shown in the figure. The following three integers are those written on the petals in the order shown in the figure
Output
Output one line containing seven integers separated with a space. The integers must form a Hamming code. The set of integers may differ from the input set by one integer at most. It is guaranteed that either the input set is a Hamming code or a Hamming code can be obtained from it by changing exactly one integer.
Example
题目大意:
一共七个区域
在这里插入图片描述
如果1 3 4区域的数相加%2!=6区域,那就不是汉明码;
1 2 4
2 3 4 同理;
(本质是二进制)
题目重点理解
For example, if there were the integers 0, 1, 0, and 1 written on the disks, then the integers written on the petals will be 0, 1, and 0 (the disks and petals are given in the order shown in the figure).

#include<iostream>
using namespace std;
int a[20];
bool judge(int a,int b,int c,int d,int e,int f,int g)
{
	if((a+c+d)%2!=f) return false;
	if((a+b+d)%2!=g) return false;
	if((b+c+d)%2!=e) return false;
	return true;
}
int main()
{
	
  int i;
  for(i=1;i<=7;i++)
  {
  	cin>>a[i];
  }
  if(judge(a[1],a[2],a[3],a[4],a[5],a[6],a[7])!=0)
  {
	  for(i=1;i<=7;i++)
	  {
	  	if(i>1)
	  	cout<<" ";
	  	cout<<a[i];
	  }
	  
  }
  	for(i=1;i<=7;i++)
  	{
  	  a[i]=1^a[i];
	  if(judge(a[1],a[2],a[3],a[4],a[5],a[6],a[7])!=0)	
	  {
	    cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7];
	    break;
	  }
	  a[i]=1^a[i];
	}
  
	return 0;
 } 

新学会的 ^
这个是异或
1^1=0
0^0=0
1^0=1
0^1=1
相同为0
不同为1
我忘记是不是离散数学提到的了0.0

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值