当前位置: 首页 > news >正文

牛客-【237题】算法基础精选题单-第二章 递归、分治

第二章 递归、分治

  • 递归
    • NC15173 The Biggest Water Problem
    • NC22164 更相减损术

递归

NC15173 The Biggest Water Problem

简单递归,直接暴力

#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
#define de(x) cout << x << " ";
#define sf(x) scanf("%d", &x);
#define Pu puts("");
#define ll long long
int n, m, ans;
int fun(int x) {if (x / 10 == 0) {return x;}int res = 0;while (x) {res += (x % 10);x /= 10;}return fun(res);
}
int main() {cin >> n;cout << fun(n) << endl;return 0;
}

NC22164 更相减损术

注意处理的时候,使用较大的值去减去较小的值

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sf(x) scanf("%d", &x);
#define de(x) cout << x << " ";
#define Pu puts("");
const int N = 1e5 + 9, mod = 1e9 + 7;
int n, m, ans;
int fun(int x, int y) {if (x % y == 0) {return y;}int t = x - y;if (t > y) {  // 大数放前面return fun(t, y);} else {return fun(y, t);}
}
int main() {cin >> n >> m;cout << fun(max(n, m), min(n, m)) << endl;return 0;
}
http://www.lryc.cn/news/212419.html

相关文章:

  • leetcode-字符串
  • 多线程---synchronized特性+原理
  • Qt实现卡牌对对碰游戏
  • 【3D 图像分割】基于 Pytorch 的 VNet 3D 图像分割7(数据预处理)
  • 极米科技H6 Pro 4K、H6 4K高亮定焦版——开启家用投影4K普及时代
  • 软考系统架构师知识点集锦九:数据库系统
  • IOC课程整理-6 Spring IoC 依赖注入
  • FANUC机器人PRIO-621和PRIO-622设备和控制器没有运行故障处理
  • 《动手深度学习》线性回归简洁实现实例
  • 国家数据局正式揭牌,数据专业融合型人才迎来发展良机
  • 基于springboot实现休闲娱乐代理售票平台系统项目【项目源码+论文说明】
  • 3.AUTOSAR OS分析(一)
  • AB试验(七)利用Python模拟A/B试验
  • Go语言入门-流程控制语句
  • 深入探究ASEMI肖特基二极管MBR60100PT的材质
  • python类模拟“对战游戏”
  • Maven第二章:Maven基本概念与生命周期
  • <蓝桥杯软件赛>零基础备赛20周--第3周--填空题
  • 【Linux】VM及WindowsServer安装
  • 【实用教程】MySQL内置函数
  • 第十二节——ref
  • 少儿编程 2023年9月中国电子学会图形化编程等级考试Scratch编程四级真题解析(判断题)
  • 【设计模式三原则】
  • 600MW发电机组继电保护自动装置的整定计算及仿真
  • 【蓝桥每日一题]-字符串(保姆级教程 篇1)#atcoder324C~E题
  • 4.2.1 SQL语句、索引、视图、存储过程
  • 1992-2021年全国各地级市经过矫正的夜间灯光数据(GNLD、VIIRS)
  • 机器人的触发条件有什么区别,如何巧妙的使用
  • 【Qt6】QStringList
  • 代码随想录算法训练营第五十三天|309.最佳买卖股票时机含冷冻期 ● 714.买卖股票的最佳时机含手续费