hdu2522 A simple problem(循环节)

本文详细介绍了如何通过除法机制和哈希表来识别无限不循环小数(无理数)的循环节特性,特别关注分母与循环节位数之间的关系,提供了一种有效的方法来解决数学中的循环节问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做这题前要确定两件事:

1、无限不循环小数即无理数(无循环节),不能写成两整数之比。所以1/n的形式必定有循环节。

2、一个分母为n的循环节位数最多不超过n - 1。


接着就可以用除法机制打哈希表,注意保存的并非余数而是n - 1个数中已经出现过的数。



#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;

typedef long long LL;

const int N = 100005;
const int INF = 1e8;

int main()
{
  //  freopen("in.txt", "r", stdin);
    int t, n, vis[N];
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d", &n);
        if(n < 0)
        {
            printf("-");
            n *= -1;
        }
        if(n == 1)
        {
            printf("1\n");
            continue;
        }
        memset(vis, 0, sizeof(vis));
        int mod = 1;
        vis[mod] = 1;
        printf("0.");
        while(mod)
        {
            mod *= 10;
            printf("%d", mod / n);
            mod %= n;
            if(vis[mod]) break;
            vis[mod] = 1;
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值