C#便利数组&&foreach循环&&杨辉三角

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sj1013
{
    class Program
    {
        public static void Main(string[]args)
        {
            string str1= "星期一\\星期二\\星期三";

            string str2=@"星期一\\星期二\\星期三";

           Console.WriteLine(str1);

           Console.WriteLine(str2);
        }
    }
}

结果为:

星期一\星期二\星期三
星期一\\星期二\\星期三
请按任意键继续. . .

 

 

 

 

二维数组是按照你定义的类型的一组数,比如 
int [2,3]那就是说一个两行三列,每一个元素都是一个整型数的数组,但是交错数组int[2][],意思是这个数组有两个元素,每一个元素都是一个整型的数组,但是长度可以不一样,
例一:
int [][] arr= new int[2][]; 
int [0][]=new int[10]; 
int [1][]=new int[8];
例二:
int[][] c1 = new int[3][];
 c1[0] = new int[3];
 c1[1] = new int[2];
 c1[2] = new int[1];

 如何遍历交错数组?
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sj1013
{
    class Program
    {
        public static void Main(string[] args)
        {
            //交错数组
            int[][] CrossArray = new int[3][] { new int[] { 1, 2 }, new int[] { 3, 4, 5 }, new int[] { 6, 7, 8, 9 } };
            //遍历交错数组
            foreach (int[] c in CrossArray)//在这里定义了一个int[] c是CrossArray的第一维,
            {
                foreach (int i in c)//定义了一个int i 在c中而c是CrossArray的第一维,就是i说能够遍历CrossArray的第二维,
                {
                    Console.Write(i.ToString() + ",");
                }
                Console.WriteLine();
            }
        }
    }
}
结果为:
1,2,
3,4,5,
6,7,8,9,
请按任意键继续. . .

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sj1013
{
    class Program
    {
        public static void Main(string[]args)
        {
            int[] a =new int[100];
            int b = 1;
            for(int i =0; i < 100; i++)
            {
               a[i] = b++;
            }
            int sum = 0;
            foreach(inti in a)
            {
               sum += i;
            }
           Console.WriteLine(sum);
        }
    }
}

 

 

 

交错数组打印杨辉三角

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace sj1016

{

    classProgram

    {

        staticvoid Main(string[] args)

        {

            int i, j, k;

 

            Console.WriteLine("请输入你要打印的行数:");

 

            k = int.Parse(Console.ReadLine());

 

            int[][] Y = newint[k][];

 

            for (i = 0; i < Y.Length; i++)

 

            {

 

                Y[i] = newint[i + 1];

 

                Y[i][0] = 1;

 

                Y[i][i] = 1;

 

            }

 

            for (i = 2; i < Y.Length; i++)

 

                for (j = 1; j < Y[i].Length - 1; j++)

 

                    Y[i][j] = Y[i - 1][j - 1] +Y[i - 1][j];

 

            for (i = 0; i < Y.Length; i++)

 

            {

 

                for (j = 0; j < Y[i].Length; j++)

 

                {

 

                    Console.Write("{0,5:d}", Y[i][j]);

 

                }

 

                Console.WriteLine();

 

                Console.Read();

 

            }

 

        }

 

    }

}

 

 

 

 

例二:打印杨辉三角

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace sj1016

{

    classProgram

    {

        staticvoid Main(string[] args)

        {

                //打印杨辉三角 

                int i, j, k, m;

                k = 7;

                int[][] Y = newint[k][];//定义二维锯齿状数组 

                for (i = 0; i < Y.Length; i++)

                {

 

                    Y[i] = newint[i + 1];

                    Y[i][0] = 1;

                    Y[i][i] = 1;

 

                }

                for (i = 2; i < Y.Length; i++)

                {

                    for (j = 1; j < Y[i].Length - 1; j++)

                    {

                        Y[i][j] = Y[i - 1][j -1] + Y[i - 1][j];

 

 

                    }

                }

 

                for (i = 0; i < Y.Length; i++)

                {

                    for (j = 0; j < Y[i].Length; j++)

                    {

                        Console.Write("{0,5:d}", Y[i][j]);

                    }

                    Console.WriteLine();

                }

                Console.Read();

            }

        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值