C語言算法練習(xí)之求二維數(shù)組最值問題
一、問題描述
求二維數(shù)組最大最小值
問題的描述
如下幾點(diǎn)所示
1.在n 行 n 列的二維整數(shù)數(shù)組中,按以下要求選出兩個(gè)數(shù)。
2.首先從每行選出大數(shù),再從選出 的 n 個(gè)大數(shù)中選出小數(shù);
3.其次,從每行 選出小數(shù),再從選出的 n 個(gè)小數(shù)中選出大數(shù)。
二、算法實(shí)例編譯環(huán)境
本文C語言經(jīng)典算法實(shí)例的編譯環(huán)境,使用的是集成開發(fā)環(huán)境:Visual Studio 2019


Visual Studio 2019官網(wǎng)鏈接如下

Visual Studio 2019集成的開發(fā)環(huán)境的特點(diǎn)有
- Visual Studio 2019默認(rèn)安裝Live Share代碼協(xié)作服務(wù)。
- 幫助用戶快速編寫代碼的新歡迎窗口、改進(jìn)搜索功能、總體性能改進(jìn)。
- Visual Studio IntelliCode AI幫助。
- 更好的Python虛擬和Conda支持。
- 以及對(duì)包括WinForms和WPF在內(nèi)的.NET Core 3.0項(xiàng)目支持等
三、算法實(shí)例實(shí)現(xiàn)過程
3.1、包含頭文件
包含頭文件 代碼如下所示
#include <stdio.h> #include <stdlib.h>
將要用到的C語言頭文件包含近年來。
3.2、定義宏和聲明數(shù)組
定義宏和聲明數(shù)組 代碼如下所示
#define MAXN 20 int a[MAXN][MAXN];
定義了MAXN ,代表了MAXN為常數(shù)20。
聲明了數(shù)組a。
3.3、聲明相關(guān)變量
聲明相關(guān)變量 代碼如下所示
int min, max;
int row, col, n;
聲明相關(guān)變量min, max,row, col, n;
3.4、輸入數(shù)組(方陣)的階
輸入數(shù)組(方陣)的階 代碼如下所示
printf("Please input the order of the matrix:");/* 輸入方陣的階次 */
scanf("%d", &n);
printf("\nPlease input the elements of the matrix,\nfrom a[0][0] to a[%d][%d]:\n", n - 1, n - 1);
for (row = 0; row < n; row++)
{
for (col = 0; col < n; col++)
{
scanf("%d", &a[row][col]);
}
}
根據(jù)文字提示,輸入數(shù)組(方陣)的階。
根據(jù)文字提示,向數(shù)組中輸入數(shù)據(jù)。
3.5、輸出 “輸入的數(shù)組”
輸出 “輸入的數(shù)組” 代碼如下所示
printf("\nThe original matrix is\n");
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
printf("%d ", a[row][col]);
}
printf("\n");
}
printf("\n");
輸出 我們向數(shù)組中輸入的數(shù)據(jù)。
按F5進(jìn)行編譯,調(diào)試結(jié)果如下所示。

可以正確的輸出我們向數(shù)組中輸入的數(shù)據(jù)。
3.6、計(jì)算每行最大數(shù)據(jù)中的 最小的那一個(gè)數(shù)字
計(jì)算每行最大數(shù)據(jù)中的 最小的那一個(gè)數(shù)字代碼如下所示
for (min = a[0][0], row = 0; row < n; row++)
{
for (max = a[row][0], col = 1; col < n; col++) /*從 row 行選出大數(shù) */
{
if (max < a[row][col])
{
max = a[row][col];
}
}
if (row == 0) /* 保存至 row 行的小數(shù) */
{
min = max;
}
else if (min > max)
{
min = max;
}
}
printf("The minimum of maximum number is %d\n", min);
先從每行選出大數(shù)
再從選出 的 n 個(gè)大數(shù)中選出小數(shù);
按F5進(jìn)行編譯,調(diào)試結(jié)果如下所示。
3.6.1 二階數(shù)組調(diào)試的結(jié)果

3.6.2 三階數(shù)組調(diào)試的結(jié)果

3.7、計(jì)算每行最小數(shù)據(jù)中的 最大的那一個(gè)數(shù)字
計(jì)算每行最小數(shù)據(jù)中的 最大的那一個(gè)數(shù)字 代碼如下所示
for (max = a[0][0], row = 0; row < n; row++)
{
for (min = a[row][0], col = 1; col < n; col++) /* 從 row 行選出小數(shù) */
{
if (min > a[row][col])
{
min = a[row][col];
}
}
if (row == 0) /*保存至 row 行的大數(shù) */
{
max = min;
}
else if (max < min)
{
max = min;
}
}
printf("\nThe maximum of minimum numbers is %d\n", max);
先從每行 選出小數(shù),
再從選出的 n 個(gè)小數(shù)中選出大數(shù)。
按F5進(jìn)行編譯,調(diào)試結(jié)果如下所示。
3.7.1 二階數(shù)組調(diào)試的結(jié)果

3.7.2 三階數(shù)組調(diào)試的結(jié)果

四、經(jīng)典算法實(shí)例程序
完整代碼
經(jīng)典算法實(shí)例程序完整代碼如下所示
4.1、main.h文件
#pragma once #include <stdio.h> #include <stdlib.h>
4.2、main.c文件
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
#define MAXN 20
int a[MAXN][MAXN];
int main()
{
system("color 3E");
int min, max;
int row, col, n;
printf("Please input the order of the matrix:");/* 輸入方陣的階次 */
scanf("%d", &n);
printf("\nPlease input the elements of the matrix,\nfrom a[0][0] to a[%d][%d]:\n", n - 1, n - 1);
for (row = 0; row < n; row++)
{
for (col = 0; col < n; col++)
{
scanf("%d", &a[row][col]);
}
}
printf("\nThe original matrix is\n");
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
printf("%d ", a[row][col]);
}
printf("\n");
}
printf("\n");
for (min = a[0][0], row = 0; row < n; row++)
{
for (max = a[row][0], col = 1; col < n; col++) /*從 row 行選出大數(shù) */
{
if (max < a[row][col])
{
max = a[row][col];
}
}
if (row == 0) /* 保存至 row 行的小數(shù) */
{
min = max;
}
else if (min > max)
{
min = max;
}
}
printf("The minimum of maximum number is %d\n", min);
for (max = a[0][0], row = 0; row < n; row++)
{
for (min = a[row][0], col = 1; col < n; col++) /* 從 row 行選出小數(shù) */
{
if (min > a[row][col])
{
min = a[row][col];
}
}
if (row == 0) /*保存至 row 行的大數(shù) */
{
max = min;
}
else if (max < min)
{
max = min;
}
}
printf("\nThe maximum of minimum numbers is %d\n", max);
system("pause");
return 0;
}
五、總結(jié)
本文的C語言經(jīng)典算法實(shí)例:求二維數(shù)組最大最小值,要實(shí)現(xiàn)的目標(biāo)如下
- 在n 行 n 列的二維整數(shù)數(shù)組中,按以下要求選出兩個(gè)數(shù)。
- 首先從每行選出大數(shù),再從選出 的 n 個(gè)大數(shù)中選出小數(shù);
- 其次,從每行 選出小數(shù),再從選出的 n 個(gè)小數(shù)中選出大數(shù)。
到此這篇關(guān)于C語言算法練習(xí)之求二維數(shù)組最值問題的文章就介紹到這了,更多相關(guān)C語言求數(shù)組最值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言連接并操作Sedna XML數(shù)據(jù)庫的方法
這篇文章主要介紹了C語言連接并操作Sedna XML數(shù)據(jù)庫的方法,實(shí)例分析了C語言操作XML文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06
c++入門必學(xué)算法之快速冪思想及實(shí)現(xiàn)
快速冪相較于普通的冪,具有占用空間少,效率更高等優(yōu)點(diǎn),全面碾壓普通的冪,下面這篇文章主要給大家介紹了關(guān)于c++入門必學(xué)算法之快速冪思想及實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-11-11
詳解C語言內(nèi)核中的鏈表與結(jié)構(gòu)體
Windows內(nèi)核中是無法使用vector容器等數(shù)據(jù)結(jié)構(gòu)的,當(dāng)我們需要保存一個(gè)結(jié)構(gòu)體數(shù)組時(shí),就需要使用內(nèi)核中提供的專用鏈表結(jié)構(gòu)。本文分享了幾個(gè)內(nèi)核中使用鏈表存儲(chǔ)多個(gè)結(jié)構(gòu)體的通用案例,希望對(duì)你有所幫助2022-09-09
C語言實(shí)現(xiàn)數(shù)組的循環(huán)左移,右移,翻轉(zhuǎn)的示例
今天小編就為大家分享一篇C語言實(shí)現(xiàn)數(shù)組的循環(huán)左移,右移,翻轉(zhuǎn)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
C++多態(tài)的實(shí)現(xiàn)機(jī)制深入理解
這篇文章主要介紹了C++多態(tài)的實(shí)現(xiàn)機(jī)制理解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

