C 计算小矩阵中出现在大矩阵中的字母数
C-Void方法计算小矩阵中有多少个字母在不使用string.h的情况下也出现在大矩阵中C 计算小矩阵中出现在大矩阵中的字母数,c,C,C-Void方法计算小矩阵中有多少个字母在不使用string.h的情况下也出现在大矩阵中 Count()函数需要使用指针对小矩阵中出现在大矩阵中的字母进行计数 结果应显示在新的结果矩阵中。结果矩阵的维数与小矩阵相同,但每个单元格都是整数计数,表示小矩阵中的字母在大矩阵中出现的次数 我尝试使用指针指向小矩阵中的特定字符 void Count(char mat[ROWS][COLS], char smallMat[SIZE][SIZE]) { int i, j; int count
Count()
函数需要使用指针对小矩阵中出现在大矩阵中的字母进行计数
结果应显示在新的结果矩阵中。结果矩阵的维数与小矩阵相同,但每个单元格都是整数计数,表示小矩阵中的字母在大矩阵中出现的次数
我尝试使用指针指向小矩阵中的特定字符
void Count(char mat[ROWS][COLS], char smallMat[SIZE][SIZE]) {
int i, j;
int count = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
const char stringptr = smallMat[i][j];
if (stringptr == mat[i][j])
count++;
}
}
return count;
}
void Count(char mat[ROWS][COLS],char smallMat[SIZE][SIZE]){
int i,j;
整数计数=0;
对于(i=0;i
计数应返回main。
2个矩阵和新矩阵的示例应为
大矩阵
P A D A Q E Q B G H R O P H C W S P B Q
M B R P R N V S C H M U J P W C V M F D
V W R K E V I Y K K Q Y N H N G V L B Z
U N T E
C P G X
D L A B
1 3 0 2
3 4 2 0
2 1 2 4
P A D A Q E Q B G H R O P H C W S P B Q
M B R P R N V S C H M U J P W C V M F D
V W R K E V I Y K Q Y N H N V L B Z
小矩阵
P A D A Q E Q B G H R O P H C W S P B Q
M B R P R N V S C H M U J P W C V M F D
V W R K E V I Y K K Q Y N H N G V L B Z
U N T E
C P G X
D L A B
1 3 0 2
3 4 2 0
2 1 2 4
U N T E
CpGx
D L A B
结果矩阵
P A D A Q E Q B G H R O P H C W S P B Q
M B R P R N V S C H M U J P W C V M F D
V W R K E V I Y K K Q Y N H N G V L B Z
U N T E
C P G X
D L A B
1 3 0 2
3 4 2 0
2 1 2 4
1 3 0 2
3 4 2 0
2 1 2 4
像这样的东西可能有用。我会将其分解为一个计数函数,该函数包含两个参数:您正在搜索的字母和指向大矩阵的指针 然后,我将创建一个与小矩阵具有相同维度的结果矩阵,并为结果矩阵的每个单元格调用count函数,传入大矩阵,并将每个count调用的结果分配给结果矩阵的每个后续单元格 最后,为了清楚main()函数,创建一个最终的函数来打印结果矩阵。大概是这样的: 编辑:我根据OP问题的更新编辑了代码。此代码创建以下输出:
$> a.out
P A D A Q E Q B G H R O P H C W S P B Q
M B R P R N V S C H M U J P W C V M F D
V W R K E V I Y K K Q Y N H N G V L B Z
U N T E
C P G X
D L A B
1 3 0 2
3 5 2 0
2 1 2 4
The most repetitive char(s) occurred 5 time(s): P
The least repetitive char(s) occurred 1 time(s): U L
Process finished with exit code 0
该代码打印最高和最低频率数的所有分钟数和所有最大字符数。为此,它保留一个max和min数组。该数组不需要长于结果矩阵边的乘积
如果没有字符出现至少1次,则不会打印最大值和最小值
虽然OP建议我更新Count()函数来搜索最频繁的字母,但这确实变得很复杂,特别是当有多个字母在最小插槽的最大插槽中出现相同次数时
因此,我编写了一个利用ResultMatrix的新函数。ResultMatrix已包含频率计数。小矩阵告诉我们哪些字母最常见
因此,PrintMaxesAndMins()
在实际收集与最小值对应的字符和与最小值对应的字符之前,可以对代码进行优化,以首先找到最小值和最大值。而不是这样做,我的代码在每次发现更高的最大值时都会重置最大值字符的字符串
如果你有任何问题,请告诉我。如果这对你有效,请接受答案。祝你周末愉快
代码如下:
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
const size_t ROWS = 3;
const size_t COLUMNS = 20;
const size_t SMALL_ROWS = 3;
const size_t SMALL_COLUMNS = 4;
const size_t SIZE = 4;
char LargeMatrix[ROWS][COLUMNS] =
{{'P', 'A', 'D', 'A', 'Q', 'E', 'Q', 'B', 'G', 'H', 'R', 'O', 'P', 'H', 'C',
'W', 'S', 'P', 'B', 'Q'},
{'M', 'B', 'R', 'P', 'R', 'N', 'V', 'S', 'C', 'H', 'M', 'U', 'J', 'P', 'W',
'C', 'V', 'M', 'F', 'D'},
{'V', 'W', 'R', 'K', 'E', 'V', 'I', 'Y', 'K', 'K', 'Q', 'Y', 'N', 'H', 'N',
'G', 'V', 'L', 'B', 'Z'},};
char SmallMatrix[SIZE][SIZE] =
{{'U', 'N', 'T', 'E'}, {'C', 'P', 'G', 'X'}, {'D', 'L', 'A', 'B'}};
void Count(char mat[ROWS][COLUMNS], char c, size_t *count)
{
size_t counter = 0; // Initialize the count answer cell
for (size_t i = 0; i < ROWS; i++) {
for (size_t j = 0; j < COLUMNS; j++) {
if (mat[i][j] == c)
counter++;
}
}
*count = counter;
}
// clear the char array
void zero_char_array(char *array, size_t len)
{
for (size_t i = 0; i < len; i++)
array[i] = 0;
}
//
//
void PrintMaxesAndMins(char haystack[ROWS][COLUMNS],
char needlestack[SMALL_ROWS][SMALL_COLUMNS],
size_t answerStack[SMALL_ROWS][SMALL_COLUMNS],
size_t result_rows,
size_t result_columns)
{
char max_char; // char that occurred the most
char min_char; // char that occurred the least
size_t max_char_count =
0; // best to use unsigned ints when a value should never go negative.
size_t min_char_count = UINT32_MAX; // Value should not go negative.
char max_chars[SMALL_COLUMNS * SMALL_ROWS]; // save all possible max chars
char *next_max_char;
char min_chars[SMALL_COLUMNS * SMALL_ROWS]; // sall all possible min chars
char *next_min_char;
size_t counter = 0; // Initialize the count answer cell
for (size_t i = 0; i < result_rows; i++) {
for (size_t j = 0; j < result_columns; j++) {
if (answerStack[i][j] > max_char_count) {
max_char_count = answerStack[i][j]; // we have a new max
zero_char_array(max_chars, SMALL_COLUMNS * SMALL_ROWS);
next_max_char =
max_chars; // We have a new max, reset result char array
*next_max_char = needlestack[i][j];// grab character from needle stack
++next_max_char; // increment. Could do in prior line, but try to be clear.
} else if (answerStack[i][j] >= max_char_count) {
// we are adding to an old max
*next_max_char = needlestack[i][j];// grab character from needle stack
++next_max_char; // increment. Could do in prior line, but try to be clear.
}
if (answerStack[i][j] > 0 && answerStack[i][j] < min_char_count) {
min_char_count = answerStack[i][j]; // we have a new min
zero_char_array(min_chars, SMALL_COLUMNS * SMALL_ROWS);
next_min_char =
min_chars; // We have a new min, reset result char array
*next_min_char = needlestack[i][j];// grab character from needle stack
++next_min_char; // increment. Could do in prior line, but try to be clear.
} else if (answerStack[i][j] > 0 && answerStack[i][j] <= min_char_count) {
// we are adding to an old max
*next_min_char = needlestack[i][j];// grab character from needle stack
++next_min_char; // increment. Could do in prior line, but try to be clear.
}
}
}
if (max_char_count > 0) {
printf("The most repetitive char(s) occurred %lu time(s): ", max_char_count);
next_max_char = max_chars;
while (*next_max_char)
printf(" %c", *next_max_char++);
}
printf("\n\n");
if (min_char_count > 0) {
printf("The least repetitive char(s) occurred %lu time(s): ", min_char_count);
next_min_char = min_chars;
while (*next_min_char) {
printf(" %c", *next_min_char++);
}
}
}
// BuildResultMatrix()
// haystack is a pointer to the large matrix.
// needlestack is a pointer to the small matrix.
// answerStack is the result matrix of size_t
void BuildResultMatrix(char haystack[ROWS][COLUMNS],
char needlestack[SMALL_ROWS][SMALL_COLUMNS],
size_t answerStack[SMALL_ROWS][SMALL_COLUMNS],
size_t result_rows,
size_t result_columns)
{
// Loop through the rows and columns of the small matrix
for (size_t i = 0; i < result_rows; i++) {
for (size_t j = 0; j < result_columns; j++) {
// Pass the current cell of the small matrix to Count()
// Count() will then loop through the large matrix, counting
// the number of cells with the current cell value from the small matrix.
// Count() will place the final count into the correpsonding cell of the
// answerStack matrix.
Count(LargeMatrix, needlestack[i][j], &answerStack[i][j]);
}
}
}
void PrintLargeCharMatrix(char (*matrix)[COLUMNS], size_t rows)
{
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < COLUMNS; j++) {
printf("%c ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
void PrintSmallCharMatrix(char (*matrix)[SMALL_COLUMNS], size_t rows)
{
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < SMALL_COLUMNS; j++) {
printf("%c ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
void PrintCountMatrix(size_t (*matrix)[SMALL_COLUMNS], size_t rows)
{
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < SMALL_COLUMNS; j++) {
printf("%lu ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
size_t ResultMatrix[SMALL_ROWS][SMALL_COLUMNS];
PrintLargeCharMatrix(LargeMatrix, ROWS);
PrintSmallCharMatrix(SmallMatrix, SMALL_ROWS);
BuildResultMatrix(LargeMatrix,
SmallMatrix,
ResultMatrix,
SMALL_ROWS,
SMALL_COLUMNS);
PrintCountMatrix(ResultMatrix, SMALL_ROWS);
PrintMaxesAndMins(LargeMatrix,
SmallMatrix,
ResultMatrix,
SMALL_ROWS,
SMALL_COLUMNS);
return 0;
}
#包括
#包括
#包括
const size_t ROWS=3;
const size_t COLUMNS=20;
const size\u t SMALL\u ROWS=3;
const size\u t SMALL\u COLUMNS=4;
常数大小=4;
char LargeMatrix[行][列]=
{{'P',A',D',A',Q',E',Q',B',G',H',R',O',P',H',C',
‘W’、‘S’、‘P’、‘B’、‘Q’},
{'M','B','R','P','R','N','V','S','C','H','M','U','J','P','W',',
‘C’、‘V’、‘M’、‘F’、‘D’},
{'V','W','R','K','E','V','I','Y','K','Q','Y','N','H','N',',
‘G’、‘V’、‘L’、‘B’、‘Z’}、};
字符矩阵[大小][大小]=
{'U','N','T','E'},{'C','P','G','X'},{'D','L','A','B'};
无效计数(字符表[行][列],字符c,大小*计数)
{
size\u t counter=0;//初始化计数应答单元格
对于(大小i=0;i最大字符数){
max\u char\u count=answerStack[i][j];//我们有了一个新的max
零字符数组(最大字符、小列*小行);
下一个\u max\u char=
max_chars;//我们有一个新的max,reset result char数组
*next_max_char=neederstack[i][j];//从针堆中获取字符
++next_max_char;//increment.可以在前一行中执行,但请尽量清楚。
}else if(answerStack[i][j]>=最大字符数){
//我们正在添加一个旧的max
*下一个\u最大\u字符=针