a program that calculates the maximum common denominator of two positive integers and writes them out to the standard output

Asked 2 years ago, Updated 2 years ago, 30 views

Thank you for your help.

I'm learning programming, but I don't know at all.
My heart is broken.
Can someone tell me?

The following are the problems and examples of implementation.I don't know what I don't understand.
Thank you for your cooperation.

problem
Two positive integers are given from the standard input. Create a program that calculates the maximum common denominator of the two positive integers and writes them out to the standard output.
The maximum common denominator should be left-handed and written as a single line.
Complete the program by adding what you think here, as only the framework is written.

Example Execution

Standard input
8
15
standard output
1
#include<stdio.h>

int main(intargc, char*argv[]){
    inta,b;

    scanf("%d", & a); scanf("%d", & b);

    // ...to find the highest common divisor of a and b,
    // ...and write it down with the minimum number of digits required.

    return 0;
}

c

2022-09-30 16:49

1 Answers

A possible solution to this problem is to review the following steps in order:

"This time, ""I don't know what I don't understand"", so let's start by checking where you are at this stage and where you ""don't understand."

The maximum common denominator for positive integers a and b is the largest common divisor of a and b.

When a and b are given, there are several algorithms to find the maximum common denominator for a and b.

For example, in a simple case, if a or b or the smaller one is i, reduce i by one to see if a and b are both divisible, and i is the maximum common denominator.Do you know why this calculates the maximum common denominator?

Another famous algorithm that can be calculated more efficiently is Euclidean reciprocity.If you're not familiar with the Wikipedia article, try reading it (if you're not familiar with the reciprocal algorithm, try translating it into a language and asking new questions:)

Euclidean reciprocity --Wikipedia

Algorithms are abstract concepts and should be written as real programs.

What elements of the C language should I use to write the program?If you need to repeat, you can use elements to express the iteration, such as the for statement or the while statement.If you want to implement Euclidean reciprocity, you need to calculate a divided by b, which is calculated by writing a%b in C language.

If you don't know what for and while are like, I think it's better to do some kind of C language tutorial before working on this problem.The class designations, books on the market, or materials on the web will help.

Also, in the case of a well-known problem like this one, you can find the source code by searching the web using the "Maximum Commonwealth Program" or "Euclidean Mutual Aid."You can also read the existing source code, see what it is doing, and then play with it to see how it works or write it again.

In this assignment, we need to get a and b from the input and output the maximum common divisor required.

The input part is already written in the program in question as scanf, so the rest is the output part.It would be convenient to use printf.


2022-09-30 16:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.