当前位置:网站首页>Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string
Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string
2022-06-11 02:15:00 【bingbing_ bang】
One 、 greatest common divisor
Ideas 1:( Exhaustive method )
The greatest common divisor of two numbers must be less than the smaller of the two numbers , The greatest common divisor is the smallest 1.
int main()
{
int a = 0;
int b = 0;
scanf("%d %d",&a,&b);
int min = a < b ? a : b;
int i = 1;
int s = min;
for (i = 1;i <= min;i++)
{
if (a % i == 0 && b % i == 0)
{
s = i;
}
}
printf("%d\n", s);
return 0;
}Ideas 2: division
Take the remainder of two numbers , If 0; Divisor is the greatest common divisor , If not for 0, Make the dividend equal to the divisor , Divisor becomes remainder , Continue to fetch remainder , Until the remainder of the two numbers is 0; Then the divisor is the greatest common divisor .
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
while (a%b)
{
int tmp = a %b;
a = b;
b = tmp;
}
printf("%d\n", b);
return 0;
}Ideas 3: More impairment surgery ( Reference blog :http://t.csdn.cn/DlDcv, The original text is more wonderful )
Subtract the smaller number from the larger number , And then continue to subtract from the difference to get a new difference ( When the difference is negative , It's the reverse ), Keep cycling until the subtraction and difference are equal , At this point, the equal number is the greatest common divisor .
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
printf("%d\n", a);
return 0;
}Two 、 Minimum common multiple
Ideas 1:( Exhaustive method )
The smallest common multiple of two numbers is the larger number itself , Then incrementally perform trial operation , Until we find a number that can take the remainder of two numbers at the same time 0 until . This number is the least common multiple .
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
int max = a > b ? a : b;
int i = 0;
//1
for (i = max;i <= a * b;i++)
{
if (i % a == 0 && i % b == 0)
{
printf("%d\n", i);
break;
}
}
//2
/*for (i = 1;i <= max;i++)
{
if ((a * i) % b == 0)
{
printf("%d\n", a*i);
break;
}
}
*/
return 0;
}Ideas 2: Calculation by rolling Division a,b greatest common divisor m, Reuse the formula a*b/m Find the least common multiple
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
int x = a;
int y = b;
while (a % b)
{
int tmp = a % b;
a = b;
b = tmp;
}
printf("%d\n", x * y / b);
return 0;
}3、 ... and 、 Reverse order of strings in complex cases
Input I like beijing.
Output beijing. like I
This question is not a simple string in reverse order , Instead, reverse the word order , The words themselves are not in reverse order .
We found that through analysis

#include<assert.h>
void reverse(char* left, char* right)
{
assert(left && right);
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
char arr[101] = { 0 };
gets(arr);
int len = strlen(arr);
//1、 The whole is in reverse order
reverse(arr, arr + len - 1);
//2、 The reverse order of each word
char* s = arr;
char* e = s;
while (*s!='\0')
{
while (*e != ' ' && *e != '\0')
{
e++;
}
reverse(s, e-1);
s = e + 1;
if (*e == ' ')
{
e++;
}
}
printf("%s\n", arr);
return 0;
}Be careful :
When getting a string , Not used scanf, It uses gers function , The reason lies in ,scanf The function does not read spaces
such as , Input abc def, He will only accept adc
however scanf It is not that there is no way to accept spaces , If used, it can be written like this scanf("%[^\n]",arr)
边栏推荐
- Find - (half find / half find)
- 【Qt】error: QApplication: No such file or directory 解决方案
- [matlab] image enhancement (power transformation, histogram specification processing method, smoothing and sharpening filtering)
- Secret
- C language practice (IX)
- The female programmer gives out a salary slip: the salary is high, but she feels she is 10 years old
- [untitled]
- Enrichment of core knowledge points of interface automation to add points to the interview
- [music] playing blue and white porcelain based on MATLAB [including Matlab source code 1873]
- Go develop web
猜你喜欢

14:00面试,14:08就出来了 ,问的实在是太...

Contest2902 - following Tang Kelian's programming: sequence structure question d: area 201502 question f: persistence of supporting college students in Ludian earthquake

Shader of double sided material

浅析直播间海量聊天消息的架构设计难点

Secret

CRS-5017

AI fanaticism | come to this conference and work together on the new tools of AI!

软件测试面试复盘:技术面没有难倒我,hr面却是一把挂

Tencent test development post interview programming questions

Task02: basic use of database (MySQL)
随机推荐
Task03: stack
Thoughts on small steamed stuffed bun's dividend
NFT insider 61:animoca brands holds US $1.5 billion of encrypted assets in 340 investments
SSH配置密钥登录时需要注意私钥是否设置了密码(passphrase)
Analysis of common ADB commands
Talk about an annotation implementation interface retry
Virtual joystick of QT quick QML instance
[matlab] image segmentation
InfoQ geek media's 15th anniversary solicitation | in depth analysis of container runtime Technology
---Arrange numbers---
QT widget's simple serial port assistant (qserialport)
Oracle tablespaces, users, and authorization to users
Task02: linked list
Secret
Elsevier ---elseviewer--- preprint online publishing notice
Initialize the one-dimensional array a correctly
What should be paid attention to in PMP registration? Special reminder
Programming implementation: input any English month, and output its corresponding Chinese prompt after looking up the month table. Abbreviations can also be found.
Question g: candy
[leetcode] notes on recursive time value transfer and reference transfer