hd2522A simple problem

A simple problem

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

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<cstdio>
#include<cstring>
int a[100001];
void f(int n)
{
    int t,k;
    if(n<0)
    {
        printf("-");
        n=-n;
    }
    if(n==1)
    {
        printf("1");
    }
    else
    {
        printf("0.");
        t=1;
        a[1]=1;//余数为1
        while(t)
        {
            t*=10;
            printf("%d",t/n);
            t%=n;
            if(a[t]!=0)//记录余数是否出现过
            {
                break;
            }
            a[t]=1;
        }
    }
    printf("\n");
}
int main()
{
    int n,T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        f(n);
    }
}
时间: 2024-09-17 22:40:19

hd2522A simple problem的相关文章

【北大夏令营笔记-线段树】POJ3468-A Simple Problem with Integers

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57993 Accepted: 17658 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of ope

hdu 5349 MZL&amp;#39;s simple problem

hdu 5349 的传送门 Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the

poj 2535 Very Simple Problem

开始题意搞错了,一直以为是最简单的问题才行... 后来看discuss才发现只要简单就可以了,里面看到一个很不错的题意解释... 问题: n个人给p道题打分,一道题是最容易题的条件: 该题被评为最简单的次数要过半,而且该题没有被任何评委评为最难. 方法: 可设置3个数组,数组A用来读入数据,数组B纪录对应的题目是否被 打成"最难"的了, 数组C纪录该道题被评委打成"最简单"的次数.当然 这个"最难"和"最简单"只是针对这个评委

poj 3468 A Simple Problem with Integers

点击打开链接poj 3468 思路:线段树成段更新 分析: 1 最基础的线段树的成段更新的题目,我们只要建好线段树然后进行更新即可 2 注意由于输入的数最大为10^9,因此我们应该使用long long,区间的和已经区间的延时标记都要为long long 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; type

poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)

/* 树状数组第三种模板(改段求段)不解释! 不明白的点这里:here! */ #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define N 100005 using namespace std; typedef long long LL; LL ss[N], B[N], C[N]; int n, m; void addB(int x, int k){/

【JAVA大数训练】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 196702    Accepted Submission(s): 37626 Problem Description I have a very simple problem for you. Given two integers A and B, yo

[ACMcoder] A + B Problem II

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

HDOJ 1002 A + B Problem II

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

HDOJ1002题A + B Problem II,2个大数相加

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol