C language: Find the least common multiple of the two input numbers

topic:

Find the least common multiple of the two numbers entered

                    

 =========================================================================

                       

Idea 1: Common method

(simple thinking, low efficiency)

general idea:

(one).

Enter two numbers: a and b ,

Use the ternary expression to take out the larger value

                 

(two).

Using a while loop ,

Loop to judge whether the larger value can divide two numbers at the same time ,

can be divisible , indicating that the larger value is the greatest common multiple of the two values , jump out of the loop ,

If it is not divisible , the larger value ++ will jump out of the loop until the two values ​​can be divisible at the same time .

to print

                


                 

first step:

(1). Input two numbers: a and b

                

(2). Use the ternary expression to take out the larger value

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入两个数:
	long long a = 0;
	long long b = 0;
	//输入:
	scanf("%lld %lld", &a, &b);

	//使用 三目表达式 ,把较大值取出
	//假设 a 和 b 的较大值是最小公倍数
	long long m = a > b ? a : b;
	//较大值赋给 m


	return 0;
}

Realize the picture:

                 


                 

Step two:

(1). Use the while loop to loop to judge whether the larger value can divide two numbers at the same time

               

(2). In the while loop , use the if condition to judge whether the larger value can divide two numbers at the same time .

             

can be divisible , indicating that the larger value is the greatest common multiple of the two values , jump out of the loop ,

            

If it cannot be divisible , then the larger value ++ , until it can divisible two values ​​at the same time , jump out of the loop ,

            

to print

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入两个数:
	long long a = 0;
	long long b = 0;
	//输入:
	scanf("%lld %lld", &a, &b);

	//使用 三目表达式 ,把较大值取出
	//假设 a 和 b 的较大值是最小公倍数
	long long m = a > b ? a : b;
	//较大值赋给 m

	//使用 while循环,
	//循环判断 较大值 能否同时整除两个数
	while (1) //死循环,等满足用break跳出循环
	{
		//使用 if条件判断 :
		if (m % a == 0 && m % b == 0)
		//看 较大值 能否同时整除两个数
		{
			//能整除,跳出循环
			break;
		}
		//不能整除,较大值++,直到能整除两个值
		m++;
	}

	//打印:
	printf("%lld\n", m);

	return 0;
}

Realize the picture:

                    

Idea 1: Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//输入两个数:
	long long a = 0;
	long long b = 0;
	//输入:
	scanf("%lld %lld", &a, &b);

	//使用 三目表达式 ,把较大值取出
	//假设 a 和 b 的较大值是最小公倍数
	long long m = a > b ? a : b;
	//较大值赋给 m

	//使用 while循环,
	//循环判断 较大值 能否同时整除两个数
	while (1) //死循环,等满足用break跳出循环
	{
		//使用 if条件判断 :
		if (m % a == 0 && m % b == 0)
		//看 较大值 能否同时整除两个数
		{
			//能整除,跳出循环
			break;
		}
		//不能整除,较大值++,直到能整除两个值
		m++;
	}

	//打印:
	printf("%lld\n", m);

	return 0;
}

Realize the effect:

                    

 =========================================================================

                       

Idea 2: a * i % b

(Using method, more efficient)

general idea:

(one). 

Enter two numbers: a and b

         

(two).

Find the least common multiple of a and b :

        

Suppose the least common multiple of two values ​​is k , i = 1 ,

Let         a * i % b        see if it can be divisible ( the remainder is equal to 0 )

Divisible evenly , the least common multiple is k = a * i

Not divisible , then i++,

Until it can be divisible , at this time the least common multiple k = a * i

                


              

first step:

Enter two numbers: a and b

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入两个数:
	long long a = 0;
	long long b = 0;
	//输入:
	scanf("%lld %lld", &a, &b);




	return 0;
}

Realize the picture:

                 


                 

Step two:

Use while loop to judge whether a * i % b is divisible

             

Can be divisible , the least common multiple is k = a * i , print

                 

not divisible , then i++

             

Until it can be divisible , at this time the least common multiple k = a * i

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入两个数:
	long long a = 0;
	long long b = 0;
	//输入:
	scanf("%lld %lld", &a, &b);

	//求最小公倍数:
	int i = 1; //a的倍数
	while (a * i % b != 0)//这里 !=0 可以省略
	// a * i 不能整除 b,则改变 i,即a的倍数,
	//直到可以整除退出循环
	{
		i++;//不能整除则 i++
	}

	//直到能整除为止,此时最小公倍数 k = a * i
	printf("%lld\n", a * i);

	return 0;
}

Realize the picture:

                    

Idea 2: Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//输入两个数:
	long long a = 0;
	long long b = 0;
	//输入:
	scanf("%lld %lld", &a, &b);

	//求最小公倍数:
	int i = 1; //a的倍数
	while (a * i % b != 0)//这里 !=0 可以省略
	// a * i 不能整除 b,则改变 i,即a的倍数,
	//直到可以整除退出循环
	{
		i++;//不能整除则 i++
	}

	//直到能整除为止,此时最小公倍数 k = a * i
	printf("%lld\n", a * i);

	return 0;
}

Realize the effect:

Guess you like

Origin blog.csdn.net/weixin_63176266/article/details/131276390