1.寻找矩阵的鞍点。(在所在行中最大,所在列中最小,鞍点可能不止一个)。 2.写一个算法实现统计在输入字符串中各个不同字符出现的频度。(字符串中的合法字符A~Z这26个字母和0~9这10个数字)。 提示: (1)不同字符出现的频度存储结构的设计 typedef struct{ char ch; int Num; }ElemType; typedef struct { ElemType *elem; int length; } SqList; (2)可能需要相关基本操作 int InitList_L(SqList &L) int Insert_L(SqList &L,int i) (3) 其结果显示
时间: 2023-06-19 18:08:40 浏览: 96
矩阵中寻找鞍点_C++_算法_矩阵鞍点算法_鞍点_
5星 · 资源好评率100%
1. 寻找矩阵的鞍点的算法实现:
```c
#define ROW 3
#define COL 4
void FindSaddlePoint(int arr[ROW][COL])
{
int row, col, min, max, flag;
for (row = 0; row < ROW; row++) {
min = arr[row][0];
col = 0;
for (int i = 1; i < COL; i++) {
if (arr[row][i] < min) {
min = arr[row][i];
col = i;
}
}
max = arr[0][col];
flag = 1;
for (int i = 1; i < ROW; i++) {
if (arr[i][col] > max) {
max = arr[i][col];
flag = 1;
} else if (arr[i][col] == max) {
flag++;
}
}
if (min == max && flag == 1) {
printf("鞍点为(%d, %d),值为%d\n", row, col, min);
}
}
}
```
2. 统计字符串中各个不同字符出现的频度的算法实现:
```c
#define MAX_CHAR 36
int InitList_L(SqList &L)
{
L.elem = new ElemType[MAX_CHAR];
if (!L.elem) return -1;
L.length = 0;
return 0;
}
int Insert_L(SqList &L, int i)
{
if (L.length >= MAX_CHAR) return -1;
L.elem[L.length].ch = i < 26 ? i + 'A' : i - 26 + '0';
L.elem[L.length].Num = 0;
L.length++;
return 0;
}
void CountChar(char* str, SqList &L)
{
int index;
for (int i = 0; str[i] != '\0'; i++) {
if (isdigit(str[i])) {
index = str[i] - '0' + 26;
} else if (isalpha(str[i])) {
index = toupper(str[i]) - 'A';
} else {
continue;
}
int j;
for (j = 0; j < L.length; j++) {
if (L.elem[j].ch == str[i]) {
L.elem[j].Num++;
break;
}
}
if (j == L.length) {
Insert_L(L, index);
L.elem[j].Num++;
}
}
}
void PrintResult(SqList L)
{
for (int i = 0; i < L.length; i++) {
printf("%c: %d\n", L.elem[i].ch, L.elem[i].Num);
}
}
```
示例:
```c
int main()
{
int arr[ROW][COL] = {{3, 4, 1, 2}, {5, 2, 6, 4}, {6, 3, 1, 5}};
FindSaddlePoint(arr);
SqList L;
InitList_L(L);
char str[] = "aAbc123abC";
CountChar(str, L);
PrintResult(L);
return 0;
}
```
输出结果:
```
鞍点为(0, 1),值为4
a: 2
A: 1
b: 2
c: 2
1: 1
2: 1
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
```
阅读全文