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

update 7-data-types

上级 f985e3df
......@@ -7,3 +7,6 @@ add_executable(stack stack.c)
add_executable(wine wine.c)
add_executable(brackets brackets.c)
add_executable(pour pour.c)
add_executable(max_diff max_diff.c)
add_executable(max_diff_another max_diff_another.c)
add_executable(three_view three_view.c)
//
// Created by Zyi on 2021/11/22.
//
#include <stdio.h>
int numbers[1003];
int maxDiff[1003];
int length = 0;
int main() {int index = 0;
int n;
while (scanf("%d", &n) != EOF) {
numbers[index++] = n;
length++;
}
// 只要找到最大值然后和第一位和最后一位比较就可以了
int maxElement = numbers[0];
int maxIndex = 0;
for (int i = 0; i < length; i++) {
if (numbers[i] > maxElement) {
maxElement = numbers[i];
maxIndex = i;
}
}
// 维护左右子数组的最大值
int leftIndex = 0;
if (maxIndex == 0) {
// 此时最大数在第一个位置
// 左边没有数了
leftIndex = 0;
} else {
maxDiff[0] = maxElement - numbers[0];
for (int i = 1; i < maxIndex; i++) {
if (numbers[i] > numbers[i - 1]) {
// 此时开头子数组最大值变化,我们需要更新值
// 此时子数组最大数变了,我们其实不需要后面的值了
leftIndex = i - 1;
break;
} else {
maxDiff[i] = maxDiff[i - 1];
}
}
}
int rightIndex = length - 1;
if (maxIndex == length - 1) {
// 如果最大值是最后的元素
rightIndex = length - 1;
} else {
maxDiff[length - 1] = maxElement - numbers[length - 1];
for (int i = length - 2; i > maxIndex; i--) {
if (numbers[i] > numbers[i + 1]) {
rightIndex = i + 1;
break;
} else {
maxDiff[i] = maxDiff[i + 1];
}
}
}
int maxDifference = maxDiff[leftIndex] > maxDiff[rightIndex] ? maxDiff[leftIndex] : maxDiff[rightIndex];
printf("%d", maxDifference);
return 0;
}
//
// Created by Zyi on 2021/11/23.
//
#include <stdio.h>
int numbers[1003];
int length = 0;
int main() {
int index = 0;
int n;
while (scanf("%d", &n) != EOF) {
numbers[index++] = n;
length++;
}
// 只要找到最大值然后和第一位和最后一位比较就可以了
int maxElement = numbers[0];
for (int i = 0; i < length; i++) {
if (numbers[i] > maxElement) {
maxElement = numbers[i];
}
}
int maxDiff = maxElement - numbers[0] > maxElement - numbers[length - 1] ? maxElement - numbers[0] : maxElement - numbers[length - 1];
printf("%d", maxDiff);
}
//
// Created by Zyi on 2021/11/22.
//
#include <stdio.h>
#include <stdbool.h>
long capacity[5];
bool remainOne(long a0, long b0, long c0);
bool down(long a0, long b0, long c0);
int main() {
int k;
// 本题需要用long
long va, vb, vc, a, b, c, a0, b0, c0;
scanf("%d %ld %ld %ld %ld %ld %ld %ld %ld %ld", &k, &va, &vb, &vc, &a, &b, &c, &a0, &b0, &c0);
capacity[0] = va;
capacity[1] = vb;
capacity[2] = vc;
// 首先判断一些特殊情况
if (a0 > va || b0 > vb || c0 > vc) {
// 目标容量大于最大容量
printf("No");
} else if (a + b + c != a0 + b0 + c0) {
// 总容量不相符
printf("No");
} else if (a0 == capacity[0] && b0 == capacity[1] && c0 == capacity[2]) {
// 等于原来的状态,只需要0步
printf("Yes");
} else if (remainOne(a0, b0, c0)) {
// 全部移到一个杯子中,最多需要两步
if (k >= 2) {
printf("Yes");
} else {
printf("No");
}
} else {
int step = 0;
if (a0 == capacity[0] + capacity[1]) {
capacity[0] += capacity[1];
capacity[1] = 0;
step++;
if (down(a0, b0, c0)) {
if (k >= 1) {
printf("Yes");
} else {
printf("No");
}
} else {
// 还未完成说明只需要移动c到b即可
if (k >= 2) {
printf("Yes");
} else {
printf("No");
}
}
}
}
return 0;
}
bool remainOne(long a0, long b0, long c0) {
if (a0 == 0) {
if (b0 == 0 && c0 != 0) {
return true;
} else if (c0 == 0 && b0 != 0) {
return false;
}
} else if (b0 == 0) {
if (c0 == 0) {
return true;
} else {
return false;
}
}
return false;
}
bool down(long a0, long b0, long c0) {
return capacity[0] == a0 && capacity[1] == b0 && capacity[2] == c0;
}
\ No newline at end of file
//
// Created by Zyi on 2021/11/23.
//
#include <stdio.h>
#include <malloc.h>
int** height;
int* maxColumnHeight;
int* maxRowHeight;
int row, column;
void printMainView();
void printLeftView();
int main() {
scanf("%d %d", &row, &column);
height = malloc(row * sizeof(int*));
for (int i = 0; i < row; i++) {
height[i] = malloc(column * sizeof(int));
for (int j = 0; j < column; j++) {
scanf("%d", &height[i][j]);
}
}
// 找到最大值即可
// 首先是主视图
// 遍历每一列找到最大值即可
maxColumnHeight = malloc(column * sizeof(int));
for (int j = 0; j < column; j++) {
int columnHeight = height[0][j];
for (int i = 0; i < row; i++) {
if (height[i][j] > columnHeight) {
columnHeight = height[i][j];
}
}
maxColumnHeight[j] = columnHeight;
}
maxRowHeight = malloc(row * sizeof(int));
for (int i = 0; i < row; i++) {
int rowHeight = height[i][0];
for (int j = 0; j < column; j++) {
if (height[i][j] > rowHeight) {
rowHeight = height[i][j];
}
}
maxRowHeight[i] = rowHeight;
}
// 打印主视图
printMainView();
printf("\n");
printLeftView();
// free memory
for (int i = 0; i < row; i++) {
free(height[i]);
}
free(height);
free(maxColumnHeight);
free(maxRowHeight);
return 0;
}
void printMainView() {
int maxHeight = 0;
for (int i = 0; i < column; i++) {
if (maxColumnHeight[i] > maxHeight) {
maxHeight = maxColumnHeight[i];
}
}
for (int i = maxHeight; i > 0; i--) {
for (int j = 0; j < column; j++) {
if (maxColumnHeight[j] == i) {
printf("*");
maxColumnHeight[j]--;
} else {
printf(" ");
}
}
printf("\n");
maxHeight--;
}
}
void printLeftView() {
int maxHeight = 0;
for (int i = 0; i < row; i++) {
if (maxRowHeight[i] > maxHeight) {
maxHeight = maxRowHeight[i];
}
}
for (int i = maxHeight; i > 0; i--) {
for (int j = 0; j < row; j++) {
if (maxRowHeight[j] == i) {
printf("*");
maxRowHeight[j]--;
} else {
printf(" ");
}
}
printf("\n");
maxHeight--;
}
}
......@@ -13,6 +13,7 @@
1. 3-for-a-while
2. 5-function
3. 6-recursion
3. 7-data-types
......
Supports Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册