4.27 总结
Confused_Konjac · · 个人记录
又没垫底,哈哈。
U404848 递归函数f(x,n)
Description
求解
的值,保留两位小数。
Solution
按题意来做,从顶层开始一直向下递归,没啥好说的。
Teacher.J 说这也是送分题,但我没做出来,小数递归写着写着写乱了。
Code
#include <bits/stdc++.h>
using namespace std;
#define int long long
double ans[10005];
double dfs(double x,int st) {
if(st == 0) return 1;//递归结束
if(ans[st] != -1) return ans[st];//算过就不再算了
ans[st] = st * 1.0 / (st + dfs(st - 1));//计算这一层的值
return ans[st];
}
signed main() {
int n;
double t,x;
cin >> t >> x;
for(int i = 1;i <= 10000;i ++) {
ans[i] = -1;
}
while(t --) {//多组数据
cin >> n;
double anss = dfs(x,n);
printf("%.2lf\n",anss);
}
return 0;
}
P10376 [GESP202403 六级] 游戏
Description
你有四个正整数
n,a,b,c ,并准备用它们玩一个简单的小游戏。在一轮游戏操作中,你可以选择将
n 减去a ,或是将n 减去b 。游戏将会进行多轮操作,直到当n \leq c 时游戏结束。
求上述操作有多少种不同的操作序列。
Solution
第一题之后的第二道送分题。
依旧按题意来做,不同的是这次递归每次会产生两种不同的分支。
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2 * 1e5 + 10;
const int Mod = 1e9 + 7;
int n,a,b,c;
map<int,int>mp;
int dfs(int st) {
if(mp.count(st)) return mp[st];//找过的不再找一次
if(st <= c) return 1;//Game over
int res1 = dfs(st - a) % Mod;
int res2 = dfs(st - b) % Mod;
return mp[st] = (res1 + res2) % Mod;//我合着写就挂了,玄学?
//记忆化
}
signed main() {
cin >> n >> a >> b >> c;
cout << dfs(n) % Mod;
}
P1434 [SHOI2002] 滑雪
我觉得还是送分题……
Description
给定一个数字矩阵,你可以从任意一个点出发,找到一个最长的下降序列,不过过程中可以转向。
Solution
DFS 找路径呗,过程中记忆化一下,然后多加一个判断要走的位置是不是比当前位置小的判断。
因为每个点都能当起点,所以要遍历每个点进行一次搜索,然后取最大值。
Code
#include<bits/stdc++.h>
using namespace std;
int high[110][110];
int dp[110][110];
int r,c;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int dfs(int x,int y) {
if(dp[x][y] != -1) return dp[x][y];
for(int d = 0;d < 4;d ++) {
int px = x + dx[d],py = y + dy[d];
if(px < 1 || px > r || py < 1 || py > c) continue; //越界
if(high[px][py] < high[x][y]) {//是不是比当前位置小
dp[x][y] = max(dp[x][y],dfs(px,py) + 1); //神秘的转移
}
}
return dp[x][y];
}
int main() {
cin >> r >> c;
for(int i = 1;i <= r;i ++) {
for(int j = 1;j <= c;j ++) {
cin >> high[i][j];
}
}
for(int i = 1;i <= r;i ++) {//初始化
for(int j = 1;j <= c;j ++) {
dp[i][j] = -1;
}
}
int ans = -91917891;//恶趣味
for(int i = 1;i <= r;i ++) {//找答案
for(int j = 1;j <= c;j ++) {
ans = max(ans,dfs(i,j));
}
}
cout << ans + 2;//没算起点和终点
}
P5635 【CSGRound1】天下第一
Description
给定两个数
cbw 拥有数
第一个回合:
第二个回合:
第三个回合:
第四个回合:
以此类推....
如果
问游戏结果。
Solution
原本有点难,但数据太小了,被我模拟水过了,可怜的学弟学妹们……
模拟呗,注意到如果出现了重复的形式则为平局,所以可以用个键的类型为 pair 的 map 来存
Code
#include<bits/stdc++.h>
using namespace std;
map<pair<int,int>,int>mp;
int T,p;
int main() {
cin >> T >> p;
while(T --) {
int x,y;
cin >> x >> y;
int f = 1,i = 1;
mp.clear();
while(f == 1) {
if(mp.count({x,y})) {//判平局
cout << "error" << endl;
f = 0;
}
if(i % 2 == 1) { // 奇数回合,代表本回合对x进行改变
mp[{x,y}] = 1;
x = (x + y) % p;
if(x == 0) {
cout << 1 << endl;
f = 0;
}
} else { // 偶数回合,代表本回合对y进行改变
mp[{x,y}] = 1;
y = (x + y) % p;
if(y == 0) {
cout << 2 << endl;
f = 0;
}
}
i ++;
}
}
}