zoukankan      html  css  js  c++  java
  • 某某面试题

    1.设计算法

    输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。

    Console.WriteLine("请输入一组升序的数组值。");
    
                var all = Console.ReadLine().Split(',');
    
    
                Console.WriteLine("请输入数字。");
    
                var str = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(str))
                {
    
                    for (int i = 0; i < all.Length; i++)
                    {
                        for (int j = all.Length - 1; j > 0; j--)
                        {
                            if (i != j)
                            {
                                if (int.Parse(all[i]) + int.Parse(all[j]) == int.Parse(str))
                                {
                                    Console.WriteLine("数组为" + all[i] + "," + all[j]);
                                    break;
                                }
                            }
                        }
                    }
                }

      

    2.随意输入的字母,遍历出最先重复的字母

     
    string result = string.Empty;
                var len = str.Length;
                char[] arr = str.ToCharArray();
                int[] a = new int[len];
    
                int index = 0;
                for (int i = 0; i < len; i++)
                {
                    index = str.IndexOf(arr[i], i + 1);
                    if (index == -1)
                        a[i] = 999;
                    else
                        a[i] = index;
                }
    
                Array.Sort(a);
    
                if (index == 999)
                {
                    result = "没有出现重复的";
                }
                else
                    result = arr[a[0]].ToString();

    3.任意数字的因数从小到大排列 

    if (n == 1)
                {
                    result = "没有因数";
                    return result;
    
                }
    
                for (int i = 2; i < n + 1; i++)
                {
    
                    if (n == 1)
                        break;
                    for (int j = 0; ; j++)
                    {
                        if (n % i == 0)
                        {
    
                            result += i.ToString() + ",";
                            n = n / i;
                        }
    
                        else
                        {
                            break;
                        }
    
                    }  
                }
  • 相关阅读:
    git push要输入密码问题
    excel换行
    React的diff算法
    https的通信过程
    一道面试题的分析
    Mac将应用拖入Finder工具栏
    React获取组件实例
    Warning: Received `false` for a non-boolean attribute `xxx`.
    warning: React does not recognize the xxx prop on a DOM element
    webpack开发模式和生产模式设置及不同环境脚本执行
  • 原文地址:https://www.cnblogs.com/sunnie/p/4950701.html
Copyright © 2011-2022 走看看