zoukankan      html  css  js  c++  java
  • c#程序员机试题

    一、题目:

      有一数组: 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

  • 相关阅读:
    Educational Codeforces Round 14
    2016 Multi-University Training Contest 4
    2016 Multi-University Training Contest 2
    Codeforces
    BZOJ1776
    Codeforces Round #261 (Div. 2)
    String Painter, Chengdu 2008, LA4394
    Codeforces Round #239 (Div. 1)C, 407C
    python语句和语法
    Python快速入门——容易忽略的技巧
  • 原文地址:https://www.cnblogs.com/rushme/p/7657278.html
Copyright © 2011-2022 走看看