南邮 OJ 1519 H_TRC1

H_TRC1

时间限制(普通/Java) :  2000 MS/ 6000 MS          运行内存限制 : 65536 KByte
总提交 : 41            测试通过 : 12 

比赛描述

The ternary expansion of a number is that numberwritten in base 3. A number can have more than oneternary expansion. A ternary expansion is indicatedwith a subscript 3. For example, 1 = 1
= 0.222... 3,and 0.875 = 0.212121...
3.The Cantor set is defined as the real numbersbetween 0 and 1 inclusive that have a ternaryexpansion that does not contain a 1. If a number hasmore than one ternary expansion, it is enough for asingle one to not contain a 1.For example, 0 = 0.000...
and 1 = 0.222... 3, sothey are in the Cantor set. But 0.875 = 0.212121...
3and this is its only ternary expansion, so it is notin the Cantor set.Your task is to determine whether a given number is in the Cantor set.



输入

The input consists of several test cases.Each test case consists of a single line containing a number written in decimalnotation, with 0 <= <= 1, and having at most 6 digits after the decimal point.The last line of input is END. This is not a test case.

输出

For each test case, output MEMBER if is in the Cantor set, and NON-MEMBER if isnot in the Cantor set.

样例输入

01
0.875
END

样例输出

MEMBER
MEMBER
NON-MEMBER

提示

undefined

题目来源

Waterloo local contest





/* Wrong Answer at Test 2
#include<iostream>
#include<set>
using namespace std;

int main(){
	double d;
	bool inCantor;
	int i;
	set<double> ds;
	while(scanf("%lf",&d)==1){
		ds.clear();
		inCantor = 1;
		if(d<0 || d>1){
			inCantor = 0;
		}else if(d==0 || d==1){
			inCantor = 1;
		}else{
			while(1){
				d *= 3;
				i = (int)d;
				if(i==1){
					inCantor = 0;
					break;
				}
				d -= i;
				if(ds.count(d)){
					inCantor = 1;
					break;
				}else{
					ds.insert(d);
				}
			}
		}
		if(inCantor){
			printf("MEMBER\n");
		}else{
			printf("NON-MEMBER\n");
		}
	}
}
*/

/* Wrong Answer at Test 2
#include<iostream>
#include<set>
using namespace std;

int main(){
	double d;
	set<double> ds;
	while(scanf("%lf",&d)==1){
		if(d==0 || d==1){
			printf("MEMBER\n");
		}else{						//0.020202……
			printf("NON-MEMBER\n");
		}
	}
}
*/



/* AC 9MS
#include<iostream>
#include<set>
using namespace std;

int main(){
	double d;
	bool inCantor;
	int n,i;
	set<int> is;
	while(scanf("%lf",&d)==1){//有限字长效应,输入0.7会变成0.69999999999999996
		is.clear();
		inCantor = 1;
		n = (int)(d*1000000+0.5);//扩大1000000倍,四舍五入
		if(n<0 || n>1000000){
			inCantor = 0;
		}else if(n==0 || n==1000000){
			inCantor = 1;
		}else{
			while(1){
				n *= 3;
				i = n/1000000;
				n -= i*1000000;
				if(i==1){
					inCantor = 0;
					break;
				}
				if(is.count(n)){
					inCantor = 1;
					break;
				}else{
					is.insert(n);
				}
			}
		}
		if(inCantor){
			printf("MEMBER\n");
		}else{
			printf("NON-MEMBER\n");
		}
	}
}
*/

// AC 6MS
#include<iostream>
#include<set>
using namespace std;

bool b[1000000];

int main(){
	double d;
	bool inCantor;
	int n,i;
	while(scanf("%lf",&d)==1){//有限字长效应,输入0.7会变成0.69999999999999996
		memset(b,0,sizeof(b));
		inCantor = 1;
		n = (int)(d*1000000+0.5);//扩大1000000倍,四舍五入
		if(n<0 || n>1000000){
			inCantor = 0;
		}else if(n==0 || n==1000000){
			inCantor = 1;
		}else{
			while(1){
				n *= 3;
				i = n/1000000;
				n -= i*1000000;
				if(i==1){
					inCantor = 0;
					break;
				}
				if(b[n]){
					inCantor = 1;
					break;
				}else{
					b[n] = 1;
				}
			}
		}
		if(inCantor){
			printf("MEMBER\n");
		}else{
			printf("NON-MEMBER\n");
		}
	}
}






你可能感兴趣的:(ACM,南邮OJ,H_TRC1)