7-16 队列的实现及基本操作
![](https://img-blog.csdnimg.cn/e153fad2bab44dffa147ca6289e7f942.png)
代码:
#include <stdio.h>
int queue[20000];
int front;
int rear;
void initqueue();//初始化队列
void enterqueue(int x);//入队
int deletequeue();//出队
int main()
{
int n,t;
int i,k=0;
int a[20000][2];
char ch;
scanf("%d",&n);
for(i=0;i<n;i++)
{
k=0;
do
{
scanf("%d",&a[i][k]);
k++;
}while((ch=getchar())!='\n');
}
for(i=0;i<n;i++)
{
if(a[i][0]==1)//入队
{
enterqueue(a[i][1]);
}
if(a[i][0]==0)
{
t=deletequeue();
if(t==0)
printf("invalid\n");
else
printf("%d\n",queue[front-1]);
}
}
return 0;
}
void initqueue()//初始化队列
{
front=0;
rear=0;
}
void enterqueue(int x)//入队
{
queue[rear]=x;
rear++;
}
int deletequeue()//出队
{
if(front==rear)
return 0;//队列为空
else
{
front++;
return 1;
}
}
7-17 字符串模式匹配
![](https://img-blog.csdnimg.cn/50664188c0c546378dbba6300cedd6d4.png)
代码:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int* getNext(char* p, int pn)
{
if (pn == 1) {
int* next = (int*)malloc(sizeof(int));
next[0] = -1;
return next;
}
int* next = (int*)malloc(sizeof(int) * pn);
next[0] = -1;
next[1] = 0;
int pos = 2;
int cn = 0;
while (pos < pn) {
if (p[pos - 1] == p[cn]) {
next[pos++] = ++cn;
}
else if (cn > 0) {
cn = next[cn];
}
else
{
next[pos++] = 0;
}
}
return next;
}
int kmp(char* t, int tn, char* p, int pn)
{
if (t == NULL || p == NULL || pn < 1 || tn < pn) {
return -1;
}
int i = 0;
int j = 0;
int* next = getNext(p, pn);
int m = tn;
int n = pn;
while ((i < m) && (j < n)) {
if (t[i] == p[j]) {
++i;
++j;
}
else if (next[j] == -1) i++;
else j = next[j];
}
return j == pn ? i - j : -1;
}
void fail(char* a,int an) {
int* b = (int*)malloc(sizeof(int) * an);
b[0] = -1;
for (int i = 1; i < an; ++i) {
int j = b[i - 1];
while (a[i] != a[j + 1] && (j >= 0)) j = b[j];
if (a[i] == a[j + 1]) b[i] = j + 1;
else b[i] = -1;
}
for (int i = 0; i < an; ++i) {
printf("%d ", b[i]);
}
printf("\n");
}
int main()
{
// char str1[] = "qwerabcabhlk";
// char str2[] = "abcab";
char str1[100000], str2[100000];
//str1 = "qwerabcabhlk";
//str2 = "abcab";
scanf("%s", str1);
scanf("%s", str2);
int length1 = strlen(str1);
int length2 = strlen(str2);
fail(str2, length2);
int res = kmp(str1, length1, str2, length2);
printf("%d", res);
return 0;
}
7-18 大菲波数
![](https://img-blog.csdnimg.cn/b44870c148344792bebdc70ce29c6c01.png)
代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
int a[1000 +5][400];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int pi,i,j;
scanf("%d",&pi);
memset(a,0,sizeof(a));
a[0][0]=a[1][0]=1;
int k=1;
if(pi>1){
for(i = 2;i<pi;i++){
for(j = 0;j <k ;j++){
a[i][j]=a[i-1][j]+a[i-2][j];
}
for(j=0;j<k;j++){
if(a[i][k-1]>9)
k++;
if(a[i][j]>9){
a[i][j+1]+=a[i][j]/10;
a[i][j]=a[i][j]%10;
}
}
}for(i = k-1;i >=0;i--){
printf("%d",a[pi-1][i]);
}
}
else
printf("%d",a[pi-1][0]);
printf("\n");
}
return 0;
}
7-19 "聪明的学生"
![](https://img-blog.csdnimg.cn/3f09826368814550983a8d20af3cacc8.png)
代码:
#include <stdio.h>
#include <stdlib.h>
int step(int t1,int t2)
{
if(t2>t1)
return t2-t1;
else
return t2+3-t1;
}
int times(int i,int j,int t1,int t2,int t3)
{
int k;
k=i-j;
if(k==0)
{
return t3;
}
if(k>0)
{
return times(j,i-j,t2,t3,t1)+step(t1,t3);
}
if(k<0)
{
return times(i,j-i,t1,t3,t2)+step(t2,t3);
}
}
int main()
{
int result;
int a=1,b=2,c=3;
int arr[3];
scanf("%d,%d,%d",&arr[0],&arr[1],&arr[2]);
int index=0;
int max_num=-1,mid_num=-1;
int max_index=-1,mid_index=-1;
for(;index<3;index++)
{
if(max_num<arr[index])
{
mid_num=max_num;
mid_index=max_index;
max_num=arr[index];
max_index=index;
}
if(mid_num<arr[index]&&arr[index]!=max_num)
{
mid_num=arr[index];
mid_index=index;
}
}
c=max_index+1;
b=mid_index+1;
if((c==1&&b==2)||(c==2&&b==2))
a=3;
else
if((c==2&&b==3)||(c==3&&b==2))
a=1;
else
a=2;
result=times(arr[a-1],arr[b-1],a,b,c);
printf("%d\n",result);
return 0;
}
7-20 who is the last?
![](https://img-blog.csdnimg.cn/e82964f63f2f4b5d86493e91f683c88b.png)
代码:
#include <stdio.h>
#include <stdlib.h>
struct Node;
typedef struct Node *ptrtoNode;
typedef ptrtoNode position;
typedef position Head;
struct Node
{
int x;
position next;
};
int main()
{
Head head = malloc(sizeof(struct Node));
head->next = NULL;
head->x = 1;
position pLast = head;
int N, M;
scanf("%d", &N);
getchar();
scanf("%d", &M);
int i;
if (N == 0)
{
for (i = 1; i < N; i++)
{
printf("%d,", i);
}
printf("%d", i);
}
else
{
for (i = 2; i <= N; i++)
{
position pnew = malloc(sizeof(struct Node));
pnew->x = i;
pLast->next = pnew;
pnew->next = head;
pLast = pnew;
}
int nowPeople = N;
position p = head;
while (nowPeople > 1)
{
for (i = 1; i <= M - 1; i++)
{
p = p->next;
}
// deleteNode(p);
printf("%d,", p->next->x);
p->next = p->next->next;
p = p->next;
nowPeople--;
}
printf("%d\n", p->x);
}
return 0;
}