Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哈哈,自己用位运算解出来了,贴一下

Posted by Codievilky at 2012-08-07 15:17:57 on Problem 1013
In Reply To:用位运算应该会很简单,可是不会啊 Posted by:Codievilky at 2012-08-07 09:21:22
哈哈,自己用位运算解出来了,贴一下
#include<iostream>
using namespace std;
int main(){
	int n=0;	
	
	scanf("%d",&n);
	for(int i=0;i<12;i++){
		int tip[3]={0xfff,0xfff,0xfff},judgement=0,b[3]={0},left=0,right=0;
		int Tleft[3]={0},Tright[3]={0};
		char weight[6]={0};
		for(int m=0;m<3;m++){
			char left[8],right[8],balance[6];
			scanf("%s %s %s",left,right,balance);
			if(strcmp(balance,"even")!=0){
				tip[m]=0;
				for(int p=0;p<12;p++){
					if(left[p]!=0){
						Tleft[m]=Tleft[m]|(1<<(left[p]-'A'));
						Tright[m]=Tright[m]|(1<<(right[p]-'A'));
						tip[m]=Tleft[m]|tip[m]|Tright[m];
					}
					else break;
				}
				if(strcmp(balance,"up")==0)
					b[m]=1;
				else 
					b[m]=-1;
			}
			else{
				for(int p=0;p<12;p++){
					if(left[p]!=0)
						judgement=judgement|(1<<(left[p]-'A'))|(1<<(right[p]-'A'));
					else break;
				}		
			}
		}
		if(tip[0]==0xfff&&tip[1]==0xfff&&tip[2]==0xfff){
			int templeft=~0,tempright=~0;
			for(int u=0;u<3;u++){
				if(b[u]!=0){
				templeft=templeft&Tleft[u];
				tempright=tempright&Tright[u];
				}
			}
			for(int u=0;u<3;u++){
				if(b[u]!=0){
				Tleft[u]=Tleft[u]&(~templeft);
				Tright[u]&=(~tempright);
				}
			}
			if(judgement!=0){
				//tip[0]=tip[0]&(~judgement);
				for(int u=0;u<3;u++){
					if(b[u]!=0){
						Tleft[u]=Tleft[u]&(~judgement);
						Tright[u]&=(~judgement);
						if(Tleft[u]==0){
							b[u]++;
							tip[0]=Tright[u];
						}
						else if(Tright[u]==0){
							b[u]--;
							tip[0]=Tleft[u];
						}
						if(b[u]!=0)
							strcpy(weight,"light");
						else 
							strcpy(weight,"heavy");
						break;
					}
				}
			}
			else{
			tip[0]=~0;			
			int temp=b[0]+b[1]+b[2];
			left=~0;right=~0;
			for(int u=0;u<3;u++){				
				if(b[u]==temp){					
					left=Tleft[u]&left;
					right=Tright[u]&right;
				}
			}
			tip[0]=left|right;
			for(int u=0;u<3;u++){
				if(Tleft[u]==tip[0]||Tright[u]==tip[0]){
				if(Tleft[u]==tip[0])
					b[u]++;
				else if(Tright[u]==tip[0])
					b[u]--;
				if(b[u]==0)
					strcpy(weight,"light");
				else 
					strcpy(weight,"heavy");
				}
			}	
			}
		}
		else{						
		tip[0]=tip[0]&tip[1]&tip[2];
		tip[0]=tip[0]&(~judgement);
		int wei=0;
		for(int m=0;m<3;m++){
			
			if(b[m]==1||b[m]==-1){
				int temp=b[m];
				left=0;right=0;
				left=Tleft[m];	
				right=Tright[m];
				left=left&tip[0];
				right=right&tip[0];
				if(left>0&&right>0)
					continue;
				else if(left==0) 
					temp--;					
				else if(right==0) 
					temp++;	
				if(temp==0){
					strcpy(weight,"light");
					wei=-1;
				}
				else {
					strcpy(weight,"heavy");
					wei=1;
				}
			}
		}
		for(int m=0;m<3;m++){
			if(b[m]==1||b[m]==-1){
				int temp=b[m];
				left=0;right=0;
				left=Tleft[m];	
				right=Tright[m];
				left=left&tip[0];
				right=right&tip[0];
				if(left>0&&right>0){
					temp=temp+wei;
					if(temp==0)
						tip[0]=right&tip[0];
					else
						tip[0]=left&tip[0];
				}
			}
		}
		}
		int move=0;
		while((1<<move)!=tip[0]){
			move++;
		}
		char anwser='A'+move;
		printf("%c is the counterfeit coin and it is %s. \n",anwser,weight);		
	}
	return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator