PTA 程序设计题(数据结构第一章)
C语言版
第一题 二分查找
感觉还好
Position BinarySearch(List L, ElementType X) { // 数组大小 // int N = sizeof(L->Data) / sizeof(*L->Data); int start = 1; int end = L->Last; int mid; while (start <= end) { mid = (start + end) / 2; if (L->Data[mid] > X) end = mid - 1; else if (L->Data[mid] < X) start = mid + 1; else return mid; } return NotFound; }
第二题 最大子序列和
方法1
#include<stdio.h> #include<malloc.h> int Sum(int A[], int N) { int maxSum, thisSum; maxSum = thisSum = 0; for (int i = 0; i < N; i++) { thisSum = 0; for (int j = i; j < N; j++) { for (int k = i; k <= j; k++) thisSum += A[k]; if (thisSum > maxSum) maxSum = thisSum; } } return maxSum; } int main(void) { int* a = NULL; int N; scanf("%d", &N); a = (int*)malloc(N * sizeof(int)); for (int i = 0; i < N; i++) { scanf("%d", &a[i]); } printf("%d\n", Sum(a, N)); }
方法二
#include<stdio.h> #include<malloc.h> int Sum(int A[], int N) { int maxSum, thisSum; maxSum = thisSum = 0; for (int i = 0; i < N; i++) { thisSum += A[i]; if (thisSum > maxSum) { maxSum = thisSum; } else if (thisSum < 0) { thisSum = 0; } } return maxSum; } int main(void) { int* a = NULL; int N; scanf("%d", &N); a = (int*)malloc(N * sizeof(int)); for (int i = 0; i < N; i++) { scanf("%d", &a[i]); } printf("%d\n", Sum(a, N)); }
方法四 在线处理
#include<stdio.h> #include<malloc.h> #include<time.h> clock_t start, endness; double duration; #define recyle_k 1e7 int Sum(int A[], int N, int* front, int* end) { int maxSum, thisSum; maxSum = thisSum = 0; for (int i = 0; i < N; i++) { thisSum += A[i]; if (thisSum > maxSum) { maxSum = thisSum; *end = i; } else if (thisSum < 0) { thisSum = 0; *front = i + 1; } } return maxSum; } int main(void) { int* a = NULL; int N; int front, end; int result; scanf_s("%d", &N); front = 0, end = N; a = (int*)malloc(N * sizeof(int)); for (int i = 0; i < N; i++) { scanf_s("%d", &a[i]); } start = clock(); for (int i = 0; i < recyle_k; i++) { result = Sum(a, N, &front, &end); } endness = clock(); duration = ((double)(endness - start)) / CLK_TCK / recyle_k; printf("消耗时间: %2.6e \n", duration); printf("最大和: %d\n", result); printf("子序列首尾:[%d, %d]", front, end); return 0; }
关于 返回子序列首尾,不知道为什么总是不对,吐血
#include<stdio.h> #include<malloc.h> int Sum(int A[], int N, int* front, int* end) { int maxSum = 0, thisSum = 0; int flag = 0, m = 0; // flag 作为 全负数 标记 + -2 -3 5 0 for (int i = 0; i < N; i++) { thisSum += A[i]; if (thisSum < 0) { thisSum = 0; } else if (thisSum > maxSum) { maxSum = thisSum; *end = i; } if (A[i] == 0) { flag = 0; m = i; } } maxSum = thisSum > maxSum ? thisSum : maxSum; if (maxSum != 0) { thisSum = 0; for (int i = *end; i >= 0 ; i--) { thisSum += A[i]; if (maxSum == thisSum) { *front = i; } } } else if (flag) { *front = 0; *end = N; } else { *front = m; *end = m; } return maxSum; } int main(void) { int* a = NULL; int N; int front, end; int result; scanf("%d", &N); front = 0, end = N; a = (int*)malloc(N * sizeof(int)); for (int i = 0; i < N; i++) { scanf("%d", &a[i]); } result = Sum(a, N, &front, &end); printf("%d %d %d ", result, front, end); return 0; }
暂时先这样吧。纠结
相关推荐
koushr 2020-11-12
zhangxiafll 2020-11-13
kikaylee 2020-10-31
范范 2020-10-28
MILemon 2020-10-22
hugebawu 2020-10-12
LauraRan 2020-09-28
shenwenjie 2020-09-24
omyrobin 2020-09-23
guangcheng 2020-09-22
qiangde 2020-09-13
hanyujianke 2020-08-18
晨曦之星 2020-08-14
xiesheng 2020-08-06
KAIrving 2020-08-02
xiesheng 2020-08-02
范范 2020-07-30
chenfei0 2020-07-30