Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better methods, lower time complexity or space complexity #55

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions 经典示例/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"files.associations": {
"*.C": "c",
"array": "c",
"atomic": "c",
"*.tcc": "c",
"cctype": "c",
"clocale": "c",
"cmath": "c",
"cstdarg": "c",
"cstddef": "c",
"cstdint": "c",
"cstdio": "c",
"cstdlib": "c",
"cwchar": "c",
"cwctype": "c",
"deque": "c",
"unordered_map": "c",
"vector": "c",
"exception": "c",
"algorithm": "c",
"memory": "c",
"memory_resource": "c",
"optional": "c",
"string": "c",
"string_view": "c",
"system_error": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"fstream": "c",
"initializer_list": "c",
"iosfwd": "c",
"iostream": "c",
"istream": "c",
"limits": "c",
"new": "c",
"ostream": "c",
"sstream": "c",
"stdexcept": "c",
"streambuf": "c",
"typeinfo": "c"
}
}
24 changes: 24 additions & 0 deletions 经典示例/008.数列求和/8.1.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*输入一个正整数 n。
对于每个整数 i(从 1 到 n),计算从 1 到 i 的累加和,并将这个结果累加到总和 sum 中。*/
// 通过直接使用数学公式代替循环,减少了时间复杂度,降到了 O(1)

#include <stdio.h>

int main() {
int n;
long sum = 0;

printf("Please input a number to n:\n");
scanf("%d", &n);

if (n < 1) {
printf("The n must not be less than 1!\n");
return 1;
}

// 使用公式计算总和
sum = (n * (n + 1) * (n + 2)) / 6;

printf("The sum of the sequence(%d) is %ld\n", n, sum);
return 0;
}
91 changes: 91 additions & 0 deletions 经典示例/059.约瑟夫环2022211861fix/2022211861fix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>

#define N 7
#define OVERFLOW 0
#define OK 1

typedef struct LNode {
int password;
int order;
struct LNode *next;
} LNode, *LinkList;

// 创建循环链表
LinkList createCircularList(const int *passwords, int n) {
LinkList head = (LinkList)malloc(sizeof(LNode)); // 动态分配头节点内存
if (!head) return NULL; // 如果分配失败,返回NULL
head->password = passwords[0];
head->order = 1;
head->next = NULL;
LinkList current = head;

// 改动1:使用循环创建链表节点,并检查内存分配是否成功
for (int i = 1; i < n; ++i) {
LinkList newNode = (LinkList)malloc(sizeof(LNode)); // 动态分配新节点内存
if (!newNode) return NULL; // 如果分配失败,返回NULL
newNode->password = passwords[i];
newNode->order = i + 1;
current->next = newNode; // 链接新节点
current = newNode; // 更新当前节点指针
}
current->next = head; // 形成循环链表
return head;
}

// 约瑟夫环问题的核心算法
void josephus(LinkList head, int step, int count) {
if (count <= 0) return; // 如果链表为空,直接返回

// 改动2:对步长进行求余处理,并处理特殊情况
step %= count; // m对x求余
if (step == 0) step = count; // 如果刚好整除,设置为最后一位

LinkList current = head;
for (int i = 1; i < step; ++i) {
current = current->next; // 寻找要删除节点的前一个节点
}

// 改动3:删除节点并释放内存
LinkList toRemove = current->next;
printf("%d ", toRemove->order); // 输出被删除节点的order值
current->next = toRemove->next; // 从循环链表中删除节点
free(toRemove); // 释放被删除节点的内存

// 改动4:递归调用自身
josephus(current->next, toRemove->password, --count); // 递归调用自身
}

int main() {
int PassW[N] = {3, 1, 7, 2, 4, 8, 4};
int m;

// 改动5:增加输入验证
printf("请输入第一次计数值m: \n");
if (scanf("%d", &m) != 1 || m < 1) { // 输入验证
printf("无效的输入。\n");
return OVERFLOW;
}
printf("第一次计数值m= %d \n", m);

// 改动6:检查头节点是否创建成功
LinkList head = createCircularList(PassW, N);
if (!head) { // 检查头节点是否创建成功
printf("内存分配失败。\n");
return OVERFLOW;
}

printf("约瑟夫环问题的结果顺序是:\n");
josephus(head, m, N);
printf("\n");

// 改动7:释放剩余的内存
LinkList temp = head;
do {
LinkList next = temp->next;
free(temp); // 释放当前节点内存
temp = next; // 移动到下一个节点
} while (temp != head);

return OK;
}
Binary file not shown.
54 changes: 54 additions & 0 deletions 经典示例/059.约瑟夫环2022211861fix/59.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <malloc.h>
#define N 7 //����N=7����ʾ��7��������Ԫ
#define OVERFLOW 0
#define OK 1
typedef struct LNode{ //���������ṹ
int password;
int order;
struct LNode *next;
}LNode,*LinkList;

