Skip to content

Commit

Permalink
408 2010 42 Brute-Force
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeBottle committed Jul 30, 2023
1 parent 23bfbfe commit 1f972de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
41 changes: 41 additions & 0 deletions Algo/code/C-Cpp/DataStructure/408-2010-BF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <cstdio>

using namespace std;

int main()
{
int seq[] = {1, 2, 3, 4, 5, 6, 7, 8};
int seqLen = sizeof(seq) / sizeof(seq[0]);
int p = 12; // 循环左移位数
// 以下是暴力算法。
p = p % seqLen; // 可能会左移超过一个序列的长度,先模一下,得到实际要移动的位数。
// 开一个一样大的临时数组
int tempArr[seqLen];
int i = 0; // i指向临时数组
for (; i < seqLen - p; i++)
{
// 先把原序列的[p,seqLen-1]这部分读入临时数组的前半部分
tempArr[i] = seq[i + p];
}
for (int j = 0; j < p; j++)
{
// 再把原序列的[0,p-1]这部分读入临时数组的后半部分
tempArr[i++] = seq[j];
}
// 打印结果
printf("Result: ");
for (int i = 0; i < seqLen; i++)
printf("%d ", tempArr[i]);
printf("\n");
return 0;
}

/*
这一题主要是一个线性表循环移位问题,把线性表循环左移p位。
这个源代码是我第一时间能想到的暴力解(Brute-Force)。
主要思路就是把序列分为两半来看待,将两半对调即可,借用一个辅助数组。
这题可能被坑的地方就在于,需要考虑到循环移位次数超过序列长度的情况。
- SomeBottle 2023.7.30
*/
5 changes: 3 additions & 2 deletions Algo/code/C-Cpp/DataStructure/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# 咱在其他地方做过的数据结构题
# 数据结构相关题

数据结构的题在各大OJ平台多多少少都有一些,这里列出的是我做过的数据结构题 (不包含**蓝桥杯、PTA平台**的题目)。

| 平台-题目编号 | 题目 | 我的题解 | 值得玩味 |备注|
|:---:|:---:|:---:|:---:|:---:|
|Dotcpp-1713|[平衡二叉树的基本操作](https://www.dotcpp.com/oj/problem1713.html)|[AVLTree-Insertion.cpp](./AVLTree-Insertion.cpp)|| 主要考察**AVL**(二叉平衡搜索)树的**插入****查找**操作 |
|Dotcpp-1713|[平衡二叉树的基本操作](https://www.dotcpp.com/oj/problem1713.html)|[AVLTree-Insertion.cpp](./AVLTree-Insertion.cpp)|| 主要考察**AVL**(二叉平衡搜索)树的**插入****查找**操作 |
|408-2010统考-42题|[循环左移p位](https://zhuanlan.zhihu.com/p/574160078)|暴力解:[408-2010-BF.cpp](./408-2010-BF.cpp)| | 要考虑到循环左移超过一个序列长度的情况。 |

0 comments on commit 1f972de

Please sign in to comment.