`

南阳理工OJ 一个简单的数学题 模拟除法,找循环节

 
阅读更多

连接:  http://acm.nyist.net/JudgeOnline/problem.php?pid=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

 

#include<stdio.h>
#include<string.h>
bool p[1000001];
int main()
{
       int T,n,term;
       scanf("%d",&T);
       while(T--)
       {
              memset(p,0,sizeof(p));
              term=10;
              scanf("%d",&n);
              if(n<0)
              {
                     printf("-");
                     n=-n;
              }
              if(n==1)
              {
                     printf("1\n");
                     continue;
              }
              printf("0.");
              while(p[term]==false)
              {
                     printf("%d",term/n);
                     p[term]=true;
                     term=(term%n)*10;
                     if(term==0)break;
              }
              printf("\n");
       }
       return 0;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics