当前位置:网站首页>1110: nearest common ancestor (function topic)
1110: nearest common ancestor (function topic)
2022-06-29 02:43:00 【Huaze flower】
Title Description

As shown in the figure above , From positive integers 1, 2, 3, ... It forms an infinite binary tree . From a node to the root node
spot ( The number is 1 The node of ) There is a unique path , For instance from 10 The path to the root node is (10, 5, 2, 1),
from 4 The path to the root node is (4, 2, 1), All nodes on the path from the node to the root node are called the ancestors of the node . The problem now is , Given x and y, seek x and y The most recent common ancestor of , such as ,10 and 4 The most recent common ancestor is 2,10 and 5 Our nearest common ancestor is 5.
Define recursive functions
int common(int x, int y)
{
If x==y, return x;
If x>y, seek x/2 And y The common ancestor of ;
otherwise , seek x And y/2 The common ancestor of ;
}
Input
There is only one line of input , Including two positive integers x and y, Neither of these positive integers is greater than 1000.
Output
The output has only one positive integer , namely x and y The most recent common ancestor of .
The sample input Copy
10 4
Sample output Copy
2
Code :
#include<stdio.h>
int common(int x,int y)
{
if(x==y)
return x;
else if(x>y)
common(x/2,y);
else common(x,y/2);
}
int main()
{
int x,y,m;
scanf("%d%d",&x,&y);
m=common(x,y);
printf("%d",m);
}Operation diagram :

What has been learned :
From this question I know , The defined function can also be called in the definition function module , Let the originally linear function form a circle through this ingenious call , So as to achieve the purpose of circulation .
This design is too clever , cattle B cattle B!
边栏推荐
- "The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
- Relationship between EMC, EMI and EMS
- Sysbench Pressure Test Oracle (installation and use examples)
- “内窥镜第一股”二闯IPO,去年亏损5个亿,核心产品商业化仍存疑 | IPO速递
- 矩阵特征值和特征向量求解——特征值分解(EVD)
- Overview of PMP project management
- CTFHub-Web-密码口令-默认口令
- Ctfhub web SQL injection - integer injection
- 线程池是什么老鸡?
- Trigonometric function calculation
猜你喜欢
随机推荐
"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
18. `bs對象.節點名.next_sibling` 獲取兄弟節點
Install mysql5.7 and change the password
PHP system function
Deploy redis high availability cluster
线程池是什么老鸡?
Why should the pointer be null after delete
EMC、EMI、EMS的关系
apache不解析PHP文件,直接显示源码
leetcode 统计放置房子的方式数
【无标题】
组合数据类型之元组小练习
PHP database ODBC
Download and installation of MySQL
MySQL binlog log cleanup
Talk about SQL optimization
String attribute exercise
学习太极创客 — MQTT 第二章(九)本章测试
Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first
18. ` BS object. Nom du noeud. Suivant Sibling ` get Brother Node








