Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

c - 如何用for循环计数器替换scanf?(how can i replace scanf with for loop counter?)

I am writing a program in order to count all achilles numbers without the math.h library.

(我正在编写一个程序,以便在没有math.h库的情况下计算所有跟腱数。)

In this programm first I calculate powerful numbers after that I calculate GCD of powerful number.

(在此程序中,首先我计算大数,然后再计算大数的GCD。)

If GCD is 1 then the current number is achilles.

(如果GCD为1,则当前数字为阿喀琉斯。)

My problem is that my program works with scanf() but not with for loop counter!

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

What i am doing wrong?

(我做错了什么?)

Thank you very much!

(非常感谢你!)

#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
", achilles);
        }
        // printf("GCD(%d,%d)= %d", max, min, gcd);
        printf("%d
", achilles);
    }   
}

The programm before editing with the for loop is the following!

(下面是使用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
", achilles);
        }*/
        printf("GCD(%d,%d)= %d", max, min, gcd);
        //printf("%d
", achilles);
}   
  ask by Konstantinos Kon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...