int PassW[N]={3,1,7,2,4,8,4};
void Joseph(LinkList p,int m,int x); //��������
int main()
{
int i,m;
LinkList Lhead,p,q; //��������ָ�������ṹ��ָ��
Lhead=(LinkList)malloc(sizeof(LNode)); //��ʼ��ͷ�ڵ�
if(!Lhead)return OVERFLOW; //����ռ�ʧ�ܷ���
Lhead->password=PassW[0];
Lhead->order=1;
Lhead->next=NULL;
p=Lhead;
for(i=1;i<7;i++){
if(!(q=(LinkList)malloc(sizeof(LNode))))return OVERFLOW;
q->password=PassW[i]; //��ʼ��ѭ���б��е�����ֵ
q->order=i+1;
p->next=q;p=q; //�´���һ��ָ��ڵ㲢ʹp->nextָ����,��ʹp=q
}
p->next=Lhead; //ʹp->nextָ��ͷ�ڵ�,�Ӷ��γ�ѭ������
printf("�������һ�μ���ֵm: \n");
scanf("%d",&m); //�û������һ�μ���ֵm
printf("��һ�μ���ֵm= %d \n",m);
Joseph(p,m,N);
return OK;
}
void Joseph(LinkList p,int m,int x){
LinkList q;
int i;
if(x==0)return; //������û�нڵ�Ļ�,����������һ�㺯��
q=p;
m%=x; //m��x����,�Ӷ���������еĵڼ�����Ԫ������ڵ�
if(m==0)m=x; //��m�պÿ�������x,����m=x,��Ϊ���m=0,�򲻽�����һ��
//forѭ��,�������޷�ʹqָ��Ҫɾ���ڵ�,pָ�����ĵ�ǰһ�ڵ�,�������޷�����ɾ������
for(i=1;i<=m;i++){
p=q;
q=p->next; //ʹqָ��Ҫɾ���Ľڵ�,pָ��q��ǰһ���ڵ�
}
p->next=q->next; //��ѭ��������ɾ��qָ��Ľڵ�
i=q->password;
printf("%d ",q->order);
free(q); //�ͷ�qָ��Ŀռ�
Joseph(p,i,x-1);
}
Binary file not shown.
54 changes: 54 additions & 0 deletions 经典示例/195.括号匹配/195.1.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <string>
#include <stack>
#include <stdexcept>

const int MaxLength = 100; // 最大的字符串长度

// 括号匹配函数
void PrintMatchedPairs(const std::string& expr)
{
std::stack<int> s; // 用于存储左括号的索引
for (size_t i = 0; i < expr.length(); ++i) {
if (expr[i] == '(') {
s.push(i); // 遇到左括号,将其索引压入栈
} else if (expr[i] == ')') {
if (s.empty()) {
std::cout << "没有对应第" << (i + 1) << "个右括号的左括号" << std::endl;
} else {
int j = s.top();
s.pop();
std::cout << (i + 1) << "\t" << (j + 1) << std::endl; // 打印匹配的括号对
}
}
}

// 堆栈中所剩下的(都是未匹配的
while (!s.empty()) {
int j = s.top();
s.pop();
std::cout << "没有对应第" << (j + 1) << "个左括号的右括号" << std::endl;
}
}

// 主函数
int main()
{
try {
std::string expr;
std::cout << "请输入符号个数小于" << MaxLength << "的表达式:" << std::endl;
std::getline(std::cin, expr);

if (expr.length() > MaxLength) {
throw std::runtime_error("输入的表达式超过了最大长度限制");
}

std::cout << "括号对是:" << std::endl;
PrintMatchedPairs(expr);
} catch (const std::exception& e) {
std::cerr << "发生错误: " << e.what() << std::endl;
return 1;
}

return 0;
}
Loading