当前位置:网站首页>Rolling division, Young's matrix and three-step flip
Rolling division, Young's matrix and three-step flip
2022-07-25 02:19:00 【JDSZGLLL】
Catalog
introduction
This article is about learning C Three classic problems in language process , Elaborate , Understand the details of the three solutions from the perspective of logic and code .
division
Euclid algorithm also called division , Is used to calculate two nonnegative integers a,b Of greatest common divisor . There are two fields of application: Mathematics and computer .
Next, let's take a look at its implementation principle :

#include <stdio.h>
int main()
{
int n = 0;
int m = 0;
scanf("%d%d", &n, &m);
// set up max Is the greatest common divisor
int max = n>m?m:n;
// set up min It's the least common multiple
int min = n>m?n:m;
while(1)
{
if(m%max==0 && n%max ==0)
{
break;
}
max--;
}
while(1)
{
if(min%m == 0 && min%n==0)
{
break;
}
min++;
}
printf("%d\n", max+min);
return 0;
}Young's matrix
Young's matrix is a numerical matrix , Each row of the matrix is incremented from left to right , The matrix is increasing from top to bottom , Please write a program to find out whether a number exists in such a matrix .
Let's observe from the picture :

#include <stdio.h>
int findnum(int a[][3], int x, int y, int f) // The type of the first parameter needs to be adjusted
{
int i = 0, j = x - 1; // Start from the top right corner and traverse
while (j >= 0 && i < y)
{
if (a[i][j] < f) // Older than me, down
{
i++;
}
else if (a[i][j] > f) // He left when he was younger than me
{
j--;
}
else
{
return 1;
}
}
return 0;
}
int main()
{
int a[][3] = { {1, 3, 5},
{3, 5, 7},
{5, 7, 9} }; // An example
if (findnum(a, 3, 3, 2))
{
printf(" eureka \n");
}
else
{
printf(", I didn't find it \n");
}
return 0;
}Three step flipping
The three-step inversion method is used to solve the problem of multiple left rotation of strings .
Implement a function , You can rotate left in a string k Characters .
for example :

void reverse_part(char *str, int start, int end) // String from start To end This paragraph is in reverse order
{
int i, j;
char tmp;
for (i = start, j = end; i < j; i++, j--)
{
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
void leftRound(char * src, int time)
{
int len = strlen(src);
int pos = time % len;
reverse_part(src, 0, pos - 1); // The preceding paragraph in reverse order
reverse_part(src, pos, len - 1); // Backward segment
reverse_part(src, 0, len - 1); // The whole is in reverse order
}
边栏推荐
- Hcip - BGP - border gateway protocol
- Management mode of agricultural science data center based on life cycle theory
- Talk about resume optimization and interview skills of algorithm post!
- Inventory of well-known source code mall systems at home and abroad
- Hbuilderx developed by uni app connects to night God simulator
- Antdb database products were selected into the global database industry map (2022) of the China Academy of communications and communications
- Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc
- Research and application of scientific data management strategy for high energy synchrotron radiation source
- Upgrade the leapfrog products again, and the 2023 Geely Xingrui will be sold from 113700 yuan
- Promise basic use
猜你喜欢

In the post deep learning era, where is the recommendation system going?

ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline

Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts

Use SAP ui5 application to consume create and delete operations of OData in business application studio

Use Fiddler to capture apps

KVM virtualization jobs
Windows Server 2022 received a non security update in July: fix the sticking problem caused by defender

C#/VB. Net insert watermark in word

VRRP virtual redundancy protocol configuration

Hcip - BGP - border gateway protocol
随机推荐
Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
Server performance monitoring
R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set
When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
Agreement on sharing agricultural scientific data in China
Talk about what's going on with C # backstage GC?
Digital commerce cloud fine chemical industry management platform integrated informatization solution
Dynamic memory development
Speed comparison between 64 bit width and 32 bit width of arm64 memory
The importance of system to Philosophy Hegel
B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry
What do growth enterprises need most to build a data-driven organization?
iptables :chains, target
Yunyuanyuan (VIII) | Devops in depth Devops
Solution to the occupation of project startup port
Summary thinking caused by the function of a SMS verification code [easy to understand]
Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts
A weird jedisconnectionexception: connection rejected problem
[system design] distributed key value database
Eolink - quickly develop interfaces through document driven