- 题解
西川中学提高训练-讨论
- @ 2025-3-20 13:19:28
仁慈的希蒙

8 条评论
-
hishuchao LV 5 MOD @ 2025-3-21 8:38:21已修改
希蒙的套娃函数
#include<bits/stdc++.h> using namespace std; double fun(int n,int x){ if(n==1) return sqrt(x+1); return sqrt(n + fun(n-1,x)); } int main(){ int x,n; cin>>n>>x; printf("%.2f", fun(n,x)); return 0; } -
@ 2025-3-21 8:37:54
希蒙的数学公式
#include <bits/stdc++.h> using namespace std; double fun(double n,double x){ if(n==1)return x/(x+1); return x/(n+fun(n-1,x)); } int main(){ double x,n; cin>>x>>n; printf("%.2f",fun(n,x)); return 0; } -
@ 2025-3-21 8:36:01
幂次方
#include <bits/stdc++.h> using namespace std; // 函数convert用于将给定的整数转换成2的幂次方表示形式 string convert(int n) { // 处理特殊情况:0, 1, 和 2 的直接返回值 if (n == 0) return "0"; // 0 的特殊处理 if (n == 1) return "2(0)"; // 1 的特殊处理 if (n == 2) return "2"; // 2 的特殊处理 string res; // 结果字符串 // 遍历可能的2的幂(从大到小) for (int i = 14; i >= 0; --i) { // 由于最大输入是2*10^4,所以最多考虑2^14 int pw = 1 << i; // 计算2的幂次方 if (n & pw) { // 如果当前位被设置 // 如果结果字符串不为空且最后一位不是'(',则添加加号 if (!res.empty() && res[res.length() - 1] != '(') res += "+"; // 根据不同的幂次进行不同处理 if (i == 1) res += "2"; // 特殊情况: 2^1 else if (i == 0) res += "2(0)"; // 特殊情况: 2^0 else res += "2(" + convert(i) + ")"; // 递归调用convert函数来处理较大的幂次 } } return res; // 返回最终的结果字符串 } int main() { int n; // 输入的正整数 cin >> n; // 读取输入 cout << convert(n) << endl; // 输出转换后的字符串 return 0; } -
@ 2025-3-21 8:31:42
希蒙的特殊Function
#include <bits/stdc++.h> using namespace std; // 定义三维数组用于记忆化存储已经计算过的结果 long long dp[21][21][21]; // 递归函数w,根据题目描述进行递归计算 long long w(long long a, long long b, long long c) { // 如果任意参数小于等于0,则返回1 if (a <= 0 || b <= 0 || c <= 0) return 1; // 如果任一参数大于20,则转化为w(20,20,20) if (a > 20 || b > 20 || c > 20) return w(20, 20, 20); // 如果当前参数组合已经被计算过,则直接返回存储的结果 if (dp[a][b][c] != -1) return dp[a][b][c]; // 根据题目条件进行递归调用,并将结果存储到dp数组中 if (a < b && b < c) return dp[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c); else return dp[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1); } int main() { // 手动初始化dp数组为-1,表示未计算状态 for(int i = 0; i <= 20; ++i) for(int j = 0; j <= 20; ++j) for(int k = 0; k <= 20; ++k) dp[i][j][k] = -1; long long a, b, c; // 定义变量用于存储输入值 // 循环读取输入直到遇到终止信号(-1, -1, -1) while (cin >> a >> b >> c, a != -1 || b != -1 || c != -1) { // 输出格式化的结果 cout << "w(" << a << ", " << b << ", " << c << ") = " << w(a, b, c) << endl; } return 0; } -
@ 2025-3-21 8:30:29
希蒙的F91函数
#include <bits/stdc++.h> using namespace std; // 定义一个数组用于存储已经计算过的f91结果,避免重复计算 int dp[250001]; // F91函数的实现 int f91(int n) { // 如果n大于等于101,直接返回n-10 if (n >= 101) return n - 10; // 如果之前已经计算过当前n的结果,则直接返回存储的结果 if (dp[n] != -1) return dp[n]; // 否则,根据题目描述进行递归调用并存储结果 return dp[n] = f91(f91(n + 11)); } int main() { // 手动初始化dp数组为-1,表示未计算状态 for(int i = 0; i <= 250000; ++i) dp[i] = -1; int n; // 输入的整数n // 循环读取输入直到遇到n=0为止 while (cin >> n && n) { // 输出每个n对应的f91(n)结果 cout << "f91(" << n << ") = " << f91(n) << endl; } return 0; } -
@ 2025-3-20 16:03:58
P397 hanoi(汉诺塔)
#include <bits/stdc++.h> // 使用万能头文件,包含所有标准库 using namespace std; // 函数move用于递归解决汉诺塔问题 // 参数n表示要移动的盘子数量,a是起始柱子,b是辅助柱子,c是目标柱子 void move(int n, char a, char b, char c) { if (n == 1) { // 如果只有一个盘子,则直接从a移动到c cout << a << "->" << c << endl; // 输出移动步骤 return; // 结束当前函数调用 } // 先将n-1个盘子从a移动到b,使用c作为辅助柱子 move(n-1, a, c, b); // 将第n个盘子从a移动到c cout << a << "->" << c << endl; // 最后将n-1个盘子从b移动到c,使用a作为辅助柱子 move(n-1, b, a, c); } int main() { int n; // 汉诺塔层数 char a, b, c; // 三根柱子的标识符 cin >> n >> a >> b >> c; // 输入数据 move(n, a, b, c); // 调用move函数开始解决汉诺塔问题 return 0; } -
@ 2025-3-20 15:50:04
CC的烦恼1
#include<bits/stdc++.h> // 万能头文件,包含所有标准库 using namespace std; int cnt; // 全局变量 cnt,用于统计符合条件的字符的权重 int main() { string s; // 定义字符串 s,用于存储输入 cin >> s; // 输入字符串 s // 遍历字符串 s 的每一个字符 for (int i = 0; i < s.size(); i++) { // 如果当前字符是 'x' 或 'y' if (s[i] == 'x' || s[i] == 'y') { // 检查下一个字符是否是 '^'(表示指数) if (s[i + 1] == '^') { // 如果是指数形式,将指数部分的值加到 cnt 中 cnt += s[i + 2] - '0'; // s[i+2] 是指数的数字字符,减去 '0' 得到其整数值 } else { // 如果不是指数形式,直接加 1(默认指数为 1) cnt++; } } // 如果当前字符是 '^',并且前一个字符是 'x' 或 'y',则跳过 // 或者当前字符是数字(2 到 9),并且前两个字符是 'x' 或 'y',则跳过 else if ((s[i] == '^' && (s[i - 1] == 'x' || s[i - 1] == 'y')) || ((s[i] > '1' && s[i] <= '9') && (s[i - 2] == 'x' || s[i - 2] == 'y'))) { continue; // 跳过这些字符,不处理 } // 其他情况,直接输出当前字符 else { cout << s[i]; } } // 输出换行符 cout << endl; // 输出统计的 cnt 值 cout << cnt; return 0; // 程序结束 } -
@ 2025-3-20 15:49:06
CC的烦恼2
#include <bits/stdc++.h> using namespace std; int calculateDegree(const string& term) { size_t degree = 0; for (size_t i = 0; i < term.size(); i++) { if (term[i] == 'x' || term[i] == 'y') { if (i + 1 < term.size() && term[i + 1] == '^') { degree += term[i + 2] - '0'; i += 2; } else { degree += 1; } } } return static_cast<int>(degree); } int main() { string polynomial; cin >> polynomial; string terms[100]; int termCount = 0; string term = ""; for (size_t i = 0; i < polynomial.size(); i++) { char ch = polynomial[i]; if (ch == '+' || ch == '-') { if (!term.empty()) { // Remove leading '+' or '-' before storing if (term[0] == '+' || term[0] == '-') { terms[termCount++] = term.substr(1); } else { terms[termCount++] = term; } } term = string(1, ch); // Start new term with the sign } else { term += ch; } } if (!term.empty()) { // Handle the last term if (term[0] == '+' || term[0] == '-') { terms[termCount++] = term.substr(1); } else { terms[termCount++] = term; } } int maxDegree = 0; string maxTerm = ""; for (int i = 0; i < termCount; i++) { int degree = calculateDegree(terms[i]); if (degree > maxDegree) { maxDegree = degree; maxTerm = terms[i]; } } cout << termCount << " " << maxDegree << " " << maxTerm << endl; return 0; }
- 1