第1题:逆波兰表达式
逆波兰表达式是一种把运算符前置的算术表达式,例如普通的表达式2 + 3的逆波兰表示法为+ 2 3。逆波兰表达式的优点是运算符之间不必有优先级关系,也不必用括号改变运算次序,例如(2 + 3) * 4的逆波兰表示法为* + 2 3 4。本题求解逆波兰表达式的值,其中运算符包括+ - * /四个。
时间限制:1000
内存限制:65536
输入
输入为一行,其中运算符和运算数之间都用空格分隔,运算数是浮点数。
输出
输出为一行,表达式的值。 可直接用printf(“%f\n”, v)输出表达式的值v。
样例输入
* + 11.0 12.0 + 24.0 35.0
样例输出
1357.000000
下面是一个使用栈来计算逆波兰表达式值的示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>typedef struct {int top;double* array;
} Stack;void initializeStack(Stack* stack, int capacity) {stack->top = -1;stack->array = (double*)malloc(capacity * sizeof(double));
}int isEmpty(Stack* stack) {return stack->top == -1;
}int isFull(Stack* stack, int capacity) {return stack->top == capacity - 1;
}void push(Stack* stack, double element) {stack->array[++stack->top] = element;
}double pop(Stack* stack) {return stack->array[stack->top--];
}double peek(Stack* stack) {return stack->array[stack->top];
}double performOperation(double operand1, double operand2, char operator) {switch (operator) {case '+':return operand1 + operand2;case '-':return operand1 - operand2;case '*':return operand1 * operand2;case '/':return operand1 / operand2;default:return 0.0;}
}double evaluateExpression(char* expression) {int length = strlen(expression);Stack stack;initializeStack(&stack, length);char* token = strtok(expression, " ");while (token != NULL) {if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {push(&stack, atof(token));} else {double operand2 = pop(&stack);double operand1 = pop(&stack);double result = performOperation(operand1, operand2, token[0]);push(&stack, result);}token = strtok(NULL, " ");}return pop(&stack);
}int main() {char expression[1000];fgets(expression, sizeof(expression), stdin);double result = evaluateExpression(expression);printf("%f\n", result);return 0;
}
在这段代码中,我们定义了Stack
结构体,用于表示栈。栈包含一个指向栈顶的指针以及一个双精度浮点型数组用于存储栈中的元素。
initializeStack
函数用于初始化栈。
isEmpty
函数用于检查栈是否为空。
isFull
函数用于检查栈是否已满。
push
函数用于将元素压入栈中。
pop
函数用于从栈中弹出元素。
peek
函数用于查看栈顶元素但不弹出。
performOperation
函数用于执行两个操作数之间的运算。
evaluateExpression
函数用于计算逆波兰表达式的值。函数遍历给定的逆波兰表达式,根据操作数和运算符的顺序,使用一个栈来计算表达式的值。
在main
函数中,我们首先读取输入的逆波兰表达式,并调用evaluateExpression
函数计算表达式的值。最后,我们使用printf
函数将结果输出。
这样,我们就可以使用堆栈的方法计算逆波兰表达式的值。
第2题:多项式相加
我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起。首先,我们会有两个多项式,每个多项式是独立的一行,每个多项式由系数、幂数这样的多个整数对来表示。
如多项式2x20- x17+ 5x9- 7x7+ 16x5+ 10x4 + 22x2- 15
对应的表达式为:2 20 -1 17 5 9 - 7 7 16 5 10 4 22 2 -15 0。
为了标记每行多项式的结束,在表达式后面加上了一个幂数为负数的整数对。
同时输入表达式的幂数大小顺序是随机的。
我们需要做的就是把所给的两个多项式加起来。
时间限制:1000
内存限制:65536
输入
输入包括多行。 第一行整数n,表示有多少组的多项式需要求和。(1 < n < 100) 下面为2n行整数,每一行都是一个多项式的表达式。表示n组需要相加的多项式。 每行长度小于300。
输出
输出包括n行,每行为1组多项式相加的结果。 在每一行的输出结果中,多项式的每一项用“[x y]”形式的字符串表示,x是该项的系数、y 是该项的幂数。要求按照每一项的幂从高到低排列,即先输出幂数高的项、再输出幂数低的项。 系数为零的项不要输出。
样例输入
2
-1 17 2 20 5 9 -7 7 10 4 22 2 -15 0 16 5 0 -1
2 19 7 7 3 17 4 4 15 10 -10 5 13 2 -7 0 8 -8
-1 17 2 23 22 2 6 8 -4 7 -18 0 1 5 21 4 0 -1
12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2 19 9 -7
样例输出
[ 2 20 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 5 9 ] [ 6 5 ] [ 14 4 ] [ 35 2 ] [ -22 0 ]
[ 2 23 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 6 8 ] [ 8 7 ] [ -3 5 ] [ 44 4 ] [ 22 2 ] [ -18 0 ]
提示
第一组样例数据的第二行末尾的8 -8,因为幂次-8为负数,所以这一行数据结束,8 -8不要参与计算。
下面是一个使用链表来实现多项式相加的示例代码:
#include <stdio.h>
#include <stdlib.h>typedef struct Node {int coefficient;int exponent;struct Node* next;
} Node;Node* createNode(int coefficient, int exponent) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->coefficient = coefficient;newNode->exponent = exponent;newNode->next = NULL;return newNode;
}void insertNode(Node** head, int coefficient, int exponent) {Node* newNode = createNode(coefficient, exponent);if (*head == NULL) {*head = newNode;} else {Node* current = *head;while (current->next != NULL) {current = current->next;}current->next = newNode;}
}void addPolynomials(Node* poly1, Node* poly2) {Node* result = NULL;while (poly1 != NULL && poly2 != NULL) {if (poly1->exponent > poly2->exponent) {insertNode(&result, poly1->coefficient, poly1->exponent);poly1 = poly1->next;} else if (poly1->exponent < poly2->exponent) {insertNode(&result, poly2->coefficient, poly2->exponent);poly2 = poly2->next;} else {int sum = poly1->coefficient + poly2->coefficient;if (sum != 0) {insertNode(&result, sum, poly1->exponent);}poly1 = poly1->next;poly2 = poly2->next;}}while (poly1 != NULL) {insertNode(&result, poly1->coefficient, poly1->exponent);poly1 = poly1->next;}while (poly2 != NULL) {insertNode(&result, poly2->coefficient, poly2->exponent);poly2 = poly2->next;}// Print the resultNode* current = result;while (current != NULL) {printf("[ %d %d ] ", current->coefficient, current->exponent);current = current->next;}printf("\n");// Free the memorywhile (result != NULL) {Node* temp = result;result = result->next;free(temp);}
}int main() {int n;scanf("%d", &n);getchar(); // Read the newline character after nfor (int i = 0; i < n; i++) {int m;scanf("%d", &m);getchar(); // Read the newline character after mNode* poly1 = NULL;Node* poly2 = NULL;for (int j = 0; j < m; j++) {int coefficient, exponent;scanf("%d %d", &coefficient, &exponent);insertNode(&poly1, coefficient, exponent);}getchar(); // Read the newline character after the inputfor (int j = 0; j < m; j++) {int coefficient, exponent;scanf("%d %d", &coefficient, &exponent);insertNode(&poly2, coefficient, exponent);}getchar(); // Read the newline character after the inputaddPolynomials(poly1, poly2);// Free the memorywhile (poly1 != NULL) {Node* temp = poly1;poly1 = poly1->next;free(temp);}while (poly2 != NULL) {Node* temp = poly2;poly2 = poly2->next;free(temp);}}return 0;
}
该程序使用链表来表示多项式。它定义了一个Node
结构来表示每个项,每个节点包含系数和指数,并且有一个指向下一个节点的指针。createNode
函数用于创建新的节点,insertNode
函数用于在链表末尾插入节点。
addPolynomials
函数接收两个多项式的链表作为输入,并将它们相加得到一个新的链表。它通过比较指数的大小来决定插入哪个节点或将系数相加。最后,它遍历结果链表并打印每个项。
在main
函数中,首先读取输入中多项式的数量n
。接着,它使用一个循环来处理每组多项式的输入。对于每组多项式,它首先读取第一个多项式的项数m
,然后按顺序读取每个项的系数和指数,并将它们插入第一个多项式的链表中。之后,它读取第二个多项式的项,并将它们插入第二个多项式的链表中。
接下来,它调用addPolynomials
函数,并将第一个和第二个多项式的链表作为参数传递给它,以计算它们的和并打印结果。
最后,它释放每个多项式链表的内存,并继续处理下一组多项式的输入。
请注意,在输入多项式的过程中,我们使用getchar
函数来读取每行的换行符,以便在读取完整一行后移动到下一行的输入。
这是一个基本的多项式相加的实现示例。您可以根据需要进行修改和扩展。
第3题:扑克牌排序
假设这里有36张扑克牌,分别为A1
A9,B1B9,C1C9,D1D9,其中A代表方片,B代表草花,C代表红桃,D代表黑桃,那么,设定如下的排序规则:
1.对于两张卡牌,X1Y1与X2Y2,X1与X2表示A~D,Y1与Y2表示1~9,如果X1与X2不同,那么依照D>C>B>A的方式进行排序
2.假如有X1与X2相同时,那么就比较Y1与Y2的大小。
例如,对于如下的四张牌,有如下的升序排序结果:
D3,C4,A4,C1
升序排序的结果为A4,C1,C4,D3
有人提出了如下的排序策略:
先建立9个队列,用于存放点数的大小,将卡牌依点数存放入各自的队列之中,然后再按队列1到队列9依次出队。
例如,对于上面的结果,依次进队后,结果如下:
队列1:C1;队列3:D3,队列4:C4,A4
将其依次出队后,结果为C1,D3,C4,A4
然后,再建立4个队列,用于存放花色。将卡牌依花色A~D存放入队列1~4中,然后再按队列1到队列4依次出队。
例如,对于上面刚刚出队的序列C1,D3,C4,A4,将其依次进队,结果如下:
队列1:A4;队列3:C1,C4;队列4:D3
将其依次出队后,结果为A4,C1,C4,D3,排序结束。
请根据上面的算法,编写一个用队列对扑克牌排序的程序,要求依照上面的排序规则,根据先花色后点数的方法进行排序。
时间限制:1000
内存限制:65536
输入
输入分为两行,第一行为一个整数n,表示一共有n张牌(1<=n<=100) 第二行用XY的形式表示每一张牌,其中X为A~D,Y为1~9
输出
输出三个部分 第一个部分为第一次进队出队的结果,用Queue1:…表示,共9行,结果用空格分隔,下同 第二部分为第二次进队出队的结果,用QueueA:…表示,共4行 第三部分为一行,即将卡牌排序后的结果(升序排序)
样例输入
8
D8 A6 C3 B8 C5 A1 B5 D3
样例输出
Queue1:A1
Queue2:
Queue3:C3 D3
Queue4:
Queue5:C5 B5
Queue6:A6
Queue7:
Queue8:D8 B8
Queue9:
QueueA:A1 A6
QueueB:B5 B8
QueueC:C3 C5
QueueD:D3 D8
A1 A6 B5 B8 C3 C5 D3 D8
提示
第二次入队出队时,可以复用第一次时9个队列中的4个。所以其实只需要开辟9个队列即可。
以下是使用C语言编写的解决方案:
#include <stdio.h>
#include <stdlib.h>#define MAX_SIZE 100// 定义扑克牌结构体
struct Card {char suit; // 花色int rank; // 点数
};// 定义队列结构体
struct Queue {struct Card cards[MAX_SIZE];int front;int rear;
};// 初始化队列
void initQueue(struct Queue* queue) {queue->front = -1;queue->rear = -1;
}// 判断队列是否为空
int isEmpty(struct Queue* queue) {return queue->front == -1;
}// 判断队列是否已满
int isFull(struct Queue* queue) {return (queue->rear + 1) % MAX_SIZE == queue->front;
}// 入队
void enqueue(struct Queue* queue, struct Card card) {if (isFull(queue)) {printf("Queue is full\n");return;}if (isEmpty(queue)) {queue->front = 0;}queue->rear = (queue->rear + 1) % MAX_SIZE;queue->cards[queue->rear] = card;
}// 出队
struct Card dequeue(struct Queue* queue) {if (isEmpty(queue)) {printf("Queue is empty\n");exit(1);}struct Card card = queue->cards[queue->front];if (queue->front == queue->rear) {queue->front = -1;queue->rear = -1;} else {queue->front = (queue->front + 1) % MAX_SIZE;}return card;
}// 获取队列中元素个数
int getSize(struct Queue* queue) {if (isEmpty(queue)) {return 0;}return (queue->rear - queue->front + MAX_SIZE) % MAX_SIZE + 1;
}// 比较两张牌的大小
int compareCards(struct Card card1, struct Card card2) {if (card1.suit != card2.suit) {// 花色不同,按照 D > C > B > A 的顺序排序if (card1.suit == 'D') {return 1;} else if (card1.suit == 'C' && card2.suit != 'D') {return 1;} else if (card1.suit == 'B' && card2.suit != 'D' && card2.suit != 'C') {return 1;} else if (card1.suit == 'A' && card2.suit == 'D') {return 1;} else {return -1;}} else {// 花色相同,按照点数排序if (card1.rank < card2.rank) {return -1;} else if (card1.rank > card2.rank) {return 1;} else {return 0;}}
}// 使用冒泡排序对牌进行排序
void sortCards(struct Card* cards, int n) {for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - i - 1; j++) {if (compareCards(cards[j], cards[j + 1]) > 0) {struct Card temp = cards[j];cards[j] = cards[j + 1];cards[j + 1] = temp;}}}
}int main() {int n;scanf("%d", &n);struct Queue queues[9];for (int i = 0; i < 9; i++) {initQueue(&queues[i]);}for (int i = 0; i < n; i++) {char suit;int rank;scanf(" %c%d", &suit, &rank);struct Card card;card.suit = suit;card.rank = rank;enqueue(&queues[rank - 1], card);}// 第一次进队出队for (int i = 0; i < 9; i++) {printf("Queue%d:", i + 1);while (!isEmpty(&queues[i])) {struct Card card = dequeue(&queues[i]);printf(" %c%d", card.suit, card.rank);}printf("\n");}// 复用队列,继续:for (int i = 0; i < 4; i++) {initQueue(&queues[i]);}for (int i = 0; i < 9; i++) {while (!isEmpty(&queues[i])) {struct Card card = dequeue(&queues[i]);enqueue(&queues[card.suit - 'A'], card);}}// 第二次进队出队for (int i = 0; i < 4; i++) {printf("Queue%c:", 'A' + i);while (!isEmpty(&queues[i])) {struct Card card = dequeue(&queues[i]);printf(" %c%d", card.suit, card.rank);}printf("\n");}// 排序并输出结果struct Card sortedCards[MAX_SIZE];int index = 0;for (int i = 0; i < 4; i++) {while (!isEmpty(&queues[i])) {struct Card card = dequeue(&queues[i]);sortedCards[index++] = card;}}sortCards(sortedCards, n);printf("\n");for (int i = 0; i < n; i++) {printf("%c%d ", sortedCards[i].suit, sortedCards[i].rank);}printf("\n");return 0;
}
这个程序使用了队列实现了扑克牌的排序。它首先按照点数将牌放入9个队列中,然后依次出队,结果打印出来。接下来,它复用了这9个队列中的4个队列,将牌按照花色放入队列中,再次依次出队,结果打印出来。最后,它使用冒泡排序对牌进行排序,并打印排序后的结果。
请注意,这个程序假设输入的牌是合法的,并且没有进行输入验证。在实际应用中,你可能需要添加输入验证的代码来确保输入的正确性。
希望这可以帮助到你!如果你有任何问题,请随时提问。
第4题:表达式求值
求一个可能包含加、减、乘、除、乘方运算的中缀表达式的值。
在计算机中,我们常用栈来解决这一问题。首先将中缀表达式转换到后缀表达式,然后对后缀表达式求值。
加、减、乘、除、乘方分别用+,-,*, /, ^来表示。表达式可以有圆括号()。
时间限制:1000
内存限制:65536
输入
第一行为测试数据的组数N。 接下来的N行,每行是一个中缀表达式。 每个表达式中,圆括号、运算符和运算数相互之间都用空格分隔,运算数是整数。一般运算数可正可负(负数的符号和数字之间无空格),指数一定为自然数(0和正整数)。不必考虑除0的情况。每个运算数均可由int放下。不必考虑溢出。中缀表达式的字符串长度不超过600。乘方的优先级比乘除都高,结合性是向左结合,如2 ^ 3 ^ 4表示( 2 ^ 3 ) ^ 4 = 4096。除法的商向下取整。
输出
对每一组测试数据输出一行,为表达式的值
样例输入
2
31 * ( 5 - ( -3 + 25 ) ) + 70 ^ 2
2 * 5 + 6 * ( 7 - 8 ) + 6
样例输出
4373
10
以下是使用C语言编写的解法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>// 定义栈结构
typedef struct {int top;int capacity;int* array;
} Stack;// 初始化栈
Stack* createStack(int capacity) {Stack* stack = (Stack*)malloc(sizeof(Stack));stack->top = -1;stack->capacity = capacity;stack->array = (int*)malloc(stack->capacity * sizeof(int));return stack;
}// 检查栈是否为空
int isEmpty(Stack* stack) {return stack->top == -1;
}// 检查栈是否已满
int isFull(Stack* stack) {return stack->top == stack->capacity - 1;
}// 入栈
void push(Stack* stack, int item) {if (isFull(stack)) {printf("Stack Overflow\n");return;}stack->array[++stack->top] = item;
}// 出栈
int pop(Stack* stack) {if (isEmpty(stack)) {printf("Stack Underflow\n");return -1;}return stack->array[stack->top--];
}// 获取栈顶元素
int peek(Stack* stack) {if (isEmpty(stack)) {printf("Stack is empty\n");return -1;}return stack->array[stack->top];
}// 判断运算符的优先级
int getPriority(char operator) {if (operator == '^')return 3;else if (operator == '*' || operator == '/')return 2;else if (operator == '+' || operator == '-')return 1;elsereturn 0;
}// 将中缀表达式转换为后缀表达式
void infixToPostfix(char* infix, char* postfix) {int i, j;Stack* stack = createStack(strlen(infix));push(stack, '(');strcat(infix, ")");i = 0;j = 0;while (!isEmpty(stack)) {if (isdigit(infix[i]) || infix[i] == '-' && isdigit(infix[i + 1])) {while (isdigit(infix[i]) || infix[i] == '-')postfix[j++] = infix[i++];postfix[j++] = ' ';}else if (infix[i] == '(') {push(stack, '(');i++;}else if (infix[i] == ')') {while (peek(stack) != '(')postfix[j++] = pop(stack);pop(stack);i++;}else if (infix[i] == ' ') {i++;}else {while (getPriority(infix[i]) <= getPriority(peek(stack)))postfix[j++] = pop(stack);push(stack, infix[i]);i++;}}postfix[j] = '\0';
}// 执行后缀表达式的计算
int evaluatePostfix(char* postfix) {int i, operand1, operand2, result, len;Stack* stack = createStack(strlen(postfix));len = strlen(postfix);for (i = 0; i < len; i++) {if (isdigit(postfix[i])) {int num = 0;while (isdigit(postfix[i])) {num = num * 10 + (postfix[i] - '0');i++;}push(stack, num);}else if (postfix[i] == ' ') {continue;}else {operand2 = pop(stack);operand1 = pop(stack);switch (postfix[i]) {case '+':result = operand1 + operand2;break;case '-':result = operand1 - operand2;break;case '*':result = operand1 * operand2;break;case '/':result = operand1 / operand2;break;case '^':result = 1;for (int j = 0; j < operand2; j++)result *= operand1;break;default:break;}push(stack, result);}}return pop(stack);
}int main() {int N;scanf("%d", &N);getchar(); // 读取换行符while (N--) {char infix[601];char postfix[601];fgets(infix, sizeof(infix), stdin);infixToPostfix(infix, postfix);printf("%d\n", evaluatePostfix(postfix));}return 0;
}
这个解法使用了栈数据结构来处理中缀表达式的转换和后缀表达式的求值。首先,定义了一个栈结构,并实现了栈的基本操作。然后,定义了一个函数infixToPostfix
,它将给定的中缀表达式转换为后缀表达式。转换过程使用了栈来保存运算符,并根据运算符的优先级将它们按正确的顺序输出到后缀表达式字符串中。最后,定义了一个函数evaluatePostfix
,它对后缀表达式进行求值。求值过程中,使用栈来保存运算数,并根据遇到的运算符进行相应的计算。最后,将每组测试数据的后缀表达式求值结果打印出来。
请注意,这个解法假设输入的中缀表达式是合法的,并且不处理除以零的情况。还要注意的是,题目要求乘方运算的结合性是向左结合,即2 ^ 3 ^ 4等价于(2 ^ 3) ^ 4。这个解法按照这个要求进行了计算。
以上是一个满足题目要求的C语言解法,你可以将其保存为一个.c文件,使用C编译器进行编译和运行。