zoukankan      html  css  js  c++  java
  • uva 202 Repeating Decimals

     Repeating Decimals 

    The decimal expansion of the fraction 1/33 is tex2html_wrap_inline43 , where the tex2html_wrap_inline45 is used to indicate that the cycle 03 repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number (fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have no such repeating cycles.

    Examples of decimal expansions of rational numbers and their repeating cycles are shown below. Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.

    tabular23

    Write a program that reads numerators and denominators of fractions and determines their repeating cycles.

    For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0 which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).

    Input

    Each line of the input file consists of an integer numerator, which is nonnegative, followed by an integer denominator, which is positive. None of the input integers exceeds 3000. End-of-file indicates the end of input.

    Output

    For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire repeating cycle.

    In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins - it will begin within the first 50 places - and place ``...)" after the 50th digit.

    Print a blank line after every test case.

    Sample Input

    76 25
    5 43
    1 397

    Sample Output

    76/25 = 3.04(0)
       1 = number of digits in repeating cycle
    
    5/43 = 0.(116279069767441860465)
       21 = number of digits in repeating cycle
    
    1/397 = 0.(00251889168765743073047858942065491183879093198992...)
       99 = number of digits in repeating cycle
    注意换行,注意空格,注意50个数字,一切安好。
    附上AC代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int str[10000];
     5 int sh[10000];
     6 int a,b;
     7 int head;
     8 int m,n;
     9 int Min;
    10 int i;
    11 int rex,rey;
    12 int flag;
    13 int c;
    14 int gcd(int a,int b)
    15 {
    16     int r;
    17     while((r=b%a)!=0)
    18     {
    19         b=a;
    20         a=r;
    21     }
    22     return a;
    23 }
    24 int main()
    25 {
    26     while(scanf("%d%d",&a,&b)!=EOF)
    27     {
    28         head=a/b;
    29         Min=gcd(a,b);
    30         m=a/Min;
    31         n=b/Min;
    32         sh[0]=m%n;
    33         i=0;
    34         flag=0;
    35         printf("%d/%d = %d.",a,b,head);
    36         while(!flag)
    37         {
    38             c=sh[i]*10;
    39             str[i]=c/n;
    40             sh[++i]=c%n;
    41             for(int j=0;j<i;j++)
    42                 if(sh[i]==sh[j])
    43             {
    44                 rex=j;
    45                 rey=i;
    46                 flag=1;
    47                 break;
    48             }
    49         }
    50         for(int i=0;i<rex;i++)
    51             printf("%d",str[i]);
    52         printf("(");
    53         if(rey-rex<=50)
    54         {
    55             for(int i=rex;i<rey;i++)
    56                 printf("%d",str[i]);
    57         }
    58         else
    59         {
    60             for(int i=rex;i<rex+50;i++)
    61                 printf("%d",str[i]);
    62             printf("...");
    63         }
    64         printf(")
    ");
    65         printf("   %d = number of digits in repeating cycle
    
    ",rey-rex);
    66     }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    BadUSB 利用
    java 将函数作为参数传递
    odoo12 修行提升篇之 常用的高阶函数 (二)
    odoo12 修行提升篇之 异步定时任务 (一)
    odoo12 修行基础篇之 利用kanban做分析 点击跳转分析模型列表 (九)
    odoo12 修行基础篇之 kanban (八)
    odoo12 修行基础篇之 记录批处理 (七)
    odoo12 修行基础篇之 列表的筛选和分组 (六)
    odoo12 修行基础篇之 添加记录编码 (五)
    odoo12 修行基础篇之 添加工作流和操作记录 (四)
  • 原文地址:https://www.cnblogs.com/I-love-HLD/p/4201640.html
Copyright © 2011-2022 走看看