UVA11701 Cantor【水题】

The ternary expansion of a number is that number written in base 3. A number can have more than one ternary expansion. A ternary expansion is indicated with a subscript 3. For example, 1 = 13 = 0.222 . . .3, and 0.875 = 0.212121 . . .3.
    The Cantor set is defined as the real numbers between 0 and 1 inclusive that have a ternary expansion that does not contain a 1. If a number has more than one ternary expansion, it is enough for a single one to not contain a 1.
    For example, 0 = 0.000 . . .3 and 1 = 0.222 . . .3, so they are in the Cantor set. But 0.875 = 0.212121 . . .3 and this is its only ternary expansion, so it is not in the Cantor set.
    Your task is to determine whether a given number is in the Cantor set.
在这里插入图片描述
Input
The input consists of several test cases.
    Each test case consists of a single line containing a number x written in decimal notation, with 0 ≤ x ≤ 1, and having at
most 6 digits after the decimal point.
    The last line of input is ‘END’. This is not a test case.
Output
For each test case, output ‘MEMBER’ if x is in the Cantor set, and ‘NON-MEMBER’ if x is not in the Cantor set.
Sample Input
0
1
0.875
END
Sample Output
MEMBER
MEMBER
NON-MEMBER

问题链接UVA11701 Cantor
问题简述:给定一个0~1的十进制小数,判定它转换成三进制小数后小数点后是否含有数字1,有的话输出NON-MEMBER,否则输出MEMBER。
问题分析:水题不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11701 Cantor */

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6;
bool f[N];

int main()
{
    double d;
    while(scanf("%lf", &d) == 1) {      // 读到"END"会结束循环
        memset(f, false, sizeof(f));

        bool cantor = 1;
        int n = d * N + 0.5, k;
        if(n < 0 || n > N) cantor = 0;
        else if(n == 0 || n == N) cantor = 1;
        else {
            for(;;) {
                n *= 3;
                if((k = n / N) == 1) {
                    cantor = 0;
                    break;
                }
                if(f[n -= k * N]) {
                    cantor = 1;
                    break;
                } else
                    f[n] = true;
            }
        }

        printf(cantor ? "MEMBER\n" : "NON-MEMBER\n");
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105595014