nyoj 330 一个简单的数学题

 

一个简单的数学题

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述
zyc最近迷上了数学,一天,dj想出了一道数学题来难住他。算出1/n,但zyc一时答不上来希望大家能编程帮助他。
 
输入
第一行整数T,表示测试组数。后面T行,每行一个整数 n (1<=|n|<=10^5).
输出
输出1/n. (是循环小数的,只输出第一个循环节).
样例输入
4
2
3
7
168
样例输出
0.5
0.3
0.142857
0.005952380
来源
hdu
上传者
路过这
直接模拟求余数
用一个数组进行标记就行了
View Code
 
/*********************************
/   Problem:
/   Algorithm:
/   Language:   C++
/   Compiler:   MinGW
/   Date:       12/08/08
/
/   Copyright (C) wujianwei
/   All rights reserved.
********************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>

using namespace std;

#define INF 0x7fffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 100005
const int MAX=1<<28;
typedef long long LL;
//typedef __int64 INT

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int s[N],vist[N];
        int n;
        scanf("%d",&n);
        int cnt=0;
        if(n<0) printf("-"),n=-n;
        if(n==1) printf("1\n");
        else
        {
            int t=1;
            memset(vist,0,sizeof(vist));
            vist[t]=1;
            while(t)
            {
                t*=10;
                s[cnt++]=t/n;
                t%=n;
                if(vist[t]) break;
                vist[t]=1;
            }
            printf("0.");
            for(int i=0;i<cnt;i++)
                printf("%d",s[i]);
                printf("\n");
        }
    }
    return 0;
}

        
原文地址:https://www.cnblogs.com/wujianwei/p/2637046.html