【题】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();
}
相关推荐
暖馒2 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
刘欣的博客5 小时前
C# CS架构程序发版升级的走数据库方案
c#·单文件升级自己的方式
Yorlen_Zhang6 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝1917 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话7 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc11400756638 小时前
通信 28
c#
bugcome_com12 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中12 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
bugcome_com15 小时前
阿里云 OSS C# SDK 使用实践与参数详解
阿里云·c#
懒人咖1 天前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空