• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gooliugle
博客园    首页    新随笔    联系   管理    订阅  订阅
数据结构与算法读书笔记4----C# 查找数组中指定数字,最小值,最大值。

C# 查找数组中指定数字,最小值,最大值。

代码
 1   class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int searchNumber;
 6             bool found;
 7             //TestArray nums = new TestArray(10);
 8             int[] nums = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 9             #region  初始化数组
10             //Random rnd = new Random(100);
11             //for (int num = 0; num < 10; num++)
12             //{
13             //    nums.Insert(rnd.Next(0, 100));
14             //}
15             #endregion
16             Console.WriteLine("Enter a num to search for:");
17             searchNumber = Convert.ToInt32(Console.ReadLine());
18             found = NumSearch(nums,searchNumber);
19             if (found)
20             {
21                 Console.WriteLine(searchNumber + "is in the array.");
22             }
23             else
24             {
25                 Console.WriteLine(searchNumber + "is not in the array.");
26             }
27             //查找数组中最小值函数
28             int min=FindMin(nums);
29             int max = FindMax(nums);
30             Console.WriteLine(min.ToString());
31             Console.WriteLine(max.ToString());
32             #region  排序实现
33             //Console.WriteLine("Before Sorting: ");
34             //nums.DisplayElements();
35             //Console.WriteLine("Durring Sorting: ");
36             //nums.InsertionSort();
37             //Console.WriteLine("After Sorting: ");
38             //nums.DisplayElements();
39             #endregion
40             Console.ReadLine();
41         }
42         //<summary>
43         //查找数组中制定数字的方法
44         //<param name="arr">数组</param>
45         //<param name="value">数字</param>
46         //</summary>
47         //<returns>bool</returns>
48         public static bool NumSearch(int [] arr,int value)
49         {
50             for (int index = 0; index < arr.Length; index++)
51             {
52                 if (arr[index] == value)
53                 {
54                     return true;
55                 }
56             }
57             return false;
58         }
59         //<summary>
60         //查找数组中最小数字的方法
61         //<param name="arr">数组</param>
62         //</summary>
63         //<returns>bool</returns>
64         public static int FindMin(int[] arr)
65         {
66             int min = arr[0];
67             for (int index = 1; index < arr.Length; index++)
68             {
69                 if (arr[index] < min)
70                 {
71                     min=arr[index];
72                 }
73             }
74             return min;
75         }
76         //<summary>
77         //查找数组中最大数字的方法
78         //<param name="arr">数组</param>
79         //</summary>
80         //<returns>bool</returns>
81         public static int FindMax(int[] arr)
82         {
83             int max = arr[0];
84             for (int index = 1; index < arr.Length; index++)
85             {
86                 if (arr[index] > max)
87                 {
88                     max = arr[index];
89                 }
90             }
91             return max;
92         }
93 
94     }


 

posted on 2010-04-23 20:20  gooliugle  阅读(1136)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3