牛客-【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;
}