arrays - 查找数组中的最大值
问题描述
基本上,我需要找到具有最大价值的数组。我给出像 (3,7) 这样的初始点,程序查看周围的数组,选择最大的数组,并对选定的数组执行完全相同的操作。这个循环一直持续到它找到该区域中最大的数组。请查看图片以获取更多信息
到目前为止,我只能这样做:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float Q[10][10]=
{0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,
0.1,0.2,0.2,0.2,0.2,0.3,0.3,0.3,0.3,0.1,
0.1,0.2,0.3,0.3,0.4,0.5,0.5,0.5,0.5,0.1,
0.1,0.2,0.3,0.3,0.5,0.5,0.5,0.7,0.7,0.1,
0.1,0.2,0.4,0.4,0.5,0.7,0.7,0.8,0.7,0.1,
0.1,0.2,0.4,0.4,0.5,0.7,0.8,0.9,0.8,0.1,
0.1,0.2,0.4,0.4,0.5,0.7,0.8,0.9,1.4,0.1,
0.1,0.2,0.4,0.4,0.5,0.8,0.8,0.9,1.2,0.1,
0.1,0.2,0.4,0.4,0.5,0.7,0.8,0.9,1.1,0.1,
0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1};
int x0=3, y0=7, i,j;
int x02=x0+2, y02=y0+2;
int x03=x0-2, y03=y0-2;
float small_one=Q[x0][y0], big_one=small_one;
while (x0<x02) {
x0=x0++;
if(small_one<Q[x0][y0])
{
big_one=Q[x0][y0];
}
}
while (y0<y02){
y0=y0++;
if(small_one<Q[x0][y0])
{
big_one=Q[x0][y0];
}
printf("&f",big_one); }
return 0;}
解决方案
我认为您错过了几件事.. 首先,您需要使用 {{},{}} 声明您的多宗派数组,
其次,你的每一步你都必须查看每一边并将旧值与你刚刚检查的正方形上的点进行比较,
最后是您的停止条件:您需要在无处可去的地方停下来。
注意:我没有这样做,但你应该检查你的“板”边界,这样你就不会越过它进入“下一步”
#include <stdlib.h>
#include <string.h>
float ProceedToBiggerElement(float Q[10][10], int *x0, int *y0)
{
float max = Q[*x0][*y0];
int x_index = *x0;
int y_index = *y0;
int index = 0;
int j_index = 0;
for (index = x_index - 1; index <= x_index + 1; ++index)
{
for (j_index = y_index - 1; j_index <= y_index + 1; ++j_index)
{
if (Q[index][j_index] > max)
{
max = Q[index][j_index];
*x0 = index;
*y0 = j_index;
}
}
}
return (max);
}
int main()
{
float Q[10][10]=
{{0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1},
{0.1,0.2,0.2,0.2,0.2,0.3,0.3,0.3,0.3,0.1},
{0.1,0.2,0.3,0.3,0.4,0.5,0.5,0.5,0.5,0.1},
{0.1,0.2,0.3,0.3,0.5,0.5,0.5,0.7,0.7,0.1},
{0.1,0.2,0.4,0.4,0.5,0.7,0.7,0.8,0.7,0.1},
{0.1,0.2,0.4,0.4,0.5,0.7,0.8,0.9,0.8,0.1},
{0.1,0.2,0.4,0.4,0.5,0.7,0.8,0.9,1.4,0.1},
{0.1,0.2,0.4,0.4,0.5,0.8,0.8,0.9,1.2,0.1},
{0.1,0.2,0.4,0.4,0.5,0.7,0.8,0.9,1.1,0.1},
{0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1}};
int x0=3, y0=7, i,j;
float new_x0 = 0; float old_x0 = Q[x0][y0];
float init_x0 = old_x0;
while (old_x0 != new_x0)
{
old_x0 = new_x0;
new_x0 = ProceedToBiggerElement(Q, &x0, &y0);
}
printf("initiat piont is %f, final point is %f", init_x0, new_x0);
return (0);
}```
推荐阅读
- sql - 触发sql问题
- javascript - 用于构建 URL 的用户输入
- python - 如何在执行 matplotlib 图形后允许 python 代码继续,以便循环可以将实时数据输入 matplotlib 图形
- junit - 如何使用 Mockito 模拟抽象类?
- sql - where 子句后的 case 语句,如果满足则省略该行数据
- node.js - 如何将您的网站列入白名单以便能够查看 AWS S3 存储桶中的文件?
- r - Persistent data in reactive editable table in Shiny app using DT
- regex - 正则表达式匹配多个字符串之一,后跟另一个字符串
- bash - 在 cronjob 中使用 aws-cli
- javascript - 通过带有 UID 的 API 实时更新 firebase