美文网首页
UVA 202 Repeating Decimals

UVA 202 Repeating Decimals

作者: iamkai | 来源:发表于2016-11-18 17:44 被阅读0次

    Problem

    https://uva.onlinejudge.org/external/2/202.pdf

    Thinking

    直式除法(詳細TODO)

    Solution

    #include<iostream>
    #include<cstdio>
    #include<string>
    using namespace std;
    
    
    void solve(int a,int b){
        
        int marked[3005] = {0};
        string str;
        
        //整數部分
        printf("%d/%d = %d.", a, b, a/b);
        a = a % b;
        marked[a] = 1;
        
        //小數部份
        int i;
        for(i = 2 ; ; i++)
        {
            a = a * 10;
            str.push_back(a/b + '0');
            a = a % b;
            
            if(marked[a])
                break;
           
            marked[a] = i;
        }
        
        int start = marked[a];
        int end = i;
    
        
        for(int i = 0 ; i < end-1 && i < 50 ; i++){
            if(i == start-1)
                printf("(");
        
            printf("%c",str[i]);
        }
        
        if(end-start >= 50)
            printf("...");
        
        printf(")\n");
        printf("   %d = number of digits in repeating cycle\n", end-start);
    }
    
    
    int main()
    {
        int a,b;
        while(scanf("%d%d",&a,&b) == 2)
        {
            solve(a,b);
            printf("\n");
        }
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:UVA 202 Repeating Decimals

          本文链接:https://www.haomeiwen.com/subject/oikspttx.html