学习记录
10.输出一年每个月份的天数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
int[] month = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int i = 0; i < 12; i++)
{ Console.WriteLine("{0}月有D{1}天", i + 1, month[i]); }
Console.Read();
}
}
}
11.输出100个1-99之间的随机整数(练习Random函数的使用)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[100];
Random n = new Random();
for (int i = 0; i < 100; i++)
{
nums[i] = n.Next(99);
Console.WriteLine("第{0}个1-99之间的随机整数{1}",i+1, nums[i]);
}
Console.Read();
}
}
}
12.输入一个数,判断其是否出现在程序11所产生的随机整数中(程序12建立在11的基础上)
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" 输入一个数:");
int s = Convert.ToInt32(Console.ReadLine());
int[] nums = new int[100];
Random n = new Random(); int i = 0;
for (; i < 99; i++)
{
nums[i] = n.Next(99);
}
if (s == nums[i])
Console.WriteLine(i);
else
Console.WriteLine(-1);
Console.Read();
}
}
}
13.判断生成的100个随机数之中的最大值和最小值(程序11的引申程序)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[100];
int max,min;
max=min=nums[0];
Random n = new Random();
for (int i = 0; i < 100; i++)
{
nums[i] = n.Next(99);
if (nums[i] > max) max = nums[i];
if (nums[i] < min) min = nums[i];
}
Console.WriteLine("该数组最大值是{0},最小值是{1}", max, min);
Console.Read();
}
}
}
15.求两个二维数组之和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int i, j;
int[,] a = new int[5, 6] { { 1, 2, 3, 4, 5, 6 }, { 4, 5, 6,1, 2, 3 }, { 1, 4, 5, 6,2, 3 }, { 3, 4,1, 2, 5, 6 }, { 1, 4, 5, 2, 3, 6 } };
int[,] b = new int[5, 6] { { 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1 } };
int[,] c = new int[5, 6];
for (i = 0; i < c.GetLength(0); i++)
for (j = 0; j < c.GetLength(1); j++)
c[i, j] = a[i, j] + b[i, j];
for(i=0;i<5;++i)
{
for (j = 0; j < 6; ++j)
Console.Write("{0,3:d}", c[i, j]);
Console.WriteLine();
}
Console.Read();
}
}
}