Multiplication problem-multiplication formula table, decomposition factor, find the greatest common divisor

The
basic idea of decomposing the factor problem -given a number k, let it be divided by all the numbers less than or equal to him, if it can be divided, and if (k%i==0) is true, then k is one of his factors, Then the factor of the obtained quotient must be the factor of k, so

 if (k % i == 0)
		 {
    
    
			 printf("因素是%d\n", i);
			 j++;
			 
			 k = k / i;

			 i = 1;
		
		 }

At this time, k is the original k/i quotient. The factor of the quotient of k is also a factor of k.
Next, we need to construct a loop to generate all numbers less than or equal to k.

 for (i = 1; i <= k; i++)
	 {
    
    
		 if (k % i == 0)
		 {
    
    
			 printf("因素是%d\n", i);
			 j++;
			 
			 k = k / i;

			 i = 1;
		
		 }
	 
	 }
	

At the same time, we need to use the variable j to judge whether k is quality and feedback to us. When k%i==0 is true and only twice, k and 1 are true.

 resolve()
 {
    
    
	 int a = 0;
	 int j = 0;
	 int i = 0;
	 int k = 0;
	 printf("你要求谁的因素?");
	 scanf("%d", &k);
	 a = k;
	 for (i = 1; i <= k; i++)
	 {
    
    
		 if (k % i == 0)
		 {
    
    
			 printf("因素是%d\n", i);
			 j++;
			 
			 k = k / i;

			 i = 1;
		
		 }
	 
	 }
	 if (j == 2)
	 {
    
    
		 printf("****%d是质素****", a);
	 }
		 
	 
	 
	

	 

	 
 }

Seeking the greatest common divisor of the ancestor's more
subtraction technique The first step of the more subtraction technique is to determine whether the greatest common divisor of two numbers is 2,
we only need to set the for loop entry to **for (; i% 2 == 0&&j%2==0;)**As long as the required two numbers ij can be divisible by 2 at the same time, you can enter the loop. We also need to add a variable m to make sure that in addition to a few times, 2 will be added later

for (; i % 2 == 0&&j%2==0;)
	 {
    
    
		 i = i/2;
		 j = j / 2;
		 m++;
	 }

When we input the ij variable, the second step is to determine who is greater than ij. In order to facilitate us to set the i variable to a larger variable through simple variable conversion

if (j > i)
	 {
    
    
		 a = i;
		 i = j;
		 j = a;
	 }

Insert picture description here
It can be seen from this example that the difference obtained by subtracting the smaller variable j from the larger variable i of the input is used, and then the difference is larger than the minus, and the smaller the minus is the minus until the minus and the difference are equal and
visible. The exit of the cycle is that the subtraction and the difference are equal.
The design idea is to subtract first by attaching the larger value of the subtraction and difference to i

for (;;)
	 {
    
    
		 b = i - j;
		 
		
		 if (j == b)
		 {
    
    
			 break; 
		 }
		 if (b > j)
		 {
    
    
			 a = j;
			 j = b;
			 b = a;
		 }
		 i = j;
		 j = b;

     }

Finally, if the m variable is greater than 0, the factor must be multiplied by 2 m times, otherwise directly output

if (m > 0)
	 {
    
    
		 n = 2 * m * b;
		 printf("最大公约数是%d", n);
      }
	 else
		 printf("最大公约数是%d", j);

Attach the whole

common()
 {
    
    
	 int a = 0;
	 int n = 0;
	 int m = 0;
	 int i = 0;
	 int j = 0; 
	 printf("请输入被求数\n");
	 scanf("%d %d", &i, &j);
	 for (; i % 2 == 0&&j%2==0;)
	 {
    
    
		 i = i/2;
		 j = j / 2;
		 m++;
	 }
	 if (j > i)
	 {
    
    
		 a = i;
		 i = j;
		 j = a;
	 }
	 a = 0;
	 int b = 0;
	 for (; ; )
	 {
    
    
		 b = i - j;
		 
		
		 if (j == b)
		 {
    
    
			 break;
		 }
		 if (b > j)
		 {
    
    
			 a = j;
			 j = b;
			 b = a;
		 }
		 i = j;
		 j = b;

     }
	 if (m > 0)
	 {
    
    
		 n = 2 * m * b;
		 printf("最大公约数是%d", n);
      }
	 else
		 printf("最大公约数是%d", j);
 }

