提交 f985e3df 编辑于 作者: unknown's avatar unknown
浏览文件

update 7-data-type

上级 75678869
Start testing: Nov 17 21:19 ?D1???
Start testing: Nov 22 19:44 ?D1???
----------------------------------------------------------
End testing: Nov 17 21:19 ?D1???
End testing: Nov 22 19:44 ?D1???
/cmake-build-debug
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
7_data_types
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/7-data-types.iml" filepath="$PROJECT_DIR$/.idea/7-data-types.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
cmake_minimum_required(VERSION 3.20)
project(7_data_types C)
set(CMAKE_C_STANDARD 99)
add_executable(stack stack.c)
add_executable(wine wine.c)
add_executable(brackets brackets.c)
add_executable(pour pour.c)
# README
## stack.c
### 题目描述
用数组模拟一个栈(不知道栈的定义的同学请自行 STFW,不知道 STFW 是什么意思的也请自行 STFW),支持的操作有
- push:向栈顶添加数据
- pop:弹出栈顶的数据
- top:查看栈顶的数据
- print:打印栈
### 输入格式
输入包含多行,第一行为一个正整数 n, 10\leqslant n\leqslant 10^4*n*,10⩽*n*⩽104,表示进行操作的次数。
接下来 n*n* 行,每行有四种可能
- push m:其中 m 为一个 char 类型的变量,表示向栈顶添加字符 m;
- pop:如果栈为空,则打印 Empty 并换行,否则弹出栈顶的字符(不用打印);
- top:如果栈为空,则打印 Empty 并换行,否则打印栈顶的字符(不用弹出);
- print:如果栈为空,则打印 Empty 并换行,否则按照如下格式自顶向下打印整个栈:
```
| c |
| c |
...
| c |
|===| 其中,c 为栈中字符(注意 c 的两边都有一个空格)
```
### 输出格式
对于每一次 pop / top / print 操作。输出相印内容。
### 什么是栈
### 有关<string.h>
https://www.programiz.com/c-programming/library-function/string.h/strcmp
\ No newline at end of file
//
// Created by Zyi on 2021/11/22.
//
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
char stack[100005];
char** brackets;
char pop();
void push(char x);
// 清空栈元素,恢复指针
void clear();
bool isLeftBracket(char bracket);
bool isRightBracket(char bracket);
bool isMatch(char leftBracket, char rightBracket);
int pointer = -1;
int main() {
// 这道题其实是栈的应用
// 遇到左括号我们就推入栈
// 遇到右括号就把当前栈顶元素弹出,看是否和该右括号匹配即可
int dataLength;
scanf("%d", &dataLength);
brackets = malloc(dataLength * sizeof(char*));
for (int i = 0; i < dataLength; i++) {
brackets[i] = malloc(100005 * sizeof(char));
scanf("%s", brackets[i]);
// 读一个换行符
getchar();
int index = 0;
bool isStdBracket= true;
while (brackets[i][index] != '\0') {
// 找到字符串结尾的办法就是找'\0'
if (isLeftBracket(brackets[i][index])) {
push(brackets[i][index]);
} else if (isRightBracket(brackets[i][index])) {
char leftBracket = pop();
if (!isMatch(leftBracket, brackets[i][index])) {
isStdBracket = false;
break;
}
}
index++;
}
if (isStdBracket) {
printf("True\n");
} else {
printf("False\n");
}
clear();
// free memory after use
free(brackets[i]);
}
free(brackets);
return 0;
}
char pop() {
// 弹出栈顶的一个元素
if (pointer == -1) {
// 此时栈内没有元素
// 我们用'\0'来代表为空
return '\0';
}
char popElement = stack[pointer];
pointer--;
return popElement;
}
void push(char x) {
// 往栈中压入一个元素
pointer++;
stack[pointer] = x;
}
void clear() {
// 清空栈
// 把指针变回-1即可,不需要清空里面的内容
pointer = -1;
}
bool isLeftBracket(char bracket) {
if (bracket == '{' || bracket == '(' || bracket == '[') {
return true;
}
return false;
}
bool isRightBracket(char bracket) {
if (bracket == '}' || bracket == ')' || bracket == ']') {
return true;
}
return false;
}
bool isMatch(char leftBracket, char rightBracket) {
if ((leftBracket == '{' && rightBracket == '}') ||
(leftBracket == '[' && rightBracket == ']') ||
(leftBracket == '(' && rightBracket == ')')) {
return true;
}
return false;
}
//
// Created by Zyi on 2021/11/22.
//
//
// Created by Zyi on 2021/11/22.
//
#include <stdio.h>
#include <malloc.h>
#include <string.h>
char** operation;
// 用数组来模拟栈
// 这里不用指针和malloc的原因是因为malloc需要运行时开辟
// 而win下的栈空间太小,懒得编译调整栈大小了
char stack[10004];
// 维护一个指向栈顶的指针即可
int pointer = -1;
char pop();
char top();
void push(char x);
void print();
int main() {
int n;
scanf("%d", &n);
getchar();
operation = malloc(n * sizeof(char*));
for (int i = 0; i < n; i++) {
operation[i] = malloc(20 * sizeof(char));
// 读取字符串
scanf("%s", operation[i]);
if (strcmp(operation[i], "pop") == 0) {
// 执行pop操作
char popElement = pop();
if (popElement == '\0') {
printf("Empty\n");
}
} else if (strcmp(operation[i], "top") == 0) {
// 执行top操作
char topElement = top();
if (topElement != '\0') {
printf("%c\n", topElement);
} else {
printf("Empty\n");
}
} else if (strcmp(operation[i], "print") == 0) {
// 执行print操作
print();
} else if (strcmp(operation[i], "push") == 0) {
// 第6个char是我们要push的元素
char pushElement;
// 读取一个空格
getchar();
scanf("%c", &pushElement);
push(pushElement);
}
getchar();
}
for (int i = 0; i < n; i++) {
free(operation[i]);
}
free(operation);
return 0;
}
char pop() {
// 弹出栈顶的一个元素
// eg: 若从栈底到栈顶的元素为1, 3, 5, 7
// 则执行完pop操作后,栈中元素为1, 3, 5
if (pointer == -1) {
// 此时栈内没有元素
// 我们用'\0'来代表为空
return '\0';
}
char popElement = stack[pointer];
pointer--;
return popElement;
}
char top() {
// 返回栈顶元素
// 与pop()不同之处在于其不弹出, 保留元素
if (pointer == -1) {
return '\0';
}
char topElement = stack[pointer];
return topElement;
}
void push(char x) {
// 往栈中压入一个元素
pointer++;
stack[pointer] = x;
}
void print() {
// 输出栈中的元素
for (int i = pointer; i >= 0; i--) {
printf("| %c |\n", stack[i]);
}
printf("|===|\n");
}
//
// Created by Zyi on 2021/11/22.
//
#include <stdio.h>
#include <malloc.h>
int* values;
int* maxAdded;
void bubbleSort(int length);
void swap(int indexOne, int indexTwo);
int main() {
int n, maxCapacity;
scanf("%d %d", &n, &maxCapacity);
values = malloc(n * sizeof(int));
maxAdded = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &values[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &maxAdded[i]);
}
// 排序
// 这里用的是冒泡排序
// 想追求快速的话可以用快排,代码在下面,用了分治的思想(你会觉得好像也是递归
bubbleSort(n);
// 贪心
int totalValue = 0;
for (int i = 0; i < n; i++) {
if (maxCapacity >= maxAdded[i]) {
// 如果酒杯所剩容量大于能加的最大容量,就全部加进去
maxCapacity -= maxAdded[i];
totalValue += values[i] * maxAdded[i];
} else {
// 否则就把酒杯所剩容量加完即可
totalValue += maxCapacity * values[i];
break;
}
}
printf("%d", totalValue);
// free memory
free(values);
free(maxAdded);
return 0;
}
void bubbleSort(int length) {
// 从大到小排序
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
if (values[j] < values[j + 1]) {
swap(j, j + 1);
}
}
}
}
void swap(int indexOne, int indexTwo) {
int tempVal = values[indexOne];
values[indexOne] = values[indexTwo];
values[indexTwo] = tempVal;
int tempAdded = maxAdded[indexOne];
maxAdded[indexOne]= maxAdded[indexTwo];
maxAdded[indexTwo] = tempAdded;
}
Supports Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册