1、輸入一個5行5列的二維數組,編程實現:
(1)求出其中的最大值和最小值及其對應的行列位置.
(2)求出對角線上各元素之和.
數組例如 ? : ? ?int[,] arr = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25} };
const int m = 5;
const int n = 5;
int[,] arr = new int[5, 5];
for (int i = 0; i < m; i++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//循環輸入
? ? ? ? ? ? for (int j = 0; j < n; j++) {
? ? ? ? ? ? ? ? ? ? ? ?arr[i,j]=int.Parse(Console.ReadLine ());
? ? ? ? ? ? ?}
}
int max = arr [0, 0], max_i = 0, max_j = 0;
int min = arr [0, 0], min_i = 0, min_j = 0;
int sum = 0;
for (int i = 0; i < m; i++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? //循環輸入
? ? ? ? ? ? for (int j = 0; j < n; j++) {
? ? ? ? ? ? ? ? ? ? ? ? if (max<arr[i,j]) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? max = arr[i,j];
? ? ? ? ? ? ? ? ? ? ? ? ? ? max_i = i;
? ? ? ? ? ? ? ? ? ? ? ? ? ? max_j = j;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?if (min>arr[i,j]) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?min = arr[i,j];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?min_i = i;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?min_j = j;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? if (i ==j) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum += arr [i, j];
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
}
Console.WriteLine ("max={0},x={1},y={2}",max,max_i,max_j);
Console.WriteLine ("min={0},x={1},y={2}",min,min_i,min_j);
Console.WriteLine ("sum={0}",sum);
2. 求{"a","ba","bc","bad","abcde"}這個字符串數組中,字母a出現的次數
string[ ]? arr = new string[ ]{"a","ba","bc","bad","abcd"};
string[ ]? strArr = {"a","ba","bc","bad","abcd"};
int number = 0;
foreach (string str in strArr) {
? ? ? ? ? ? ? ?char[ ] chars = str.ToCharArray ();
? ? ? ? ? ? ? ? foreach(char ch in chars){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (ch == 'a') {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ++number;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
}
Console.WriteLine ("number={0}",number);
3. 有一行英文語句,統計其中的單詞個數(規定:單詞之間以空格分隔),并將每一個單詞的第一個字母改為大寫
string str = "hello,my name is Yourfather";
// string[ ] strArr = str.Split (' ');? ? ? ? ? ? //用空格分割開
// string new_str = str.Trim();? ? ? ? ? ? ? ? ? //把字符串頭尾空格去掉
int number = 0;
char[ ] charArr = str.ToCharArray ();? ? ? ? ? // 生成一個新的數組? 字符串轉為 字符數組
for (int i = 0; i < str.Length; i++) {
? ? ? ? ? ?char ch = str.ToCharArray ()[i];? ? ? ? ? ? //把str下每個數單獨賦值給ch
? ? ? ? ? ? if (i == 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //定義第一個字母
? ? ? ? ? ? ? ? ? if (ch >= 97 && ch <= 122) {? ? ? ? ? ? //如果第一個字母為小寫
? ? ? ? ? ? ? ? ? ? ? ?charArr [i] = (char)(ch - 32); ? ? ? //轉化為大寫
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ?}
? ? ? ? ? ? ?if (ch == ' ') {
? ? ? ? ? ? ? ? ? ? ? number++;
? ? ? ? ? ? ? ? ? ? ? char next_ch = str.ToCharArray() [i + 1]; ? ? //空格后一位
? ? ? ? ? ? ? ? ? ? ? if (next_ch>=97 && next_ch<=122) {? ? ? ? ? //檢查是否為小寫字母
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?charArr [i + 1] = (char)(next_ch - 32);
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }
}
// string new_str = new string (charArr);
string new_str = "";
foreach (char ch in charArr) {
? ? ? ? ? ? ? ?new_str +=ch.ToString();
? }
Console.WriteLine(new_str);
4. 求二維數組{{1,2,3},{4,5,6},{7,8,9}}的鞍點。
(鞍點:在行中最大,在列中最小的元素的位置,二維數組也可能沒有鞍點)
1 ? 2 ? ?3
4 ? ?5 ? 6
7 ? ?8 ? 9
int[,] arr = {{1,2,3},{4,5,6},{7,8,9}};
int max_i = 0, max_j = 0;
for (int i = 0; i < 3; i++) {
? ? ? ? ? ? int max = 0;
? ? ? ? ? ? for (int j = 0; j < 3; j++) {? ? ? //確定數組中最大值坐在行和列
? ? ? ? ? ? ? ? ? ? ? ? if (max < arr[i,j]) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?max = arr[i,j];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?max_i = i;? ? ? ? ? ? ? ? ? //2 ? ? ? ? ? ? ? 定義最大值的下標
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?max_j = j;? ? ? ? ? ? ? ? ? //2
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? ? bool isSaddlePoint = true; ? ? ? ? ? ? ? ?//定義鞍點值為真
? ? ? ? ? ? ? for (int k = 0; k < 3; k++) { ? ? ? ? ? ? ? ? ?//確定最大值所在列數
? ? ? ? ? ? ? ? ? ? ? ? ? ?if (max>arr[k,max_j]) {? ? ? ? ? //判斷列數中最大值 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isSaddlePoint = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?if (isSaddlePoint) {
? ? ? ? ? ? ? ? ? ? ?Console.WriteLine ("有鞍點:{0},{1}",max_i,max_j);
? ? ? ? ? ? ? ?}
}