The third function prints the multiplication formula table.
We need ij two variables to control the end of the first row of the row and column is 1, 1 the end of the second row is 2, and 2
can be seen when the variable of the end column is controlled by the row, then the inner loop entry is j <=i

for (j = 1; j <= i; j++)
		{
    
    
			k = i * j;
			printf("%d = %d * %d   ",k,i,j);
			k = 0;
		}

When the row is controlled, the column also determines that the multiplication formula table has a total of 9 rows, so only need to make i less than or equal to 9.

 table()
{
    
    
	 int i = 0;
	 int j = 0;
	int k = 0;
	for (i = 1; i <= 9; i++)
	{
    
    
		j = 0;
		for (j = 1; j <= i; j++)
		{
    
    
			k = i * j;
			printf("%d = %d * %d   ",k,i,j);
			k = 0;
		}
		printf("\n");
	}
	
}

Attach test.c here

#define _CRT_SECURE_NO_WARNINGS 1
#include"实现.h"
int main()
{
    
    
	int input = 0;
	menu();
	scanf("%d", &input);
	chose(input);
	
	
	
	return 0;
}

Header file implementation.h

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS 1


menu();
void chose(int input);
table();
resolve();
common();

Implementation file

#define _CRT_SECURE_NO_WARNINGS 1
#include"实现.h"

menu()
{
    
    
	printf("*******1,分解因数**************\n");
	printf("*******2,求两个数最大公约数****\n");
	printf("*******3,打印口诀表************\n");
	printf("*******4,退出**************\n");
}

 table()
{
    
    
	 int i = 0;
	 int j = 0;
	int k = 0;
	for (i = 1; i <= 9; i++)
	{
    
    
		j = 0;
		for (j = 1; j <= i; j++)
		{
    
    
			k = i * j;
			printf("%d = %d * %d   ",k,i,j);
			k = 0;
		}
		printf("\n");
	}
	
}
 void chose(int input)
 {
    
    
	 do
	 {
    
    
		 switch (input)
		 {
    
    
		 case 1:
			 resolve();

			 break;
		 case 2:
			 common();
			 break;
		 case 3:

			 table();
			 break;
		 default:
			 break;
		 }

	 } while (input!=3);
	 

	 
 }
 resolve()
 {
    
    
	 int a = 0;
	 int j = 0;
	 int i = 0;
	 int k = 0;
	 printf("你要求谁的因素?");
	 scanf("%d", &k);
	 a = k;
	 for (i = 1; i <= k; i++)
	 {
    
    
		 if (k % i == 0)
		 {
    
    
			 printf("因素是%d\n", i);
			 j++;
			 
			 k = k / i;

			 i = 1;
		
		 }
	 
	 }
	 if (j == 2)
	 {
    
    
		 printf("****%d是质素****", a);
	 }
		 
	 
	 
	

	 

	 
 }
 common()
 {
    
    
	 int a = 0;
	 int n = 0;
	 int m = 0;
	 int i = 0;
	 int j = 0; 
	 printf("请输入被求数\n");
	 scanf("%d %d", &i, &j);
	 for (; i % 2 == 0&&j%2==0;)
	 {
    
    
		 i = i/2;
		 j = j / 2;
		 m++;
	 }
	 if (j > i)
	 {
    
    
		 a = i;
		 i = j;
		 j = a;
	 }
	 a = 0;
	 int b = 0;
	 for (;;)
	 {
    
    
		 b = i - j;
		 
		
		 if (j == b)
		 {
    
    
			 break;
		 }
		 if (b > j)
		 {
    
    
			 a = j;
			 j = b;
			 b = a;
		 }
		 i = j;
		 j = b;

     }
	 if (m > 0)
	 {
    
    
		 n = 2 * m * b;
		 printf("最大公约数是%d", n);
      }
	 else
		 printf("最大公约数是%d", j);
 }

Guess you like

Origin blog.csdn.net/qq_45849625/article/details/110264273