码迷,mamicode.com
首页 > 其他好文 > 详细

数学小技巧(模拟)求1/n

时间:2014-12-11 22:30:44      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   sp   strong   on   2014   

求1/n(是循环小数只输出第一个循环节)

思路:模拟求余

http://acm.nyist.net/JudgeOnline/problem.php?pid=330

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int vis[100005];
int main()
{
    int d,n;
    cin>>d;
    while(d--)
    {
        cin>>n;
        if(n==1)
            printf("1\n");
        if(n<0)
        {
            printf("-0.");
            n=-n;
        }
        else
            cout<<"0.";
        memset(vis,0,sizeof(vis));
        int t=1;
        while(!vis[t]&&t!=0)
        {
            vis[t]=1;
            t*=10;
            printf("%d",t/n);
            t%=n;
        }
        cout<<endl;
    }
}


数学小技巧(模拟)求1/n

标签:style   blog   http   io   os   sp   strong   on   2014   

原文地址:http://blog.csdn.net/u012349696/article/details/41872849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!