A simple problem

来源:互联网 发布:斑马打印机软件 编辑:程序博客网 时间:2024/04/28 17:48
Problem Description
Zty很痴迷数学问题.。一天,yifenfei出了个数学题想难倒他,让他回答1 / n。但Zty却回答不了^_^. 请大家编程帮助他.

Input
第一行整数T,表示测试组数。后面T行,每行一个整数 n (1<=|n|<=10^5).

Output
输出1/n. (是循环小数的,只输出第一个循环节).

Sample Input
4237168
Sample Output
0.50.30.1428570.005952380
 
 
#include<iostream>
using namespace std;int f[100000];int hash[100000];int main(){    int t,m,y,cnt,n,i;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        if(n<0)        {            printf("-");            n*=-1;        }        if(n==1)        {            printf("1\n");            continue;        }        y=1;cnt=0;        memset(hash,0,sizeof(hash));        hash[1]=1;        while(y)        {            y*=10;            f[cnt++]=y/n;            y%=n;            if(hash[y])//重复出现同一个余数,即存在了循环节            break;            hash[y]=1;        }        printf("0.");        for(i=0;i<cnt;i++)        printf("%d",f[i]);        printf("\n");    }    return 0;}