| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1475 人关注过本帖
标题:请问如何输出1/n
只看楼主 加入收藏
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
结帖率:88.89%
收藏
已结贴  问题点数:20 回复次数:6 
请问如何输出1/n
“OraOraOraOra...” It’s painful for Jotaro to repeat, also for SHP to recommend JoJo’s Bizarre Adventure series to Quasrain. To prove JoJo is fascinating, SHP wants to do it by  proving this problem is not hard for an oceanology doctor or a freshman like you.

Jotaro found for a fraction, it’s easy to work out it when the answer has limited digits, which is called finite decimal. However, for an answer like 1/3 and an answer like 33...3 (100 3s) / 100... (100 0s), it’s hard for him to tell them apart. So, he decided to let you design a program to mark the accurate answer. To make the task easy for you to understand and complete, we simplify the problem as follows: for a fraction in the form of 1/x, in which x is a positive integer, output it correctly. If you solve this problem, Quasrain will be willing to watch JoJo, which is exactly what SHP wants.
Input

Multiple test cases. Read to the end of file. Every line contains a positive integer n<=4000. Test cases are no more than 4000.
Output
Output the answer of 1/n in the following format.

For a finite decimal, output it completely without trailing zeros. For an infinite decimal, you don’t need to output it with certain constant number of digits like %.8f, %.10f, ... Please output it in the form of “0.xxxx(xxx...x)”, digits in the brackets standing for the shortest circulating part.  See sample for more detail.
Sample Input
1
3
4
9
10
28

Sample Output
1.0
0.(3)
0.25
0.(1)
0.1
0.03(571428)
Hint

Jotaro’s math is poor, but yours is better if you solve this problem.
Submit
Discuss
Time limit : 1 s
Memory limit : 512 mb
Submitted : 227
Accepted : 32
64bit Integer Format : %lld

搜索更多相关主题的帖子: the answer output problem for 
2020-11-01 15:39
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:20 
很难,以 28 为例

28 中有两个因子2(2*2*7),而除法过程中余数不停的乘以10(2*5),所以先得将n中的因子2和因子5的数目分别算出来,取其数目的最大值,对于28而言就是2个2和0个5
最多2个2和0个5,那么10^2就可以整除它,所以将 1/28 换成 1/28 = (100/28)/100 = (3 + 16/28)/100 = (3 + 4*1/7)/100
1/28 = 转变为 (3 + 4*1/7)/100 的目的是 去除28中所有的因子2和因子5
1/7 的循环节长度是 6,这个倒是简单
但假如不是 1/7 而是 1/(7*7*11*11*11) 呢?得先求 欧拉函数值,再求 欧拉函数值 的因子,再从小到大一个个测试。难难难,烦烦烦!
2020-11-01 19:17
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:0 
题目好像不需要求循环节的长度,那简单多了
先剔除n中的因子2和因子5,也就是 1/28  转变为 (3 + 4/7)/100, 这一步不变。
(3 + 4/7)/100 就是 0.03 + XXXXXX
XXXXXX 不求循环节长度的话,倒是简单:
4/7 先是 0 余 4
4*10/7 是 5 余 5
5*10/7 是 7 余 1
1*10/7 是 1 余 3
3*10/7 是 4 余 2
2*10/7 是 2 余 6
6*10/7 是 8 余 4 --------- 到此结束,因为余数跟一开始的“4”等同了,所以循环节就是 571428
2020-11-01 19:30
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <stdio.h>

void foo( unsigned n )
{
    if( n == 1 )
    {
        puts( "1.0" );
        return;
    }
    printf( "0." );

    unsigned a = 1; // 分子
    unsigned b = n; // 分母
    unsigned c = 0; // 结束时,分子应当等于此值

    for( ; a!=0; ) // 计算非循环部分
    {
        unsigned base = 10;
        if( b%2 == 0 )
        {
            base /= 2;
            b /= 2;
        }
        if( b%5 == 0 )
        {
            base /= 5;
            b /= 5;
        }

        if( base == 10 ) // 当分母中不存在因子2或因子5时,非循环部分结束
        {
            putchar( '(' );
            putchar( a*base/b + '0' );
            c = a;
            a = a*base%b;
            break;
        }

        putchar( a*base/b + '0' );
        a = a*base%b;
    }

    for( ; a!=c; ) // 计算循环部分
    {
        putchar( a*10/b + '0' );
        a = a*10%b;
    }
    if( c != 0 )
        putchar( ')' );
    putchar( '\n' );
}

int main( void )
{
    unsigned n;
    scanf( "%u", &n );
    foo( n );
}
2020-11-02 10:24
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
谢谢谢谢谢谢 判断余数循环卡我 补充一下 这题还是多组数据哈哈
2020-11-02 10:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:0 
以下是引用xiaohuo66在2020-11-2 10:49:56的发言:

补充一下 这题还是多组数据哈哈

那将 main 函数改为
程序代码:
int main( void )
{
    for( unsigned n; scanf("%u",&n)==1; )
        foo( n );
}
2020-11-02 11:21
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
谢谢 才看到 我用while写的 感谢大佬
2020-11-02 14:44
快速回复:请问如何输出1/n
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020554 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved