這次實例的要求是:
在n行n列的二維整數數組中,按照以下要求選出兩個數。首先從每行中選出最大數,在從選出的n個最大數中選出最小數;其次,從每行選出最小數,再從選出的n個小數中選出最大數。
下面就是我的代碼,在注釋中可以看到我的想法:
~~~
#include <stdio.h>
/**
* 實例要求:
* 在n行n列的二維整數數組中,
* 按照以下要求選出兩個數。
* 首先從每行中選出最大數,在從選出的n個最大數中選出最小數;
* 其次,從每行選出最小數,再從選出的n個小數中選出最大數。
*
*/
int main(void)
{
int order;
printf("%s\n","Please enter the order of the matrix:");
scanf("%d",&order);
printf("Please input the elements of the matrix,from a[0][0] to a[%d][%d]:\n",order-1,order-1);
int matrix[order][order];
/**
* 獲取用戶輸入,并填充到二維數組中
*/
int colums,rows;
for(rows = 0;rows < order;rows++){
for(colums = 0; colums < order;colums++){
scanf("%d",&matrix[rows][colums]);
//這里也可以這樣寫
//scanf("%d",matrix[rows]+colums);
}
}
/**
* 找到最大元素的最小元素
*
*/
//用于保存最大元素中的最小元素
int minInMax = 0;
for(rows = 0;rows < order;rows++){
//用于保存行最大元素
int maxInLine = 0;
for(colums = 0;colums < order;colums++){
if(matrix[rows][colums] > maxInLine)
maxInLine = matrix[rows][colums];
}
if(rows == 0){
//當獲取到第一行的最大元素時,直接賦值給最小元素
minInMax = maxInLine;
}else{
if(minInMax > maxInLine)
minInMax = maxInLine;
}
}
printf("The minimum of maximum number is %d.\n",minInMax);
/**
* 找到最小元素的最大元素
*
*/
//用于保存最小元素中的最大元素
int maxInMin = 0;
for(rows = 0;rows < order;rows++){
//用于保存行最小元素
int minInLine = matrix[rows][0];
for(colums = 0;colums < order;colums++){
if(matrix[rows][colums] < minInLine)
minInLine = matrix[rows][colums];
}
if(rows == 0){
//當獲取到第一行的最小元素時,直接賦值給最大元素
maxInMin = minInLine;
}else{
if(maxInMin < minInLine)
maxInMin = minInLine;
}
}
printf("The maximum of minimum number is %d.\n",maxInMin);
return 0;
}
~~~
- 前言
- 實例一:HelloWorld
- scanf函數學習
- 實數比較
- sizeof()保留字獲取類型的大小
- 自增/自減學習
- C學習if條件判斷和for循環
- C實現的九九乘法表
- C實現一個比較簡單的猜數游戲
- 使用C模擬ATM練習switch..case用法
- 記錄一個班級的成績練習一維數組
- C數組實現矩陣的轉置
- C二維數組練習
- 利用數組求前n個質數
- C實現萬年歷
- C實現數組中元素的排序
- C實現任意進制數的轉化
- C判斷一個正整數n的d進制數是否是回文數
- C使用遞歸實現前N個元素的和
- 鋼材切割問題
- 使用指針比較整型數據的大小
- 指向數組的指針
- 尋找指定元素
- 尋找相同元素的指針
- 整數轉換成羅馬數字
- 字符替換
- 從鍵盤讀入實數
- C實現字符行排版
- C實現字符排列
- C實例--判斷一個字符串是否是回文數
- 通訊錄的輸入輸出
- 撲克牌的結構定義
- 使用“結構”統計學生成績
- 報數游戲
- 模擬社會關系
- 統計文件中字符個數
- C實現兩個文件的內容輸出到同一個屏幕