HDU--杭电--2522--A simple problem--数学基础

A simple problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2795    Accepted Submission(s): 984


Problem Description
Zty很痴迷数学问题.。一天,yifenfei出了个数学题想难倒他,让他回答1 / n。但Zty却回答不了^_^. 请大家编程帮助他.
 

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

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

Sample Input

4 2 3 7 168
 

Sample Output

0.5 0.3 0.142857 0.005952380
 
#include 
#include 
using namespace std;
int hash[111111];
int main (void)
{
    int t,n,m,i,j,k,l;
    cin>>t;
    while(t--&&cin>>n)
    {
        if(n<0)cout<<"-",n=-n;	//有负号就输出并把n转正
        if(n==1){cout<<"1"<<endl;continue;}	//碰上1就特解它
        for(i=0;i<111111;i++)hash[i]=0;	//哈希表初始化
        cout<<"0.";
        k=10;hash[1]=1;	//一开始1/n就会余1
        while(k>0)	//这里就是模拟草稿纸计算除法的过程
        {
            l=k/n;
            k%=n;
            printf("%d",l);
            if(hash[k])break;	//碰到余数相同就找完循环节末端了
            hash[k]=1;
            k*=10;
        }
        cout<<endl;
    }
    return 0;
}


你可能感兴趣的:(数学题)