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): 1135Problem Description Zty很痴迷数学问题.。

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);
    }
}


目录
相关文章
PAT (Advanced Level) Practice:1~3题
​ ✨欢迎您的订阅✨ PAT乙级专栏(更新完毕): 👉🏻【Basic Level】👈🏻 PAT甲级专栏(更新ing): 👉🏻【Advanced Level】👈🏻 ​
PAT (Advanced Level) Practice:1~3题
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
93 0
Data Structures and Algorithms (English) - 7-10 Saving James Bond - Easy Version(25 分)
Data Structures and Algorithms (English) - 7-10 Saving James Bond - Easy Version(25 分)
91 0
Data Structures and Algorithms (English) - 7-11 Saving James Bond - Hard Version(30 分)
Data Structures and Algorithms (English) - 7-11 Saving James Bond - Hard Version(30 分)
175 0
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
118 0
|
供应链
PAT (Advanced Level) Practice - 1079 Total Sales of Supply Chain(25 分)
PAT (Advanced Level) Practice - 1079 Total Sales of Supply Chain(25 分)
138 0
PAT (Advanced Level) Practice - 1112 Stucked Keyboard(20 分)
PAT (Advanced Level) Practice - 1112 Stucked Keyboard(20 分)
128 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
114 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
99 0
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
121 0