c# 数组 循环结果
数组
为什么要使用数组?
举个例子:斐波纳西数列
1,2,3,5,8,13,21,,,,
//产生斐波纳西数列
const int N = 50;//可以用常量来存储要产生斐波纳西数列的个数,要修改个数时直接在这里修改即可
//int[] a = new int[N];//int类型的存储范围扛不住50个斐波纳西数列
long[] a = new long[N];
a[0] = 1;//数组下标从0开始
a[1] = 2;
////////////使用while循环/////////
Console.WriteLine("while循环:");
int i = 2;
while(i < N)
{
a[i] = a[i - 1] + a[i - 2];
i++;
}
//输出产生的斐波纳西数列
i = 0;
while(i < N)
{
Console.WriteLine(a[i]);
i++;
}
////////////使用for循环 for循环是数组标配/////////
Console.WriteLine("使用for循环");
a = new long[N];
a[0] = 1;
a[1] = 2;
for(int j = 2; j < N; j++)
{
a[j] = a[j - 1] + a[j - 2];
}
for (int j = 0; j < N; j++)
{
Console.WriteLine(a[j]);
}
还有一个foreach(long x in a)循环,会遍历整个a数组,x就是a中的元素,这种循环比较适合集合使用。
数组一般使用for循环,因为foreach不能控制数组下标范围。
生成一些随机数
使用Random()类
//生成一些随机数
const int N = 50;
long[] a = new long[N];
var r = new Random();
for(int i = 0; i < N; i++)
{
a[i] = r.Next() % 10;//Next()会产生非负数的int范围内的随机数,%10取余可得到0——9
}
foreach(int x in a)
{
Console.WriteLine(x);
}
Random() 的 next()还可以设置取值范围的。如
next(0, 10);//0-9随机数
next(50);// 0—49随机数
获取数组中前两个最大的数
方法1:
//生成一些随机数,输出最大两个数
const int N = 50;
long[] a = new long[N];
var r = new Random();
for(int i = 0; i < N; i++)
{
a[i] = r.Next(20);//Next()会产生非负数的int范围内的随机数,%10取余可得到0——9
}
long max1 = 0;//设置为最小数0
long max2 = 0;
foreach(long x in a)
{
Console.WriteLine(x);
if(x >= max1)
{
max1 = x;
}else if(x > max2)
{
max2 = x;
}
}
Console.WriteLine("max1:{0}, max2:{1}", max1, max2);
方法二:排序后输出
//生成一些随机数,输出最大两个数
const int N = 10;
long[] a = new long[N];
var r = new Random();
for(int i = 0; i < N; i++)
{
a[i] = r.Next(10);//Next()会产生非负数的int范围内的随机数,%10取余可得到0——9
Console.WriteLine(a[i]);
}
//对数组a进行排序
Console.WriteLine("排序之后:");
for(int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (a[i] < a[j])
{
//交换
var t = a[i];
a[i] = a[j];
a[j] = t;
}
}
Console.WriteLine(a[i]);
}
Console.WriteLine("max1:{0}, max2:{1}", a[0], a[1]);
}
求 1/2 + 2/3 + 。。。。+ 98/99 + 99/100
//解决一个数学问题:1/2 + 2/3 + 。。。。。+ 99/100
/////////使用数组///////
double[] a = new double[100];
for(int i = 0; i < 100; i++)
{
a[i] = i + 1;
}
double sum = 0;
for(int i = 1; i < 100; i++)
{
sum += a[i - 1] / a[i];
}
Console.WriteLine("使用数组:" + sum);
////////不使用数组//////
sum = 0;
for(int i = 1; i < 100; i++)
{
sum += (double)i / (i + 1);
}
Console.WriteLine("不使用数组:" + sum);
C#预处理命令
形成代码块,可展开收起。
#region 代码块名称
//代码
#regioned
list集合
使用起来比数组方便,提供排序、求最值、平均值等方法。
using System;
using System.Collections.Generic;
using System.Diagnostics;//使用计时器
using System.Linq;
using System.Text;
namespace basicPractice
{
class Program
{
static void Main(string[] args)
{
//list的使用
List<int> nums = new List<int>();
var r = new Random();
for(int i = 1; i <= 10; i++)
{
nums.Add(r.Next(10));
}
foreach(int num in nums)
{
Console.WriteLine(num);
}
Console.WriteLine(nums.Max());
Console.WriteLine(nums.Min());
Console.WriteLine(nums.Average());
nums.Sort();
Console.WriteLine("排序后:");
foreach(int num in nums)
{
Console.WriteLine(num);
}
}
}
}
在循环中 break 和 continue的使用
break是跳出循环,执行循环后面的内容;
continue是跳过本次循环中continue后面的内容,执行下一次循环。