好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。

知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好)

此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。

按照除法的机制,用一个函数表示出来就可以了,代码如下:

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>

#define eps 1e-9
#define N 100005
#define P system("pause")
using namespace std;
int hash[N];
void div(int x)
{
    int left=1; 
    hash[1]=1;
    while(left)      
    {
        left*=10;
        printf("%d",left/x);
        left%=x;
        if(hash[left]) break;
        hash[left]=1;                 
    }
}


int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);cc
    int n,x;
    scanf("%d",&n);
    while(n--){
        scanf("%d",&x);
        if(x==1) {printf("1\n");continue;}
        if(x<0) {printf("-");x = 0-x;}
        printf("0.");
        memset(hash,0,sizeof(hash));
        div(x);
        printf("\n");
    
    
    
               
    }                     

 //   P;                               
    return 0;    
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值