首页 > Windows开发 > 详细

c#程序员机试题

时间:2017-10-12 18:46:28      阅读:301      评论:0      收藏:0      [点我收藏+]

一、题目:

  有一数组: int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32};

      1、求出最大值

      2、按每个数字的10位数分组(说明:0~9的位数为0,10~19的位数为1),求出每组的最小值,用Dictionary<int,int> 表示返回结果,返回结果按10位数正序排序。

 

    参考答案如下:

 1                Dictionary<int, int> SaveMinValue = new Dictionary<int, int>();
 2  2             int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32};
 3  3             foreach (var item in arr)
 4  4             {
 5  5                 if (!SaveMinValue.ContainsKey(item / 10))
 6  6                 {
 7  7                     SaveMinValue.Add(item / 10, item);
 8  8                 }
 9  9                 else 
10 10                 {
11 11                     if (item<SaveMinValue[item / 10])
12 12                     {
13 13                         SaveMinValue[item / 10] = item;
14 14                     }
15 15                 }
16 16             }
17                //用linq进行排序
18 17             var dicSort = from objDic in SaveMinValue orderby objDic.Value  select objDic;  
19 18             foreach (KeyValuePair<int,int> key in SaveMinValue)
20 19             {
21 20                 Response.Write("" + key.Key + "最小值为:" + key.Value+"<br>");
22 21             }
23          //最大值
24 22             Response.Write("最大值:" + arr.Max());        

  输出结果:

第0组最小值为:1
第1组最小值为:12
第2组最小值为:26
第3组最小值为:32
第4组最小值为:41
第5组最小值为:55


最大值:56

 

c#程序员机试题

原文:http://www.cnblogs.com/rushme/p/7657278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!