数组,一维,二维,多维

数组

一维数组

数组初始化,创建数组,数组长度为5

int [] array=new int[5];

array[0] = 1;

array[1] = 2;

array[2] = 3;

array[3] = 4;

array[4] = 5;

直接赋值的数组

int[] array1 = new int[] {1,2,3,4,5 };

数组,在创建的时候,需要规定数据类型,初始长度

二维数组

int [,] shuzu = new int[3,4];

3     表示有3个一维数组

4     表示每一个一维数组有4个元素

给第一个一维数组的2号索引赋值

shuzu[0, 2] = 3;

直接赋值

int[,] shuzu1 = new int[,] {

{1,2,3,4},

{2,3,4,5},

{3,4,5,6}

};

冒泡排序

for (int i = 0; i < 10;i++ )

{

for (int j =i; j < 9;j++ )

{

if(shuzi[i]<shuzi[j+1])

{

int zhong = shuzi[i];

shuzi[i] = shuzi[j + 1];

shuzi[j + 1] = zhong;

}

}

练习:

输入人数,输入每个人年龄,求总和

// Console.Write("请输入班级人数:");

// int n = int.Parse(Console.ReadLine());

// int [] ban=new int[n];

//int sum=0;

// for (int i = 0; i < n; i++)

// {

//     Console.Write("请输入年龄:");

//    ban [i]=int.Parse(Console.ReadLine());

//    sum += ban[i];

// }

// Console.WriteLine("年龄的总和是"+sum);

// Console.ReadLine();

输入班级人数,存放语数英成绩,求每人总和,语文总分,数学平均分

//Console.Write("请输入班级人数:");

//int n = int.Parse(Console.ReadLine());

//double[,] chengji = new double[n, 3];

//double a = 0;

//double yz = 0;

//double sp = 0;

//for (int i = 0; i < n; i++)

//{

//    Console.Write("请输入语文成绩:");

//    chengji[i, 0] = double.Parse(Console.ReadLine());

//    Console.Write("请输入数学成绩:");

//    chengji[i, 1] = double.Parse(Console.ReadLine());

//    Console.Write("请输入英语成绩:");

//    chengji[i, 2] = double.Parse(Console.ReadLine());

//}

//for (int j = 0; j < n; j++)

//{

//    a = chengji[j, 0] + chengji[j, 1] + chengji[j, 2];

//    Console.WriteLine("第{0}个人的三门成绩总和是{1}", (j + 1), a);

//}

//for (int k = 0; k < n; k++)

//{

//    yz += chengji[k, 0];

//}

//Console.WriteLine("语文成绩的总和是:" + yz);

//for (int p = 0; p < n; p++)

//{

//    sp += chengji[p, 1];

//}

//Console.WriteLine("数学平均分成绩的是:" + (sp / n));

//Console.ReadLine();

打印柴,利用二维数组

//string[,] mingzi = new string[16, 10]

//{

//{"  ","  ","  ","●","  ","  ","","●","",""},

//{"  ","  ","  ","●","  ","  ","●","  ","  ",""},

//{"  ","●","  ","●","  ","  ","●","  ","  ","●"},

//{"  ","●","  ","●","  ","  ","●","  ","●",""},

//{"  ","●","  ","●","●","●","●","●","",""},

//{"  ","●","  ","●","  ","  ","●","","",""},

//{"  ","●","  ","●","  ","  ","●","","","●"},

//{"●","●","●","●","●","  ","●","●","●",""},

//{"  ","  ","  ","  ","●","","","","",""},

//{"  ","  ","  ","  ","●","","","","",""},

//{"  ","●","●","●","●","●","●","●","●",""},

//{"  ","  ","  ","●","●","●","","","",""},

//{"  ","  ","●","  ","●","  ","●","","",""},

//{"  ","●","  ","  ","●","  ","  ","●","",""},

//{"●","  ","  ","  ","●","  ","  ","  ","●",""},

//{"  ","  ","  ","  ","●","  ","  ","  ","  ",""},

//};

//for(int i=0;i<16;i++)

//{

//for(int b=0;b<10;b++)

//{

//Console.Write(mingzi[i,b]);

//}

//Console.WriteLine();

//}

//Console.ReadLine();

随机一百以内10个数,按照大小顺序排列

int[] shuz = new int[10];

Random shu = new Random();

for (int i = 0; i < 10; i++)

{

int a = shu.Next(101);

shuz[i] = a;

Console.WriteLine(shuz[i]);

}

for (int j = 0; j < 10; j++)

{

for (int k = j; k < 9; k++)

{

if (shuz[j] > shuz[k + 1])

{

int zhong = shuz[j];

shuz[j] = shuz[k + 1];

shuz[k + 1] = zhong;

}

}

}

for (int i = 0; i < 10; i++)

{

Console.WriteLine(shuz[i]);

}

Console.ReadLine();

随机一百以内十个不重复的数,按照大小顺序排列。

// int[] shuz = new int[10];

//Random shu=new Random();

// for (int i = 0; i < 10; i++)

// {

//    bool b=true;

//     int a = shu.Next(101);

//     shuz[i] = a;

//     for (int j = 0; j < i; j++)

//     {

//         if (a == shuz[j])

//         {

//             b = false;

//             i--;

//         }

//         if (b)

//         {

//             shuz[i] = a;

//         }

//     }

//     Console.WriteLine(shuz[i]);

// }

// for (int j = 0; j < 10; j++)

// {

//     for (int k = j; k < 9; k++)

//     {

//         if (shuz[j] > shuz[k + 1])

//         {

//             int zhong = shuz[j];

//             shuz[j] = shuz[k + 1];

//             shuz[k + 1] = zhong;

//         }

//     }

// }

// for (int i = 0; i < 10; i++)

// {

//     Console.WriteLine(shuz[i]);

// }

// Console.ReadLine();

//Random ran = new Random();

//输入每个人的手机号,,进行滚动显示,,清屏,随即一个索引,根据索引打印,等待0.1秒

// Console.Write("请输入人数:");

// int a =int.Parse( Console.ReadLine());

// string[]zhj=new string[a];

// for (int i = 0; i < a;i++ )

// {

//     Console.Write("请输入第{0}个人的手机号:",(i+1));

//     zhj[i] = Console.ReadLine();

// }

// Console.WriteLine("所有号码输入完毕,请按回车继续抽奖活动!");

//Console.ReadLine();

// Random ran=new Random();

// for (int j = 0; j < 30;j++ )

// {

//     Console.Clear();

//     int aa = ran.Next(a);

//     Console.WriteLine("中奖号码是:"+zhj[aa]);

//     System.Threading.Thread.Sleep(100);

// }

// Console.ReadLine();

时间: 2024-10-14 13:43:46

数组,一维,二维,多维的相关文章

c++ 依据输入动态声明数组(一维,二维)

较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #include <stdio.h> #include <stdlib.h> int main() { int a; int i; scanf("%d",&a); int c[a]; for( i =0 ;i<a;i++) scanf("%d",

【树状数组】树状数组一维二维模板

以下模板都是点更新,区间查询,如果是区间更新点查询,只需将利用lowbit的循环方向倒过来 一维: inline int lowbit(int x) { return x & -x; } void add(int x, int val) { for(int i = x; i <= n; i += lowbit(i)) C[i] += val; } int sum(int x) { int ret = 0; for(int i = x; i > 0; i -= lowbit(i)) re

java数组(一维数组,二维数组)

数组初始化: 1,动态初始化:数组定义与为数组分配空间和赋值的操作分开进行 2,静态初始化:在定义数组的同时就为数组元素分配空间并赋值 3,默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此,数组分配空间之后 每个元素也被按照成员的规则被隐士的初始化值 ------------------------------------------------. 一维数组: package day06; import java.util.Date; /*** * 数组演示 * 数组的声明: * t

C#一维数组,二维数组,多维数组

所谓的数组,就是相同数据类型的元素的元素按一定顺数排列的集合,就是把有限个类型相同的变量用一名字命名,然后用编号区分他们的变量的集合,这个名字成为数组名,编号称为下标.组成数组的各个变量成为数组的分量,也称为数组的元素,有时也称为下标变量,数组是在程序设计中,为了处理方便,把具有相同类型的若干变量按有序的形式组织起来的一种形式.这些按序排列的同类数据元素的集合成为数组. 数组又分为一维数组,二维数组,多维数组 一维数组用一个整数来索引,多维数组用两个或多个整数来索引. 一.数据类型[] 变量名

16.10.20 4th 1蛤蟆爬井 2一维数组 3二维数组

摘要 1蛤蟆爬井 2一维数组 3二维数组 例子 井深10米, 蛤蟆白天爬5m,晚上落4米,求几天爬出来. //思路,用循环语句来做,for因为是未知次数所以排除,while 先判断后运行排除, dowhile,先爬在判断所以可以 int gaodu = 0; int tianshu = 0; boolean tianse = true; do{ if(tainse){ //白天爬5米 gaodu+=5; //爬完后改黑天 tianse = false; //天数+1 tianshu +=1; }

一维数组,二维数组,三维数组,数组与指针,结构体数组,通过改变指针类型改变访问数组的方式

 打印数组中的每个元素,打印每个元素的地址: #include <stdio.h> #include <stdlib.h> void main(void) { int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int *p = a; p < a + 10;p++)  //指针类型决定4个字节 { printf("\n%p,%d", p, *p); } getchar(); } 指针数组 #inclu

数组(一维数组、二维数组)

数组概述 C# 数组从零开始建立索引,即数组索引从零开始.C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似.但还有一些差异应引起注意. 这些其实和泛型有些类似,数组的操作没有泛型方便,但是性能却不是泛型所能比拟的 声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面.在 C# 中,将方括号放在标识符后是不合法的语法. int[] table; // not int table[]; 另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分.这使您可

一维动态数组和二维动态数组的创建和使用

#include<stdio.h> #include<malloc.h> void main(){ int *a,n=10,i; /* calloc()函数的原型是:(void *)calloc(unsigned n,unsigned size) calloc()函数用于向系统动态申请n个,每个占sizege字节的内存单元,函数返回值为所申请的内存空间首地址 malloc和calloc主要区别在于,当系统的内存只剩下一些非常小的碎片时,用calloc函数设计的动态数组的时间效率优于

函数、内存、一维数组、二维数组【4】

  函数的定义 什么是函数? •函数就是定义在类中的具有特定功能的一段独立小程序. •函数也称为方法. 函数的格式: •修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,...)   {   执行语句;   return 返回值;   }   返回值类型:函数运行后的结果的数据类型.   参数类型:是形式参数的数据类型.   形式参数:是一个变量,用于存储调用函数时传递给函数的实际参数.   实际参数:传递给形式参数的具体数值.   return:用于结束函数.   返回值