zoukankan      html  css  js  c++  java
  • 任意给定一整数数组,求两个元素之差的最大值和数组中出现次数最多的数

    要求:不允许使用库函数及外部库,不允许使用现成的排序算法和哈希算法函数

    1、两个元素之差的最大值

         private static void GetMaxMin(int[] arrys)

            {

                //int[] arrys = new int[] { 3, 5, 1, 6, 9, 8, 4, 7, 2 };

                int max = arrys[0];

                int min = arrys[0];

                for (int i = 0; i < arrys.Length; i++)

                {

                    if (arrys[i] > max)

                        max = arrys[i];

                    if (arrys[i] < min)

                        min = arrys[i];

                }

                Console.WriteLine("Max:{0}Min{1},差为{2}", max, min, max - min);

            }

            //2、数组中出现次数最多的数

            /// <summary>

            /// 得到去重复的数组

            /// </summary>

            /// <param name="arrys"></param>

            /// <returns></returns>

            private static int[] GetNewArry(int[] arrys)

            {

                for (int i = 0; i < arrys.Length; i++)

                {

                    for (int j = i + 1; j < arrys.Length; j++)

                    {

                        if (arrys[i] == arrys[j])

                            arrys[i] = 0;

                    }

                }

                List<int> list = new List<int>();

                for (int k = 0; k < arrys.Length; k++)

                {

                    if (arrys[k] != 0)

                        list.Add(arrys[k]);

                }

                int[] arryNew = new int[list.Count];

                for (int n = 0; n < list.Count; n++)

                {

                    arryNew[n] = list[n];

                }

                return arryNew;

            }

     

            /// <summary>

            /// 得到出现最多次数的数

            /// </summary>

            /// <param name="arrysOld"></param>

            /// <param name="arrysNew"></param>

            /// <returns></returns>

            private static int GetMaxCountNum(int[] arrysOld, int[] arrysNew)

            {

                int count = GetMaxCount(arrysOld, arrysNew);

                int tmp = 0;

                for (int i = 0; i < arrysNew.Length; i++)

                {

                    for (int j = 0; j < arrysOld.Length; j++)

                    {

                        if (arrysNew[i] == arrysOld[j])

                        {

                            tmp++;

                        }

                    }

                    if (count == tmp)

                    {

                        return arrysNew[i];

                    }

                    tmp = 0;

                }

                return 0;

            }

     

            /// <summary>

            /// 得到出现最多的次数

            /// </summary>

            /// <param name="arrysOld"></param>

            /// <param name="arrysNew"></param>

            /// <returns></returns>

            private static int GetMaxCount(int[] arrysOld, int[] arrysNew)

            {

                int maxCount = 0;

                int count = 0;//次数

                //int[] arrysCount = new int[arrysNew.Length];

                for (int i = 0; i < arrysNew.Length; i++)

                {

                    for (int j = 0; j < arrysOld.Length; j++)

                    {

                        if (arrysNew[i] == arrysOld[j])

                        {

                            count++;

                        }

                    }

                    //arrysCount[i] = count;

                    if (count > maxCount)

                    {

                        maxCount = count;

                    }

                    count = 0;

                }

                return maxCount;

            }

     

            /// <summary>

            /// 得到数组中的最大数

            /// </summary>

            /// <param name="arrys"></param>

            /// <returns></returns>

            private static int GetMaxNum(int[] arrys)

            {

                int max = arrys[0];

                for (int i = 0; i < arrys.Length; i++)

                {

                    if (arrys[i] > max)

                        max = arrys[i];

                }

                return max;

            }

     

            /// <summary>

            /// 得到数组中零出现的次数

            /// </summary>

            /// <param name="arrys"></param>

            /// <returns></returns>

            private static int GetMaxCountIsZero(int[] arrys)

            {

                int count = 0;

                for (int i = 0; i < arrys.Length; i++)

                {

                    if (arrys[i] == 0)

                        count++;

                }

                return count;

            }

     

            /// <summary>

            /// 克隆一个整型数组

            /// </summary>

            /// <param name="arrys"></param>

            /// <returns></returns>

            private static int[] CloneArry(int[] arrys)

            {

                int[] arrysNew = new int[arrys.Length];

                for (int i = 0; i < arrys.Length; i++)

                {

                    arrysNew[i] = arrys[i];

                }

                return arrysNew;

    }

    在招聘网上看到的题目,随便坐了下,可能还有bug,望大家指出!!

  • 相关阅读:
    Android WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法
    Android 自定义控件之圆形扩散View(DiffuseView)
    Android线性渐变
    Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()
    Android 用Handler和Message实现计时效果及其中一些疑问
    CentOS6.5下nginx-1.8.1.tar.gz的单节点搭建(图文详解)
    Zeppelin的入门使用系列之创建新的Notebook(一)
    hadoop报错java.io.IOException: Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured
    ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?(图文详解)
    VirtualBox里如何正确安装增强工具(图文详解)
  • 原文地址:https://www.cnblogs.com/cyan/p/1606004.html
Copyright © 2011-2022 走看看