A simple problem

A simple problem


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<stdio.h>
#include<string.h>
#define maxn 100002
int vis[maxn];
int main()
{
    int n,t,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n<0)
        {
            printf("-");
            n=-n;
        }
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        memset(vis,0,sizeof(vis));
        printf("0.");
        m=1;
        vis[0]=1;
        while(!vis[m])
        {
            vis[m]=1;
            m*=10;
            printf("%d",m/n);
            m%=n;
        }
        printf("\n");
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章