//直插
#include<iostream>
using namespace std;
void print(int*& array, int n) {//打印结果
for (int i = 1; i <= n; i++) {
cout << array[i];
if (i != n)
cout << ' ';
else
cout << endl;
}
}
int main() {
int n;
cin >> n;
int *array = new int[n + 5];
for (int i = 1; i <= n; i++)
cin >> array[i];
for (int i = 2; i <= n; i++) {//直插排序
array[0] = array[i];
for (int j = i - 1; j >= 1; j--) {
if (array[j] > array[0]) {
array[j + 1] = array[j];
array[j] = array[0];//在将大的后移的同时将该为置为array【0】
}
else {
array[j + 1] = array[0];
break;
}
}
print(array, n);//打印每次的结果
}
}
//希尔降序
#include <iostream>
using namespace std;
void shell_sort()
{
int *data;
int n;
cin >> n;
data = new int[n];
for (int i = 0; i < n; i++)
cin >> data[i];
for (int gap = n / 2; gap > 0; gap /= 2) //间隔从n/2降到1
{
for (int j = 0; j < gap; j++) //根据gap划分为n个组
{
for (int p = j; p < n; p += gap) //对这些组进行2层循环插入排序
{
for (int k = p; k > 0; k -= gap)
{
if (k - gap >= 0 && data[k] > data[k - gap])
swap(data[k], data[k - gap]);
else
break;
}
}
}
for (int j = 0; j < n; j++) //输出数据
{
cout << data[j];
if (j != n - 1)
cout << ' ';
}
cout << endl;
}
delete[] data;
}
int main()
{
int t;
cin >> t;
while (t--)
{
shell_sort();
cout << endl;
}
}
//kuaipai
#include <iostream>
using namespace std;
void Quick_sort(int nums[], int start_loc, int end_loc, int n)
{
int temp;
if (end_loc - start_loc > 0) //前面不断根据数据交换,一直交换到中间,最后枢纽元取中间的数的下标i
{
int i = start_loc, j = end_loc, flag = 0;
while (i < j)
{
if (nums[i] > nums[j])
{
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
if (flag == 0)
{
i++;
flag = 1;
}
else
{
j--;
flag = 0;
}
}
else
{
if (flag == 0)
{
j--;
}
else
{
i++;
}
}
}
for (int k = 0; k < n; k++) //输出数组
{
cout << nums[k];
if (k != n - 1)
cout << ' ';
}
cout << endl;
Quick_sort(nums, start_loc, i - 1, n);
Quick_sort(nums, i + 1, end_loc, n);
}
}
int main()
{
int t, n;
cin >> t;
int nums[100];
while (t--)
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> nums[i];
Quick_sort(nums, 0, n - 1, n);
cout << endl;
}
}
//小丁鬼牌
#include <iostream>
using namespace std;
void heapify(int *nums, int loc, int n) //对长度为n的堆的第loc个结点进行维护
{
int left = 2 * loc + 1;
int right = 2 * loc + 2;
if (left < n)
{
if (right < n)
{
if (nums[left] > nums[right])
{
if (nums[right] < nums[loc]) //若右孩子为最小值,则交换右孩子与父节点的值,然后重新维护右孩子的子树的父子大小关系
{
swap(nums[right], nums[loc]);
heapify(nums, right, n);
}
}
else
{
if (nums[left] < nums[loc]) //若左孩子为最小值,则交换左孩子与父节点,然后重新维护左孩子的子树的父子大小关系
{
swap(nums[left], nums[loc]);
heapify(nums, left, n);
}
}
}
else
{
if (nums[left] < nums[loc]) //若左孩子为最小值,则交换左孩子与父节点,然后重新维护左孩子的子树的父子大小关系
{
swap(nums[left], nums[loc]);
heapify(nums, left, n);
}
}
}
}
void heap_sort(int n)
{
int *nums;
nums = new int[n];
for (int i = 0; i < n; i++)
cin >> nums[i];
for (int i = n / 2; i >= 0; i--) //建堆过程:从第n/2个结点开始递减到第1个结点,依次维护结点的父子大小关系
{
heapify(nums, i, n);
}
cout << n << ' ';
for (int i = 0; i < n; i++) //输出堆
{
cout << nums[i];
if (i != n - 1)
cout << ' ';
}
cout << endl;
for (int t = n - 1; t >= 1; t--)
{
swap(nums[0], nums[t]); //将第1个结点与最后一个结点交换
heapify(nums, 0, t); //然后维护第1个结点的父子关系
cout << n << ' ';
for (int i = 0; i < n; i++) //输出堆
{
cout << nums[i];
if (i != n - 1)
cout << ' ';
}
cout << endl;
}
}
int main()
{
int t;
cin >> t;
heap_sort(t);
}
//顺序链表
插入和删除
#include <iostream>
using namespace std;
class SeqList {
int *list;
int len;
int maxLen;
public:
SeqList(int n);
~SeqList();
void multiInsert(int i, int n, int *item);
void multiDel(int i, int n);
void outPut();
};
SeqList::SeqList(int n) {
maxLen = 1000;
len = n;
list = new int[maxLen];
for (int i = 0; i < n; i++)
cin >> list[i];
}
SeqList::~SeqList() {
delete[]list;
}
void SeqList::multiInsert(int i, int n, int *item) {
if (len + n > maxLen || i<1 || i>len + 1)
return;
for (int j = len - 1; j >= i - 1; j--)
list[j + n] = list[j];
for (int j = i - 1; j < i - 1 + n; j++)
list[j] = item[j - i + 1];
len += n;
}
void SeqList::multiDel(int i, int n) {
if (i<1 || i>len || i + n - 1 > len)
return;
for (int j = i - 1 + n; j < len; j++)
list[j - n] = list[j];
len -= n;
}
void SeqList::outPut() {
cout << len << ' ';
for (int i = 0; i < len; i++)
cout << list[i] << ' ';
cout << endl;
}
int main()
{
int n;
cin >> n;
SeqList myList(n);
myList.outPut();
int i, *item;
cin >> i >> n;
item = new int[n];
for (int j = 0; j < n; j++)
cin >> item[j];
myList.multiInsert(i, n, item);
myList.outPut();
delete[]item;
cin >> i >> n;
myList.multiDel(i, n);
myList.outPut();
return 0;
}
//合并
void mix(const seqlist &r1, const seqlist &r2)
{
int i, j = 0, k = 0;
int size_p = r2.size;
size = r1.size + r2.size;
int *p = new int[r2.size];
for (i = 0; i < r2.size; i++)
p[i] = r2.list[i];
for (i = 0; i < r1.size; i++)
{
if (r1.list[i] < p[k])
{
list[j] = r1.list[i];
j++;
continue;
}
else
{
list[j] = p[k];
j++;
k++;
i--;
size_p--;
}
if (size_p == 0)
{
i++;
for (; i < r1.size; i++, j++)
{
list[j] = r1.list[i];
}
break;
}
}
if (size_p != 0)
{
for (i = 0; i < size_p; i++, j++)
{
list[j] = p[i + k];
}
}
}
//单链表 结点交换
#include <iostream>
using namespace std;
class LNode {
int data;
LNode *next;
LNode() { next = NULL; }
friend class LinkList;
};
class LinkList {
LNode *head;
int len;
public:
LinkList();
~LinkList();
void CreateInTail(int n);
void outPut();
bool swap(int pa, int pb);
LNode* Index(int i);
};
LinkList::LinkList() {
head = new LNode;
len = 0;
}
LinkList::~LinkList() {
LNode *p = head, *q;
while (p) {
q = p;
p = p->next;
delete q;
}
head = NULL;
len = 0;
}
void LinkList::CreateInTail(int n) {
len = n;
LNode *tail = head;
while (n--)
{
int item;
cin >> item;
LNode *s = new LNode;
s->data = item;
tail->next = s;
tail = s;
}
}
void LinkList::outPut() {
LNode *p = head->next;
while (p)
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
}
bool LinkList::swap(int pa, int pb) {
if (pa<1 || pb<1 || pa>len || pb>len)
return false;
LNode *p = Index(pa - 1), *q = Index(pb - 1);
if (p->next == q)//防止相邻节点爆炸
{
p->next = q->next;
q->next = p->next->next;
p->next->next = q;
return true;
}
LNode *t = p->next;//交换前驱
p->next = q->next;
q->next = t;
t = p->next->next;//交换后继
p->next->next = q->next->next;
q->next->next = t;
return true;
}
LNode *LinkList::Index(int i) {
if (i<0 || i>len)
return NULL;
LNode *p = head;
for (int j = 0; j < i; j++)
p = p->next;
return p;
}
int main()
{
int n;
cin >> n;
LinkList myList;
myList.CreateInTail(n);
myList.outPut();
int pa, pb;
cin >> pa >> pb;
if (myList.swap(pa, pb))
myList.outPut();
else
cout << "error" << endl;
cin >> pa >> pb;
if (myList.swap(pa, pb))
myList.outPut();
else
cout << "error" << endl;
return 0;
}
//队列
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
#include <iomanip>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
double dou;
double res;
int in;
stack<char> st;
queue<char> que;
cin >> dou >> in;
int inter = (int)dou;
double dec = dou - inter;
while (inter) {
int k = inter % in;
inter /= in;
if (k > 9)
st.push('A' + k - 10);
else
st.push(k + '0');
}
while (dec) {
dec = dec * in;
int k = (int)dec;
if (k > 9) {
que.push(k + 'A' - 10);
}
else
que.push(k + '0');
dec -= k;
}
if (st.empty())
cout << "0.";
else {
while (!st.empty()) {
cout << st.top();
st.pop();
}
cout << '.';
}
if (que.empty())
cout << "000";
else {
int o = 3;
while (o--) {
if (que.empty())
cout << '0';
else {
cout << que.front();
que.pop();
}
}
}
cout << endl;
}
return 0;
}
//#include <iostream>
#include <queue>
#include <iomanip>
#include <string>
using namespace std;
class customer {
public:
int come_time, use_time;
customer(int c_t, int u_t) {
come_time = c_t;
use_time = u_t;
}
};
class window {
public:
bool is_busy;
int finish_time;
window() :is_busy(0), finish_time(0) {}
};
int main() {
int n, come_time, use_time, k;
int all_wait_time = 0, wait_time = 0, finish_time = 0, max_wait_time = 0;
queue<customer> cus;
cin >> n; //顾客人数
int n1 = n;
while (n1--) {
cin >> come_time >> use_time;
cus.push(customer(come_time, use_time));
}
cin >> k; //窗口个数
window *win = new window[k];
int clock = 0;
while (!cus.empty()) {
if (clock >= cus.front().come_time) { //顾客到达窗口
for (int i = 0; i < k; i++) {
if (win[i].finish_time <= clock) { //重置每个窗口的空闲状态
win[i].is_busy = 0;
}
}
for (int i = 0; i < k; i++) { //判断每一个窗口是否空闲
if (!win[i].is_busy && (!cus.empty()) && (clock >= cus.front().come_time)) {
win[i].is_busy = 1;
win[i].finish_time = clock + cus.front().use_time;
wait_time = clock - cus.front().come_time;
all_wait_time += wait_time;
if (wait_time > max_wait_time) {
max_wait_time = wait_time;
}
if (win[i].finish_time > finish_time) {
finish_time = win[i].finish_time;
}
cus.pop();
}
}
}
clock++;
}
cout << fixed << setprecision(1) << (double)all_wait_time / n << " " << max_wait_time << " " << finish_time << endl;
delete[]win;
return 0;
}
//kmp
#include <iostream>
#include <string>
using namespace std;
class myString {
private:
string mainstr;
int size;
void GetNext(string p, int next[]) {
next[0] = -1;
next[1] = 0;
int L = p.length();
int temp = 0;
for (int i = 2; i < L; i++) {
while (p[i - 1] != p[temp] && temp > 0) {
temp = next[temp];
}
if (p[i - 1] == p[temp]) {
temp++;
}
next[i] = temp;
}
}
int KMPFind(string p, int pos, int next[]) {
int i = pos, j = 0;
while (mainstr[i] != '\0' && p[j] != '\0') {
if (mainstr[i] == p[j]) {
++i;
++j;
}
else {
j = next[j];
}
if (j == -1) {
++i;
++j;
}
}
if (p[j] == '\0')
return i - j;
else
return -1;
}
public:
myString() { size = 0; mainstr = ""; }
myString(string s) { size = s.length(); mainstr = s; }
~myString() { size = 0; mainstr = ""; }
void SetVal(string sp) {
mainstr = "";
mainstr.assign(sp);
size = mainstr.length();
}
int KMPFindSubstr(string p, int pos) {
int i;
int L = p.length();
int *next = new int[L];
GetNext(p, next);
for (i = 0; i < L; i++) {
cout << next[i] << ' ';
}
cout << endl;
int v = -1;
v = KMPFind(p, 0, next);
delete[] next;
return v;
}
};
int main()
{
int t;
cin >> t;
while (t--)
{
string templet;
cin >> templet;
string target;
cin >> target;
myString str(templet);
cout << str.KMPFindSubstr(target, 0) + 1 << endl;
}
return 0;
}
//霍夫曼树
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int MaxW = 9999999; // 假设结点权值不超过9999999
// 定义huffman树结点类
class HuffNode
{
public:
int weight; // 权值
int parent; // 父结点下标
int leftchild; // 左孩子下标
int rightchild; // 右孩子下标
};
// 定义赫夫曼树类
class HuffMan
{
private:
void MakeTree(); // 建树,私有函数,被公有函数调用
void SelectMin(int pos, int* s1, int* s2); // 从 1 到 pos 的位置找出权值最小的两个结点,把结点下标存在 s1 和 s2 中
public:
int len; // 结点数量
int lnum; // 叶子数量
HuffNode* huffTree; // 赫夫曼树,用数组表示
string* huffCode; // 每个字符对应的赫夫曼编码
void MakeTree(int n, int wt[]); // 公有函数,被主函数main调用 ----这里wt【】是啥?---权值
void Coding(); // 公有函数,被主函数main调用
void Destroy();
};
// 构建huffman树
void HuffMan::MakeTree(int n, int wt[])// 公有函数,对外接口
{
// 参数是叶子结点数量和叶子权值
int i;
lnum = n;//椰子树
len = 2 * n - 1;//节点数
huffTree = new HuffNode[2 * n];//开辟数组用节点表示
huffCode = new string[lnum + 1]; // 位置从 1 开始计算
// huffCode实质是个二维字符数组,第 i 行表示第 i 个字符对应的编码
// 赫夫曼树huffTree初始化
for (i = 1; i <= n; i++)//n是叶子数
huffTree[i].weight = wt[i - 1]; // 第0号不用,从1开始编号
for (i = 1; i <= len; i++)//置零
{
if (i > n)//这里i依旧是从1开始但是前面n个用这个if'句式给截断再前面了
huffTree[i].weight = 0; // 前n个结点是叶子,已经设置
huffTree[i].parent = 0;
huffTree[i].leftchild = 0;
huffTree[i].rightchild = 0;
}
MakeTree(); // 调用私有函数建树
}
void HuffMan::SelectMin(int pos, int* s1, int* s2)
{
// 找出最小的两个权值的下标
// 函数采用地址传递的方法,找出两个下标保存在 s1 和 s2 中
int w1, w2, i;
w1 = w2 = MaxW; // 初始化w1和w2为最大值,在比较中会被实际的权值替换
*s1 = *s2 = 0;
for (i = 1; i <= pos; i++)
{
if (huffTree[i].parent == 0 && huffTree[i].weight < w1)
{
w2 = w1;
*s2 = *s1;//?
w1 = huffTree[i].weight;
*s1 = i;
}
else if (huffTree[i].weight < w2 && huffTree[i].parent == 0)
{
w2 = huffTree[i].weight;
*s2 = i;
}
}
}
void HuffMan::MakeTree()
{
// 私有函数,被公有函数调用
int i, s1, s2;
for (i = lnum + 1; i <= len; i++)//叶子数+1
{
SelectMin(i - 1, &s1, &s2); // 找出两个最小权值的下标放入 s1 和 s2 中
huffTree[s1].parent = i;
huffTree[s2].parent = i;
huffTree[i].weight = huffTree[s1].weight + huffTree[s2].weight;
huffTree[i].leftchild = s1;
huffTree[i].rightchild = s2;
}
}
// 销毁赫夫曼树
void HuffMan::Destroy()
{
len = 0;
lnum = 0;
delete[]huffTree;
delete[]huffCode;
}
// 赫夫曼编码
void HuffMan::Coding()
{
char* cd;
int i, c, f, start;
// 求 n 个结点的赫夫曼编码
cd = new char[lnum]; // 分配求编码的工作空间
cd[lnum - 1] = '\0'; // 编码结束符
for (i = 1; i <= lnum; ++i)
{
// 逐个字符求赫夫曼编码
start = lnum - 1; // 编码结束符位置
// 参考课本P147算法6.12 HuffmanCoding代码
for (c = i, f = huffTree[i].parent; f != 0; c = f, f = huffTree[f].parent)
{
if (huffTree[f].leftchild == c)
{
cd[--start] = '0';
}
else
{
cd[--start] = '1';
}
}
huffCode[i] = new char[lnum - start];
huffCode[i].assign(&cd[start]); // 把cd中从start到末尾的编码复制到huffCode中
}
delete[]cd; // 释放工作空间
}
// 主函数
int main()
{
int t, n, i, j;
int wt[800];
HuffMan myHuff;
cin >> t;
for (i = 0; i < t; i++)
{
cin >> n;
for (j = 0; j < n; j++)
{
cin >> wt[j];//它的wt【】指的是有n个权值
}
myHuff.MakeTree(n, wt);
myHuff.Coding();
for (j = 1; j <= n; j++)
{
cout << myHuff.huffTree[j].weight << '-'; // 输出各权值
cout << myHuff.huffCode[j] << endl; // 输出各编码
}
myHuff.Destroy();
}
return 0;
}
//#include <iostream>
using namespace std;
class BiNode {
char data;
BiNode *lChild;
BiNode *rChild;
public:
BiNode() :lChild(NULL), rChild(NULL) {}
BiNode(char e) :data(e), lChild(NULL), rChild(NULL) {}
~BiNode() { delete lChild; delete rChild; }
friend class BiTree;
};
class BiTree {
BiNode *root;
void CreateTree(BiNode *&t);
void PreOrder(BiNode *t);
void MidOrder(BiNode *t);
void PostOrder(BiNode *t);
public:
BiTree() :root(NULL) {}
~BiTree() { delete root; }
void CreateTree();
void PreOrder();
void MidOrder();
void PostOrder();
};
void BiTree::CreateTree(BiNode *&t) {
char c;
cin >> c;
if (c != '0')
{
t = new BiNode(c);
CreateTree(t->lChild);
CreateTree(t->rChild);
}
else
t = NULL;
}
void BiTree::CreateTree() {
CreateTree(root);
}
void BiTree::PreOrder(BiNode *t) {
if (t)
{
cout << t->data;
PreOrder(t->lChild);
PreOrder(t->rChild);
}
}
void BiTree::PreOrder() {
PreOrder(root);
cout << endl;
}
void BiTree::MidOrder(BiNode *t) {
if (t)
{
MidOrder(t->lChild);
cout << t->data;
MidOrder(t->rChild);
}
}
void BiTree::MidOrder() {
MidOrder(root);
cout << endl;
}
void BiTree::PostOrder(BiNode *t) {
if (t)
{
PostOrder(t->lChild);
PostOrder(t->rChild);
cout << t->data;
}
}
void BiTree::PostOrder() {
PostOrder(root);
cout << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
BiTree myTree;
myTree.CreateTree();
myTree.PreOrder();
myTree.MidOrder();
myTree.PostOrder();
}
}