Browse Source

第一次提交

bijiben
zzwcreator 2 weeks ago
commit
2163c32f56
  1. 52
      .vscode/settings.json
  2. 38
      DiDi/ques1.cpp
  3. 77
      DiDi/ques2.cpp
  4. BIN
      HuaWei/output/ques.exe
  5. BIN
      HuaWei/output/ques2.exe
  6. 104
      HuaWei/ques.cpp
  7. 72
      HuaWei/ques2.cpp
  8. 34
      LiXiang/TEST.PY
  9. BIN
      LiXiang/output/ques.exe
  10. 32
      LiXiang/ques.cpp
  11. BIN
      XiaoHongShu/output/ques.exe
  12. 92
      XiaoHongShu/ques.cpp
  13. BIN
      output/test copy.exe
  14. BIN
      output/test.exe
  15. 59
      test.cpp
  16. 35
      test.py

52
.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"
}
}

38
DiDi/ques1.cpp

@ -0,0 +1,38 @@
/*
使mn的数组和一个整数
*/
#include <iostream>
#include <vector>
using namespace std;
int maxConsecutiveElementsUnderSum(const std::vector<int>& 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<int> 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;
}

77
DiDi/ques2.cpp

@ -0,0 +1,77 @@
/*
A正在玩游戏n个不同星球m条双向航道A的任务是摧毁这些星球A将按照星球的编号从小到大依次摧毁各个星球A想知道在每个星球被摧毁时还剩下多少个联盟n个点m条边的无向图
n,mm行2u,v1<n,m<5e4
n个正整数
5 6
1 2
2 3
3 1
4 5
5 1
2 4
1 2 1 1 0
*/
#include <stdio.h>
#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);
}
}

BIN
HuaWei/output/ques.exe

BIN
HuaWei/output/ques2.exe

104
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 ,2N100 K2N-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
:线:22224123
K=160;K=85
*/
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 1e9; // 定义一个足够大的值表示不可达
int N, K;
vector<vector<int>> 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<vector<bool>> visited(N, vector<bool>(N, false));
queue<pair<int, int>> 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<int>(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;
}

72
HuaWei/ques2.cpp

@ -0,0 +1,72 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 日志文件结构体
struct LogFile {
int process; // 进程序号
int size; // 文件大小
int downloads; // 下载次数
};
int main() {
int N, M, C;
cin >> N >> M >> C;
vector<vector<LogFile>> 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<int> mustHaveDownloads; // 每个进程必须要保存的日志文件的下载次数
vector<pair<int, int>> 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<int> 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;
}

34
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))

BIN
LiXiang/output/ques.exe

32
LiXiang/ques.cpp

@ -0,0 +1,32 @@
#include <iostream>
#include <iomanip>
using namespace std;
/*杨辉三角最终显示效果是一个等腰三角形,两个最外边都是1
*/
int main(){
int n;//变量n在此处起到了限制输出行数的作用,可优化成用户输入
cin>>n;
int i,j,a[n][n];
//使第一列和对角线元素的值为1
for (i=0;i<n;i++){ //前两行全为1,拿出来单独处理
a[i][i]=1;//使最右侧边全为1
a[i][0]=1;//使最左侧边全为1
}
//从第三行开始处理
for (i=2;i<n;i++) //三行开始出现变化
for (j=1;j<=i-1;j++) //j始终慢i一步
a[i][j]=a[i-1][j-1]+a[i-1][j];//每个数等于它上方两数之和,如a32=a21+a22
//输出数组各元素的值
for (i=0;i<n;i++){ //从第一行开始
for (j=0;j<=i;j++) //利用j将每一行的数据全部输出
cout<<a[i][j]<<" "; //在C++中,setw(int n)用来控制输出间隔,这里是指前元素末尾与后元素末尾之间有个5空格位
cout<<endl;
}
//cout<<endl;
return 0;
}

BIN
XiaoHongShu/output/ques.exe

92
XiaoHongShu/ques.cpp

@ -0,0 +1,92 @@
/*
PK!ai代表目己的战斗力使
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int findClosestSum(vector<int>& nums, int target) {
int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和
int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和
vector<bool> 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<int> 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 <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int findClosestSum(vector<int>& nums, int target) {
int n = nums.size();
int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和
int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和
vector<bool> 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<int> nums = {7,3,5,3}; // 输入数组
int target = 10; // 目标值
int result = findClosestSum(nums, target);
cout << result << endl;
return 0;
}

BIN
output/test copy.exe

BIN
output/test.exe

59
test.cpp

@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <set>
#include <numeric>
#include <algorithm>
#include <cmath>
using namespace std;
// 计算一个数的质因数的最小公倍数
long long lcm(long long a, long long b) {
return a / __gcd(a, b) * b;
}
// 计算一个城堡的完美度
long long calculateCastlePerfectness(const vector<pair<int, int>>& 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<long long> perfectness(n);
vector<vector<pair<int, int>>> 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<long long> 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;
}

35
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)
Loading…
Cancel
Save