commit 2163c32f563088d1eeadef7fc272ca0822d9ddc1 Author: zzwcreator <2267110628@qq.com> Date: Sat Nov 9 19:02:14 2024 +0800 第一次提交 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4df6722 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,52 @@ +{ + "files.associations": { + "vector": "cpp", + "algorithm": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "memory": "cpp", + "new": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocnum": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp", + "queue": "cpp", + "numeric": "cpp", + "unordered_set": "cpp", + "xhash": "cpp" + } +} \ No newline at end of file diff --git a/DiDi/ques1.cpp b/DiDi/ques1.cpp new file mode 100644 index 0000000..36d794e --- /dev/null +++ b/DiDi/ques1.cpp @@ -0,0 +1,38 @@ +/* +从数组中选择尽可能多的元素,只能连续选择,使得相加小于m,求最多可以选择几个数。输入为一个个数为n的数组和一个整数,输出为最多选择元素的个数。 +*/ +#include +#include +using namespace std; +int maxConsecutiveElementsUnderSum(const std::vector& nums, int m) { + int n = nums.size(); + int max_count = 0; // 最多选择的元素个数 + int current_sum = 0; // 当前窗口内元素的和 + int left = 0; // 左指针 + // 遍历数组,用右指针表示窗口的右端 + for (int right = 0; right < n; ++right) { + current_sum += nums[right]; // 增加当前右指针对应的元素 + // 当当前窗口和大于 m 时,收缩左边界 + while (current_sum > m && left <= right) { + current_sum -= nums[left]; // 减去左指针的元素 + left++; // 左指针右移 + } + // 更新最多选择的元素个数 + max_count = std::max(max_count, right - left + 1); + } + return max_count; +} + +int main() { + vector nums; + int n,m; + cin>>n>>m; + int val; + for(int i = 0; i < n; i++){ + cin >> val; + nums.push_back(val); + } + int result = maxConsecutiveElementsUnderSum(nums, m); + cout << result << endl; + return 0; +} diff --git a/DiDi/ques2.cpp b/DiDi/ques2.cpp new file mode 100644 index 0000000..0c3fbbe --- /dev/null +++ b/DiDi/ques2.cpp @@ -0,0 +1,77 @@ +/* +小A正在玩游戏,在游戏中一共有n个不同星球,星球间共有m条双向航道,小A的任务是摧毁这些星球。若有多个星球间两两可达,则我们称它们属于同一个联盟。特别的,若一个星球与其他星球间都没有航道,则也称其为一个联盟。小A将按照星球的编号从小到大依次摧毁各个星球,当一个星球被摧毁后,与之相连的航道也将相继被摧毁,现在小A想知道在每个星球被摧毁时还剩下多少个联盟。不同星球间可能有多条航道,但每条航道连接的两个星球必然不同。上述题意可以被简化为,给定n个点,m条边的无向图,按照编号大小依次删去各个节点,请问在每个节点被删去时,还剩下多少个连通块。保证给定图无自环,但可能有重边, + +输入描述 +第一行两个正整数n,m,表示星球数与航道数。接下来m行,每行2个正整数u,v,表示两星球间有一条航道。1 +#define MAXNUM 1000 +int Group[MAXNUM]; //记录图中节点是否被访问过 +int dist[MAXNUM][MAXNUM]; //通过邻接矩阵保存图 +void DFS(int i, int n); +int main() +{ + int n, m, p, q, k = 0; + for (int i = 0; i < MAXNUM; i++) + { + for (int j = 0; j < MAXNUM; j++) + dist[i][j] = 0; //初始化邻接矩阵,初始值为0 + Group[i] = 0; //同上 + } + scanf("%d%d", &n, &m); //获取图的节点数n,和边数m + for (int i = 0; i < m; i++) + { + scanf("%d%d", &p, &q); + dist[p-1][q-1] = 1; //如果节点1和节3相连 则另dist[1][3]和dist[3][1]的值为1 + dist[q-1][p-1] = 1; + } + + for(int zz = 1; zz <= n; zz++){ + for(int j = 1; j <= n; j++){ // 删除节点zz相连的边 + p = zz; + q = j; + dist[p-1][q-1] = 0; + dist[q-1][p-1] = 0; + } + + for (int i = 0; i < MAXNUM; i++) // 清零 + { + Group[i] = 0; + } + + for (int i = 0; i < n; i++) + { + if (Group[i] != 1) + { + DFS(i, n); + k++; //执行一次DFS就是k的值自增1, + } + } + printf("%d", k - zz); // k为删除边但没有删除节点的连通数 zz表示每次删除一个节点 + k = 0; + } + return 0; +} +void DFS(int i,int n) +{ + Group[i] = 1; + for (int j = 0; j < n; j++) + { + if (dist[i][j] == 1) + if (Group[j] != 1) + DFS(j, n); + } +} diff --git a/HuaWei/output/ques.exe b/HuaWei/output/ques.exe new file mode 100644 index 0000000..cb59e60 Binary files /dev/null and b/HuaWei/output/ques.exe differ diff --git a/HuaWei/output/ques2.exe b/HuaWei/output/ques2.exe new file mode 100644 index 0000000..b62f89a Binary files /dev/null and b/HuaWei/output/ques2.exe differ diff --git a/HuaWei/ques.cpp b/HuaWei/ques.cpp new file mode 100644 index 0000000..7d7fa35 --- /dev/null +++ b/HuaWei/ques.cpp @@ -0,0 +1,104 @@ +/* +c++实现以下问题: +有一个 N x N 大小的迷宫。初始状态下,配送员位于迷宫的左上角,他希望前往迷宫的右下角。配送员只能沿若上下左右四个方向移动,从每个格子移动到相邻格子所需要的时间是 1个单位,他必须用最多 K 个(也可以少于 K 个)单位时间到达右下角格子。迷宫的每个格子都有辐射值,配送员必须穿着防护能力不低于相应辐射值的防护服,才能通过该格子。他希望知道,防护服的防护能力最少要达到多少,他才能顺利完成任务。注意:配送员需要通过迷宫的左上角和右下角,因此防护服的防护能力必须大于等于这两个格子的辐射值。 +解答要求: +时间限制: C/C++1000ms,其他语言:2000ms内存限制: C/C++256MB,其他语言:512MB +输入: +前两行各包含一个正整数,分别对应 N 和 K后 N 行各包含 N 整数,以空格分隔,表示地图上每个位置的辐射值,2≤N≤100。 K≥2N-2,以保证题目有解。所有辐射值都是非负整数,绝对值不超过 1e4 +输出: +一个整数,表示配送员穿着防护服的最低防护能力。 + +样例1 +输入: +2 +2 +1 3 +2 1 +输出:2 +解释:配送员可以选择通过左下角(辐射值为2)的路线,耗费2单位时间。 + +样例2 +输入: +5 +12 +0 0 0 0 0 +9 9 3 9 0 +0 0 0 0 0 +0 9 5 9 9 +0 0 0 0 0 +输出:3 +解释:最优路线:往右2格,往下2格,往左2格,往下2格,往右4格,耗费12单位时间,经过格子的最大辐射值为3。 +另外,在地图不变的情况下,如果K=16,输出为0;如果K=8,输出为5。 + +*/ +#include +#include +#include +#include +using namespace std; +const int INF = 1e9; // 定义一个足够大的值表示不可达 +int N, K; +vector> radiation; +// 四个方向的移动 +int dx[] = {1, -1, 0, 0}; +int dy[] = {0, 0, 1, -1}; +// 判断是否可以在指定的防护能力下从 (0,0) 到 (N-1,N-1) +bool canReach(int limit) { + if (radiation[0][0] > limit || radiation[N-1][N-1] > limit) { + return false; + } + vector> visited(N, vector(N, false)); + queue> q; + q.push({0, 0}); + visited[0][0] = true; + int time = 0; + while (!q.empty()) { + int qsize = q.size(); + if (time > K) return false; // 时间超过 K + while (qsize--) { + int x = q.front().first; + int y = q.front().second; + q.pop(); + + if (x == N-1 && y == N-1) { + return true; // 成功到达终点 + } + for (int i = 0; i < 4; ++i) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny] && radiation[nx][ny] <= limit) { + visited[nx][ny] = true; + q.push({nx, ny}); + } + } + } + ++time; // 每次扩展一层,时间增加 + } + return false; // 没有在 K 时间内到达终点 +} + +int main() { + // 读入 N 和 K + cin >> N >> K; + radiation.resize(N, vector(N)); + // 读入迷宫的辐射值 + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + cin >> radiation[i][j]; + } + } + // 二分查找防护能力 + int low = 0, high = 1e4, ans = INF; + while (low <= high) { + int mid = (low + high) / 2; + if (canReach(mid)) { + ans = mid; + high = mid - 1; + } else { + low = mid + 1; + } + } + // 输出结果 + cout << ans << endl; + return 0; +} diff --git a/HuaWei/ques2.cpp b/HuaWei/ques2.cpp new file mode 100644 index 0000000..9fc7af4 --- /dev/null +++ b/HuaWei/ques2.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; +// 日志文件结构体 +struct LogFile { + int process; // 进程序号 + int size; // 文件大小 + int downloads; // 下载次数 +}; +int main() { + int N, M, C; + cin >> N >> M >> C; + vector> logs(N); // 每个进程的日志文件 + // 读取日志文件信息 + for (int i = 0; i < N * M; ++i) { + int process, size, downloads; + cin >> process >> size >> downloads; + logs[process].push_back({process, size, downloads}); + } + int minCapacity = 0; // 最少需要的容量 + vector mustHaveDownloads; // 每个进程必须要保存的日志文件的下载次数 + vector> optionalFiles; // 额外的日志文件,保存 {文件大小,下载次数} + // 选择每个进程的最小日志文件,并记录额外的文件 + for (int i = 0; i < N; ++i) { + // 找到当前进程中最小的文件 + int minSize = 21, minDownloads = 0; + for (const auto& log : logs[i]) { + if (log.size < minSize) { + minSize = log.size; + minDownloads = log.downloads; + } + } + // 计算最少需要的容量 + minCapacity += minSize; + mustHaveDownloads.push_back(minDownloads); + + // 记录下当前进程中非最小的日志文件,作为可选的日志文件 + for (const auto& log : logs[i]) { + if (log.size != minSize || log.downloads != minDownloads) { + optionalFiles.push_back({log.size, log.downloads}); + } + } + } + // 如果最小容量已经超出了U盘容量,返回-1 + if (minCapacity > C) { + cout << -1 << endl; + return 0; + } + // 计算每个进程必须保存的文件的下载次数之和 + int totalDownloads = 0; + for (int d : mustHaveDownloads) { + totalDownloads += d; + } + // 剩余的U盘容量 + int remainingCapacity = C - minCapacity; + // 动态规划数组,dp[j] 表示容量为 j 时的最大下载次数 + vector dp(remainingCapacity + 1, 0); + // 0-1 背包:选择可选的日志文件 + for (const auto& file : optionalFiles) { + int size = file.first; + int downloads = file.second; + for (int j = remainingCapacity; j >= size; --j) { + dp[j] = max(dp[j], dp[j - size] + downloads); + } + } + // 最大的下载次数等于已选择的最小文件的下载次数 + 背包中选出的最大下载次数 + int maxDownloads = totalDownloads + dp[remainingCapacity]; + // 输出结果 + cout << maxDownloads << endl; + return 0; +} diff --git a/LiXiang/TEST.PY b/LiXiang/TEST.PY new file mode 100644 index 0000000..4b27755 --- /dev/null +++ b/LiXiang/TEST.PY @@ -0,0 +1,34 @@ +import math + +# 定义向量x和y +x = (0, 1, 0, 1) +y = (1, 0, 1, 0) + +# 计算余弦相似度 +def cosine(x, y): + numerator = sum([x[i] * y[i] for i in range(len(x))]) + denominator = math.sqrt(sum([x[i] ** 2 for i in range(len(x))])) * math.sqrt(sum([y[i] ** 2 for i in range(len(y))])) + return numerator / denominator + +# 计算相关系数 +def correlation(x, y): + n = len(x) + sum_x = sum(x) + sum_y = sum(y) + sum_xy = sum([x[i] * y[i] for i in range(n)]) + sum_x2 = sum([x[i] ** 2 for i in range(n)]) + sum_y2 = sum([y[i] ** 2 for i in range(n)]) + numerator = n * sum_xy - sum_x * sum_y + denominator = math.sqrt((n * sum_x2 - sum_x ** 2) * (n * sum_y2 - sum_y ** 2)) + return numerator / denominator + +# 计算Jaccard系数 +def jaccard(x, y): + intersection = sum([x[i] and y[i] for i in range(len(x))]) + union = sum([x[i] or y[i] for i in range(len(x))]) + return intersection / union + +# 输出结果 +print('余弦相似度:', cosine(x, y)) +print('相关系数:', correlation(x, y)) +print('Jaccard系数:', jaccard(x, y)) \ No newline at end of file diff --git a/LiXiang/output/ques.exe b/LiXiang/output/ques.exe new file mode 100644 index 0000000..594bf14 Binary files /dev/null and b/LiXiang/output/ques.exe differ diff --git a/LiXiang/ques.cpp b/LiXiang/ques.cpp new file mode 100644 index 0000000..fac6fee --- /dev/null +++ b/LiXiang/ques.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +/*杨辉三角最终显示效果是一个等腰三角形,两个最外边都是1 + 杨辉三角的根本在于,每个数等于它上方两数之和 +*/ +int main(){ + int n;//变量n在此处起到了限制输出行数的作用,可优化成用户输入 + cin>>n; + int i,j,a[n][n]; + + //使第一列和对角线元素的值为1 + for (i=0;i +#include +#include +#include +using namespace std; + +int findClosestSum(vector& nums, int target) { + int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和 + int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和 + vector dp(maxSum + 1, false); // dp数组,初始为 false + dp[0] = true; // 和为 0 是可能的 + // 动态规划填充 dp 数组 + for (int num : nums) { + for (int i = maxSum; i >= num; --i) { + dp[i] = dp[i] || dp[i - num]; + } + } + // 从 dp 数组中找到最接近目标值的和 + int closest = 0; + for (int i = 0; i <= maxSum; ++i) { + if (dp[i] && abs(i - target) < abs(closest - target)) { + closest = i; + } + } + return closest; +} + +int main() { + int n; + vector arr; + int val, sum = 0; + cin >> n; + for(int i = 0; i < n; i++){ + cin >> val; + sum += val; + arr.push_back(val); + } + int arv = sum / 2; + int tmp = findClosestSum(arr, arv); + cout << tmp << endl; + cout << arv - tmp + sum - tmp - arv << endl; + return 0; +} + + +#include +#include +#include +#include + +using namespace std; + +int findClosestSum(vector& nums, int target) { + int n = nums.size(); + int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和 + + int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和 + vector dp(maxSum + 1, false); // dp数组,初始为 false + dp[0] = true; // 和为 0 是可能的 + + // 动态规划填充 dp 数组 + for (int num : nums) { + for (int i = maxSum; i >= num; --i) { + dp[i] = dp[i] || dp[i - num]; + } + } + + // 从 dp 数组中找到最接近目标值的和 + int closest = 0; + for (int i = 0; i <= maxSum; ++i) { + if (dp[i] && abs(i - target) < abs(closest - target)) { + closest = i; + } + } + + return closest; +} + +int main() { + vector nums = {7,3,5,3}; // 输入数组 + int target = 10; // 目标值 + + int result = findClosestSum(nums, target); + + cout << result << endl; + + return 0; +} diff --git a/output/test copy.exe b/output/test copy.exe new file mode 100644 index 0000000..90d50ee Binary files /dev/null and b/output/test copy.exe differ diff --git a/output/test.exe b/output/test.exe new file mode 100644 index 0000000..88eb5bd Binary files /dev/null and b/output/test.exe differ diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..e61987f --- /dev/null +++ b/test.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +// 计算一个数的质因数的最小公倍数 +long long lcm(long long a, long long b) { + return a / __gcd(a, b) * b; +} + +// 计算一个城堡的完美度 +long long calculateCastlePerfectness(const vector>& blocks) { + long long product = 1; + for (const auto& block : blocks) { + product *= pow(block.first, block.second); + } + return product; +} + +int main() { + int n; + cin >> n; + + vector perfectness(n); + vector>> castles(n); + + for (int i = 0; i < n; ++i) { + int ti; + cin >> ti; + castles[i].resize(ti); + for (int j = 0; j < ti; ++j) { + int Pij, Cij; + cin >> Pij >> Cij; + castles[i][j] = {Pij, Cij}; + } + perfectness[i] = calculateCastlePerfectness(castles[i]); + } + + // 计算总完美度 + set uniquePerfectness; + for (int i = 0; i < n; ++i) { + long long currentLcm = 1; + for (int j = 0; j < n; ++j) { + if (j != i) { + currentLcm = lcm(currentLcm, perfectness[j]); + } + } + uniquePerfectness.insert(currentLcm); + } + + // 输出不同的总完美度的种数 + cout << uniquePerfectness.size() << endl; + + return 0; +} \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..c7338af --- /dev/null +++ b/test.py @@ -0,0 +1,35 @@ +def outer(fun): + def wrapper(s): + # 获取字符串的前三个字符 + first_three = s[1:4] + #倒序前三个字符 + reversed_three = first_three[::-1] + # 返回拼接后的字符串 + return first_three + reversed_three + return wrapper +@outer +def fun(s): + return s + + +str = input() +print(fun(str)) + + +# def process_list(li): +# li[:] = li[:6] +# li.sort(reverse=True) +# l1 = li[1::2] +# l2 = li[0::2] +# l2.reverse() +# li[:]= l2 + l1 +# return li + +# # str="[2,1,6,4,5,3,7,8]" +# str=input() +# str=str[1:-1] + +# after5 = str.split(',') +# res = process_list(after5) +# res2 = [int(i) for i in res] +# print(res2) \ No newline at end of file