數(shù)組習(xí)題(大小寫轉(zhuǎn)換)

1、輸入一個5行5列的二維數(shù)組,編程實現(xiàn):

(1)求出其中的最大值和最小值及其對應(yīng)的行列位置.

(2)求出對角線上各元素之和.

數(shù)組例如 ? : ? ?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++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//循環(huán)輸入

? ? ? ? ? ? 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++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? //循環(huán)輸入

? ? ? ? ? ? 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"}這個字符串數(shù)組中,字母a出現(xiàn)的次數(shù)

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. 有一行英文語句,統(tǒng)計其中的單詞個數(shù)(規(guī)定:單詞之間以空格分隔),并將每一個單詞的第一個字母改為大寫

string str = "hello,my name is Yourfather";

// string[ ] strArr = str.Split (' ');? ? ? ? ? ? //用空格分割開

// string new_str = str.Trim();? ? ? ? ? ? ? ? ? //把字符串頭尾空格去掉


int number = 0;

char[ ] charArr = str.ToCharArray ();? ? ? ? ? // 生成一個新的數(shù)組? 字符串轉(zhuǎn)為 字符數(shù)組

for (int i = 0; i < str.Length; i++) {

? ? ? ? ? ?char ch = str.ToCharArray ()[i];? ? ? ? ? ? //把str下每個數(shù)單獨賦值給ch

? ? ? ? ? ? if (i == 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //定義第一個字母

? ? ? ? ? ? ? ? ? if (ch >= 97 && ch <= 122) {? ? ? ? ? ? //如果第一個字母為小寫

? ? ? ? ? ? ? ? ? ? ? ?charArr [i] = (char)(ch - 32); ? ? ? //轉(zhuǎn)化為大寫

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ?}

? ? ? ? ? ? ?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. 求二維數(shù)組{{1,2,3},{4,5,6},{7,8,9}}的鞍點。

(鞍點:在行中最大,在列中最小的元素的位置,二維數(shù)組也可能沒有鞍點)

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++) {? ? ? //確定數(shù)組中最大值坐在行和列

? ? ? ? ? ? ? ? ? ? ? ? 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++) { ? ? ? ? ? ? ? ? ?//確定最大值所在列數(shù)

? ? ? ? ? ? ? ? ? ? ? ? ? ?if (max>arr[k,max_j]) {? ? ? ? ? //判斷列數(shù)中最大值 ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isSaddlePoint = false;

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ?if (isSaddlePoint) {

? ? ? ? ? ? ? ? ? ? ?Console.WriteLine ("有鞍點:{0},{1}",max_i,max_j);

? ? ? ? ? ? ? ?}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容