【题】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();
}
相关推荐
csdn_aspnet8 小时前
C# (QuickSort using Random Pivoting)使用随机枢轴的快速排序
数据结构·算法·c#·排序算法
游乐码14 小时前
c#lambad表达式
开发语言·c#
一念春风16 小时前
Qwen2.5 (AI模型 PC搭建)
人工智能·ai·c#·wpf·模型
~plus~17 小时前
C# 事件溯源与 CQRS 架构:用 EventStoreDB 打造可靠系统
开发语言·架构·c#
aq553560018 小时前
Laravel7.x重磅升级:十大新特性解析
开发语言·汇编·c#·html
唐青枫20 小时前
C#.NET Mapperly 深入解析:源生成映射、安装使用与工程化取舍
c#·.net
大空大地202620 小时前
Windows打印技术和网络编程技术
c#
xiaoshuaishuai81 天前
C# 实现Workstation相关功能
开发语言·windows·c#
游乐码1 天前
c#Lsit排序
开发语言·c#
hard_coding_wang1 天前
了解一个Excel批量替换的公式用法:REDUCE + LAMBDA 实现循环替换
开发语言·c#·excel