Continuing from the previous blog- written test questions for C language programming (1)
Article Directory
One, find the least common multiple
Title description:
The least common multiple of positive integer A and positive integer B refers to the smallest positive integer value that can be divisible by A and B. Design an algorithm to find the least common multiple of input A and B.
Enter a description:
Output the least common multiple of A and B.
Example:
enter:
5 7
Output:
35
Thought steps
1. Method One
Before doing the problem of the least common multiple, let's first understand the greatest common divisor.
How to find the greatest common divisor of two numbers?
We can use the toss and turn to divide into calculations.
Toss and turn and divide:
Okay, we can understand the process of finding the greatest common divisor based on the thinking process in the figure above and the introduction of Baidu Baike.
Ideas:
1. First find the greatest common divisor q
2. Calculate the least common multiple p
Implementation code
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
scanf("%d", &a);
scanf("%d", &b);
int m = a;
int n = b;
int tmp = 0;
//a存放较大的值
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
while (a%b != 0)
{
c = a%b;
a = b;
b = c;
}
if (a%b == 0)
{
c = b;
}
printf("%d\n",(m*n)/c);
return 0;
}
2. Method two
i is incremented by 1 every time from i=a until i%a=0 && i%b=0, at this time i is the least common multiple.
Implementation code
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
scanf_s("%d%d", &a, &b);
int tmp = 0;
//a存放较大的值
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
int i = 0;
for (i = a; i>0; i++)
{
if (i%a == 0 && i%b == 0)
{
break;
}
}
printf("%d\n", i);
return 0;
}
2. Find a single dog
Subject title:
Looking for a single dog
Title description
Only two numbers in an array appear once, and all other numbers appear twice.
Write a function to find these two numbers that only appear once.
Thinking steps
1. Find out the two numbers that only appear once
2. According to the law of binary bits, divide these two "single dogs" into new arrays respectively
3. Find out the two single dogs in the two groups according to the usage of the bit operator
Implementation code
int main()
{
int arr[] = {
1, 2, 3, 4, 5, 6, 4, 3, 2, 1 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int num = 0;
for (i = 0; i < sz; i++)
{
num ^= arr[i];
}
//我们得到了只出现了一次的两个数字的按位异或的结果。
//根据异或的结果,我们得到这两个数字在二进制第几位开始不同
//记录下异或结果中1第一次出现的位置pos,将原数组中的数字根据二进制第pos位是1/0进行分组。
int pos = 0;
for (i = 0; i < sz; i++)
{
if ((num >> i) == 1)
pos = i;
}
//我们根据这一点,将这两个数字分在两个新的数组中
int n1 = 0;
int n2 = 0;
//我们将二进制位中第pos位是0的数字分成新的一组
for (i = 0; i < sz; i++)
{
if (((arr[i] >> pos) & 1) == 0)
n1 ^= arr[i]; //新数组中的数字全部异或得到那个只出现一次的数字
}
//我们将二进制位中第pos位是1的数字分成新的一组
for (i = 0; i < sz; i++)
{
if (((arr[i] >> pos) & 1) == 1)
n2 ^= arr[i]; //新数组中的数字全部异或得到那个只出现一次的数字
}
printf("%d %d\n", n1, n2);
return 0;
}
Remember: Keep in mind the use of bitwise operators in the C language! !
Today’s topic is shared here, thank you for your appreciation and attention! ! !