uva 275 Expanding Fractions

    xiaoxiao2021-03-25  103

    题目:给你一个分数的分子和分母,求出他的循环节。

    Sample Input

    3 7

    345 800

    112 990

    53 122

    0 0

    Sample Output

    .428571 The last 6 digits repeat forever. .43125 This expansion terminates. .113 The last 2 digits repeat forever. .4344262295081967213114754098360655737704918032786 885245901639 The last 60 digits repeat forever.

    #include<cstdio>

    #include<cstring>

    #include<stack>

    #include<iostream>

    using namespace std;

    #define M 3000

    int main()

    {

    int a,b;

    a = b = 0;

    while((cin >> a >> b)&& a+b){

    printf(".");

    int yushu[M] = {0},shang[M] = {0};

    ///     memset(shang, 0, 30);

    a %= b;

    a *= 10;

    for(int t = 0; t < M; t++) {

    shang[t] = a/b;

    a = a%b;

    yushu[t] = a;

    a *= 10;

    }

    int i ,j;

    for(i = 0; i < M; i++){

    for(j = i + 1; j < M; j++) {

    if(((shang[j] == shang[i]) && (yushu[j] == yushu[i]) )|| (i >= 100)){

    goto a1;

    }

    }

    }

    a1:

    if((shang[j] == 0) && ((j-i)==1)){

    for(int m = 0; m < j-1; m++){

    if(m % 50 == 49) printf("\n");

    printf("%d",shang[m]);

    }

    printf("\nThis expansion terminates.\n\n");

    }

    else{

    for(int m = 0; m < j; m++){

    if(m % 50 == 49) printf("\n");

    printf("%d",shang[m]);

    }

    printf("\n");

    printf("The last %d digits repeat forever.\n\n",j-i);

    }

    }

    return 0;

    }

    1.输入0 0 停止,以后都可以写成 ------- while((cin >> a >> b)&& a+b) 2. 刚开始总是提示格式错误。后来发现。原因在与。每输入50个字符就要换行。只考虑了第一行。 这里有一个非常有趣的细节。因为题目要求了在第一行中。小数点也算一个字符。所以第一行只需要输出49个。剩下的需要输出50个字符。那怎么样可以统一的概括呢。你可能想到分类讨论。但其实 用这个语句  if(m % 50 == 49) printf("\n");  即可。※※前方高能➡️注意把printf("%d",shang[m]);放在这句话后面。那么第一行。即i=49的那个值还没有来的及输出就换行了。但后面都对。如果把这句话放在前面。那没就是很整齐的每一行都输出数组中50个元素
    转载请注明原文地址: https://ju.6miu.com/read-21519.html

    最新回复(0)