#include<stdio.h>#include<stdbool.h>#include<string.h>#include<ctype.h>char calc[1005]="";char stack[1005]={};int top =-1;boolisPrior(char curr,char topCh){return(((curr !='+')&&(curr !='-'))||((topCh !='*')&&(topCh !='/')))&&(curr !=')');}intmain(){scanf("%s", calc);
size_t len =strlen(calc);for(int i =0; i < len;++i){if(isalpha(calc[i]))printf("%c", calc[i]);else{if(top ==-1||isPrior(calc[i], stack[top]))
stack[++top]= calc[i];else{if(calc[i]==')'){while(stack[top]!='(')printf("%c", stack[top--]);--top;}elsewhile(top !=-1)printf("%c", stack[top--]);}}}while(top !=-1)printf("%c", stack[top--]);return0;}
9 循环队列
#include<stdio.h>int queue[1005]={};int rear =0, len;intmain(){scanf("%d",&len);while(1){scanf("%d",&queue[rear++]);char ch =getchar();if(ch =='\n'|| rear >= len)break;}int val, front =0;scanf("%d",&val);while(front < len && queue[front]!= val)++front;++front;for(int i = front; i < rear;++i)printf("%d ", queue[i]);printf("\n");printf("%d\n", queue[front]);return0;}
10
k
k
k 阶斐波那契数列
#include<stdio.h>intmain(){int m, k;scanf("%d %d",&m ,&k);int bp[k]={}, rear =0, sum =0;
bp[k -1]=1;while(1){
sum =0;// 循环数组for(int i =0; i < k;++i)
sum += bp[(i + rear)% k];if(bp[rear]<= m && sum > m)break;
bp[rear]= sum;
rear =(rear +1)% k;}for(int i =0; i < k;++i)printf("%d ", bp[(rear + i)% k]);printf("\n");return0;}
第五章 数组与广义表
11 循环右移
#include<stdio.h>int arr[105]={};voidreverse(int left,int right){while(left < right){int tmp = arr[left];
arr[left]= arr[right], arr[right]= tmp;++left,--right;}}intmain(){int n, k;scanf("%d %d",&n,&k);for(int i =0; i < n;++i)scanf("%d",&arr[i]);// 循环移动 = 三次翻转reverse(0, n -1);reverse(0, k -1);reverse(k, n -1);for(int i =0; i < n;++i)printf("%d ", arr[i]);printf("\n");return0;}
12 以三元组表为存储结构实现矩阵相加
#include<stdio.h>intmain(){int n, m, t1, t2;scanf("%d %d %d %d",&n,&m,&t1,&t2);int S1[t1][3]={}, S2[t2][3]={}, S[t1 + t2][3]={};for(int i =0; i < t1;++i)scanf("%d %d %d",&S1[i][0],&S1[i][1],&S1[i][2]);for(int i =0; i < t2;++i)scanf("%d %d %d",&S2[i][0],&S2[i][1],&S2[i][2]);// 三指针法int h1 =0, h2 =0, h =0;while(h1 < t1 && h2 <t2){if(S1[h1][0]< S2[h2][0]){
S[h][0]= S1[h1][0], S[h][1]= S1[h1][1], S[h][2]= S1[h1][2];++h1,++h;}elseif(S1[h1][0]> S2[h2][0]){
S[h][0]= S2[h2][0], S[h][1]= S2[h2][1], S[h][2]= S2[h2][2];++h2,++h;}else{if(S1[h1][1]< S2[h2][1]){
S[h][0]= S1[h1][0], S[h][1]= S1[h1][1], S[h][2]= S1[h1][2];++h1,++h;}elseif(S1[h1][1]> S2[h2][1]){
S[h][0]= S2[h2][0], S[h][1]= S2[h2][1], S[h][2]= S2[h2][2];++h2,++h;}else{
S[h][0]= S1[h1][0], S[h][1]= S1[h1][1], S[h][2]= S1[h1][2]+ S2[h2][2];++h1,++h2,++h;}}}for(int i =0; i < t1 + t2 && S[i][0];++i)printf("%d %d %d\n", S[i][0], S[i][1], S[i][2]);return0;}
13 以十字链表为存储结构实现矩阵相加
#include<stdio.h>#include<stdlib.h>typedefstructNode{int raw, col, val;structNode*down,*right;} Node, CList;
CList *createCList(int raw,int col){
CList *c =(CList*)malloc(sizeof(CList));
c->raw = raw, c->col = col, c->val =-1;
c->down = c->right = c;for(int i = raw; i >0;--i){
Node *tmp =(Node*)malloc(sizeof(Node));
tmp->val =-1, tmp->raw = i, tmp->col =0;
tmp->right = tmp, tmp->down = c->down, c->down = tmp;}for(int i = col; i >0;--i){
Node *tmp =(Node*)malloc(sizeof(Node));
tmp->val =-1, tmp->raw =0, tmp->col = i;
tmp->down = tmp, tmp->right = c->right, c->right = tmp;}return c;}voidinsertOrAdd(CList *head,int raw,int col,int val){
Node *node =(Node*)malloc(sizeof(Node)),*curr = head;for(int i =1; i <= raw;++i) curr = curr->down;while(curr->right->col < col && curr->right->col !=0) curr = curr->right;// 相同位置直接相加if(curr->right->col == col){
curr->right->val += val;free(node);return;}
node->right = curr->right, curr->right = node;
curr = head;for(int i =1; i <= col;++i) curr = curr->right;while(curr->down->raw < raw && curr->down->raw !=0) curr = curr->down;
node->down = curr->down, curr->down = node;
node->raw = raw, node->col = col, node->val = val;}voidtraverse(CList *S){for(Node *r = S->down; r != S; r = r->down){for(Node *c = r->right; c != r; c = c->right){printf("%d %d %d\n", c->raw, c->col, c->val);}}}intmain(){int n, m, t1, t2, r, c, v;scanf("%d %d %d %d",&n,&m,&t1,&t2);
CList *S1 =createCList(n, m);for(int i = t1; i >0;--i){scanf("%d %d %d",&r,&c,&v);insertOrAdd(S1, r, c, v);}for(int i = t2; i >0;--i){scanf("%d %d %d",&r,&c,&v);insertOrAdd(S1, r, c, v);}traverse(S1);return0;}