【题】C#-数组:二维数组

1. 将1~10000赋值给一个二维数组(100行100列)
cs 复制代码
int[,] array = new int[100,100];
int index = 1;
for(int i = 0;i < array.GetLength(0);i++){
    for(int j = 0;j < array.GetLength(1);j++){
        array[i,j] = index;
        ++index;
    }
}
2. 将二维数组的右上半部分置零
cs 复制代码
int[,] array = new int[4,4];
Random r = new Random();
for(int i = 0;i < array.GetLength(0);i++){
    for(int j = 0;j< array.GetLength(1);j++){
        if(i <= 1 && j >1) array[i,j] = 0;
        else array[i,j] = r.Next(1,101)
        Console.Write(array[i,j]+" ");
    }
  Console.WriteLine();
}
3. 求二维数组的对角线元素的和
cs 复制代码
int[,] array = new int[3,3];
Random r = new Random();
for(int i = 0;i < array.GetLength(0);i++){
    for(int j = 0;j< array.GetLength(1);j++){
        array[i,j] = r.Next(1,11);
        if(i==j || i+j==2) sum+=array[i,j];
        Console.Write(array[i,j]+" ");
    }
  Console.WriteLine();
}
Console.WriteLine(sum);
4. 求二维数组中最大元素及其行列号
cs 复制代码
int[,] array = new int[5,5];
Random r = new Random();
//记录最大值的行列号
int maxI=0;
int maxJ=0;
for(int i=0;i<array.GetLength(0);i++){
    for(int j=0;j<array.GetLength(1);j++){
        array[i,j] = r.Next(1,501);
        Console.Write(array[i,j]+" ");
        //找最大值
      if(array[maxI,maxJ] < array[i,j]){
          maxI = i;
          maxJ = j;
      }
    }
    Console.WriteLine();
}
5. 在一个M*N的二维数组中,数组元素的值为0或1。转换数组:将含有1的行和列全置为1
cs 复制代码
int[,] array = new int[5,5];
Random r = new Random();
bool[] hang = new bool[5];
bool[] lie = new bool[5];

for(int i = 0;i<array.GetLength(0);i++){
    for(int j = 0;j<array.GetLength(1);j++){
        if(array[i,j] ==1){
            hang[i] = true;
            lie[i] = true;
        }
        Console.Write(array[i,j] + " ");
    }
    Console.WriteLine();
}
for(int i = 0;i < array.GetLength(0);i++){
    for(int j = 0;j<array.GetLength(1);j++){
        if(hang[i] || lie[i]
           array[i,j] = 1;
         Console.Write(array[i,j]+" ");
    }
    Console.WriteLine();
}
相关推荐
玖玥拾9 小时前
C# 语言进阶(十五)C# 游戏服务端 MySQL 数据库
服务器·开发语言·网络·数据库·mysql·c#
逝水无殇10 小时前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#
唐青枫15 小时前
看懂 IL 的关键:C#.NET 栈机执行模型实战拆解
c#·.net
我才是银古15 小时前
从零构建 Windows 桌面 RPA 框架:Go 与 .NET 的跨语言协奏
c#·go·rpa
逝水无殇17 小时前
C# 正则表达式详解
开发语言·后端·正则表达式·c#
朱永博17 小时前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
吴可可12319 小时前
C# CAD二次开发添加带按钮的窗口
c#
铅笔侠_小龙虾20 小时前
Rust 学习(8)-切片类型
学习·rust·c#
习明然1 天前
我的本地化AI项目(三)
人工智能·python·electron·c#·avalonia
范小多2 天前
C#获取和风天气数据
java·开发语言·python·c#