4
5
6
9
//3.9
void ditui(int n)
{
if (n > 1)
{
printf(“%d”,n);
ditui(n - 1);
}
}
17
//3.17
//(1)用数组实现
//返回0表示字符不符合要求,反之
int symmetry(char a[])//取字符串的中间看两遍是否对称
{
int i = 0, j = 0;
while (a[i] != '@') //查看字符的个数
{
i++;
}
for (; j < i / 2; j++)
{
if (a[j] == '&') //如果序列含&,直接返回0
return 0;
if (a[j] != a[i])
return 0;
i--;
}
return 1;
}
//(2)用堆栈实现
int symmetry(char a[])//用堆栈实现倒着的字符对比
{
int i = 0;
Stack S;
InitStack(S);
char x;
while (a[i] != '&' && a[i] != '@')
{
Push(S, a[i]);
i++;
}
i ++;
while (a[i])
{
Pop(S, x);
if (x != a[i])
{
DestroyStack(S);
return 0;
}
i++;
}
return 1;
}
19
https://blog.csdn.net/qq_39993896/article/details/79116303
假设一个算术表达式中可以包含三种括号:圆括号“()”,方括号“【】”及花括号“{}”,且这三种括号可以任意次序嵌套。编写算法判断给定表达式中所含括号是否配对出现。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//1.建个堆栈,遇到([{就压栈,遇到)] } 就看现在栈顶里放的跟遇到的是不是匹配。是,出栈,不是,报错。
//2.如果遇到) ] } 栈为空,报错
//3.所有输入完成时栈不为空,报错
typedef struct Lnode* List;
struct Lnode
{
char S[100];
int top;
};
List InitList()
{
List L;
L = (List)malloc(sizeof(struct Lnode));
L->top = -1;
return L;
}
int match(char s[], int n)
{
int flag = 1;
int i = 0;
List l = InitList();
while (i < n && flag)
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
l->S[++l->top] = s[i];
else
{
switch (s[i])
{
case')':
if (l->top == -1 || l->S[l->top--] != '(')
flag = 0;
break;
case']':
if (l->top == -1 || l->S[l->top--] != '[')
flag = 0;
break;
case'}':
if (l->top == -1 || l->S[l->top--] != '{')
flag = 0;
break;
}
}
if (l->top != -1 && i == n - 1)
flag = 0;
i++;
}
return flag;
}
int main()
{
int n;
scanf_s("%d", &n);
//getchar();
char* a = new char[n];
for (int i = 0; i < n; i++)
{
scanf_s("%c", a);
}
int f = match(a, n);
printf("%d", f);
}
21
逆波兰式:https://www.cnblogs.com/lulipro/p/7450886.html
https://blog.csdn.net/alex1997222/article/details/82764166
https://blog.csdn.net/weixin_43821874/article/details/86485920
测试不对!!!!
//
//首先定义两个栈, 一个临时存储运算符s1, 另一个作为输入逆波兰式s的存放栈
//
//然后开始从表达式中取值
//
//(1)若取出的是表达式, 直接将其放置到s存放栈中
//
//(2)若取出的运算符, 则将该元素与s1栈顶元素进行比较, 如果该运算符优先级大于(小于或者等于都不行)s1栈顶运算符优先级, 则将该运算符进s1栈, 否则, 将s1栈栈的栈顶运算符弹出, 送入S2栈, 之后再和S1现在的栈顶比较, 直到运算符优先级大于栈顶为止后,将该运算符送入S1中
//
//(3)重复上述1 - 2步, 直到所有输入字符都处理完毕
//【如果取的是(,直接push到s1中,等待下一个最近的 右括号 与之配对
// 如果是),则说明有一对括号已经配对(在表达式输入无误的情况下)。不将它压栈,丢弃它,然后从s1中出栈,依次push到s里。一直循环,直到出栈元素 ( ,同样丢弃他】
//
//(4)将s1中的所有符号都push到s中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_LEN 30
#define MAX 100
//栈的数组实现
typedef struct
{
char data[MAX_LEN];
int top;
}Stack;
//为栈分配空间
Stack* Createstack()
{
Stack* p;
p = (Stack*)malloc(sizeof(*p));
p->top = -1;
return p;
}
//压栈
int Push(Stack* p, char x)
{
if (p->top == MAX_LEN - 1)
{
return -1;
}
p->top++;
p->data[p->top] = x;
return 0;
}
//出栈
int Pop(Stack* L, char* x)
{
if (L->top == -1)
{
return -1;
}
//利用传出参数传出栈顶元素
*x = L->data[L->top];
L->top--;
return 0;
}
//栈顶
int Top(Stack* L, char* x)
{
if (L->top == -1)
{
return -1;
}
*x = L->data[L->top];
return 0;
}
//判断栈是否为空
int Empty(Stack* L)
{
return (L->top == -1);
}
//定义符号的优先级
int Priority(char ope)
{
switch (ope)
{
case '(': return 0; //左括号已经在栈内时,如果比较,其优先级最低
case '+':
case '-': return 1;
case '*':
case '/': return 2;
default: return -1;
}
}
int IsOperator(char c)
{
const char* p = "+-*/";
while (*p)
{
if (*p == c)
return 1;
p++;
}
return 0;
}
void InversePolandExpression(char Buffer[])
{
Stack *s = Createstack();
Stack* s1 = Createstack();
int i = 0, j = 0;
char c;
i++;
while (Buffer[i] != '#')
{
if (IsOperator(Buffer[i]))//看字符是否是运算符
{
while (s1->top != -1 && IsOperator(s1->data[s1->top])&&
Priority(s1->data[s1->top])>= Priority(Buffer[i])
)
{
Push(s, Pop(s1,&c));
}
Push(s1, Buffer[i]);
}
else
{
if (Buffer[i] == '(')
{
Push(s1, Buffer[i]);
}
else if (Push(s1, Buffer[i]) == ')')
{
while (s1->top != -1 && s1->data[s1->top] != '(')
{
Push(s, Pop(s1, &c));
}
if (s1->top != -1)
Pop(s1, &c);
}
else//如果是数字的话
{
Push(s, Buffer[i]);
}
}
}
while (s1->top != -1)
{
Push(s, Pop(s1, &c));
}
for (; j <= s->top; j++)
{
printf("%c", s->data[j]);
}
}
int main()
{
char c[6] = "2+3-6";
InversePolandExpression(c);
return 0;
}
31
//(2)用堆栈实现
int symmetry(char a[])//用堆栈实现倒着的字符对比
{
int i = 0;
Stack S;
InitStack(S);
char x;
while (a[i] != '@') )//查看字符个数
{
Push(S, a[i]);
i++;
}
i = 0;
while (a[i])
{
Pop(S, x);
if (x != a[i])
{
DestroyStack(S);
return 0;
}
i++;
}
return 1;
}
32
计算斐波那契数列:https://blog.csdn.net/sun_jinhang/article/details/88395958
求解:https://blog.csdn.net/l954688947/article/details/49103553
https://blog.csdn.net/kangjianwei101/article/details/50510718?utm_source=blogxgwz2
注意k阶菲波那契序列,一开始有k-1个0,第k个为1,然后第k+1个数为前k个数之和,如此类推~如:
2阶斐波那契数列:0,1,1,2,3,5,8……
3阶斐波那契数列:0,0,1,2,……
//错误的,没有理解题目
#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
typedef struct Qunue* qunue;
struct Qunue
{
int base[maxsize];
int head, rear;
};
qunue InitQunue()
{
qunue q;
q = (qunue)malloc(sizeof(struct Qunue));
q->head = q->rear = 0;
return q;
}
int Fib(int k, int n)//求斐波那契数列
{
qunue q;
q = InitQunue();
int i = 0;
int a = 0, b = 1, sum = 0;
while (i < n)
{
if (i <= k - 1)
q->base[i] = 0;
else if (i == k )
q->base[i] = 1;
else
{
for (i = k + 1; i <= n; i++)
{
sum = a + b;
q->base[i] = sum;
a = b;
b = sum;
}
}
}
return sum;
}
int main()
{
int x, k, n;
printf("输入max:");
scanf_s("%d", &x);
printf("输入费伯纳西数列阶数:");
scanf_s("%d", &k);
for (n = 1;; n++)
{
if (Fib(k, n) <= x && Fib(k, n + 1) > x)break;
}
printf("n值为:%d\n", n);
return 0;
}