繁体   English   中英

如何用for循环计数器替换scanf?

[英]how can i replace scanf with for loop counter?

我正在编写一个程序,以便在没有math.h库的情况下计算所有跟腱数。 在此程序中,首先我计算大数,然后再计算大数的GCD。 如果GCD为1,则当前数字为阿喀琉斯。

我的问题是我的程序可以使用scanf()但不能使用for循环计数器! 我做错了什么?

非常感谢你!

#include <stdio.h>
#define MAX 1000

int main(void)
{
    int n;
    int i, j, a;
    int counter2 = 0;
    int large;
    int small;
    int rem, gcd, max = 1, min = 1;
    int achilles;

    for (a = 1; a <= MAX; a++) //for loop for counter
    {
        n=a;

        for (i = 1; i <= n; i++)
        {
            int count = 0;
            for (j = 1; j <= i; j++)
            {
                if (i % j == 0)
                {
                    count++;
                }
            }
            int l = 0;

            if (count == 2)
            {
                while (n % i == 0) // calculate factor and his exponent
                {
                    l++;
                    n = n / i;
                }

                if (l > max) // calculates min and max in order to find GCD
                {
                    max = l;
                }
                if (l < min)
                {
                    min = l;
                }
            }

            large = max;
            small = min;
            while (small) { // While small is not 0
                            // Calculates GCD
                rem = large % small;
                large = small;
                small = rem;
            }
            gcd = large;
            // printf("GCD(%d,%d)= %d", large, small, gcd);
        }
        if (gcd == 1) {
            achilles = n;
            // printf("%d\n", achilles);
        }
        // printf("GCD(%d,%d)= %d", max, min, gcd);
        printf("%d\n", achilles);
    }   
}

下面是使用for循环进行编辑之前的程序!

#include <stdio.h>
#define MAX 1000

int main(void)
{
    int n;
    int i, j, a;
    int counter2 = 0;
    int large;
    int small;
    int rem, gcd, max = 1, min = 1;
    int achilles;
    scanf("%d", &n);
    printf("%d = ",n);

        for (i = 1; i <= n; i++)
        {
            int count = 0;
            for (j = 1; j <= i; j++)
            {
                if (i % j == 0)
                {
                    count++;
                }
            }
            int l = 0;

            if (count == 2)
            {
                while (n % i == 0) // calculate factor and his exponent
                {
                    l++;
                    n = n / i;
                }

                if (l > max) // calculates min and max in order to find GCD
                {
                    max = l;
                }
                if (l < min)
                {
                    min = l;
                }
            }

            large = max;
            small = min;
            while (small) { // While small is not 0
                            // Calculates GCD
                rem = large % small;
                large = small;
                small = rem;
            }
            gcd = large;
            // printf("GCD(%d,%d)= %d", large, small, gcd);
        }
        /*if (gcd == 1) {
            achilles = n;
            // printf("%d\n", achilles);
        }*/
        printf("GCD(%d,%d)= %d", max, min, gcd);
        //printf("%d\n", achilles);
}   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM