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

    给定一个整数数组,例如{2,4,5,6,7,8}和一个数字,例如10,请设计一个函数找出两个元素,并且使这两个数的和为给定数字,并答应出来。

    代码
    using System;
    using System.Collections.Generic;

    public class MyClass
    {
        
    public static void Main()
        {
            
    int[] a=new int[]{2,3,4,5,6,7,8,9};
            
    int sum=10;
            
    int n=sum/2;
            
    int nc=0;
            
    for(int i=0;i<a.Length;i++)
            {
                
    if(a[i]>n)
                {
                    nc
    =i;
                    
    break;
                }
            }
            
    if(nc==0)
            {
                Console.WriteLine(
    "没有这样的两个数字之和等于"+sum);
                
    return;
            }
            
    int nc1=nc;
            
    int nc2=nc;
            
    while(nc1>=0 && nc2<a.Length)
            {
                
    int a1=a[nc1];
                
    int a2=a[nc2];    
                
    if(a1+a2==sum)
                {
                    Console.WriteLine(a1
    +"+"+a2+"="+sum);
                    nc1
    --;
                }
                
    else if(a1+a2>sum)
                {
                    nc1
    --;
                }
                
    else if(a1+a2<sum)
                {
                    nc2
    ++;
                }
            }
            Console.ReadLine();
        }
    }


  • 相关阅读:
    事务笔记
    MyBatis执行流程(面试题)
    mapper映射文件中 #{}与${}区别(面试题)
    前端内存泄露浅谈
    vue中使用element来创建目录列表
    vue中组件之间的传值
    nodejs+vue实现登录界面功能(二)
    nodejs+vue实现登录界面功能(一)
    threejs(一):初步认识与使用
    官网编辑遇到的各种问题记录(一)
  • 原文地址:https://www.cnblogs.com/mikechang/p/1710500.html
Copyright © 2011-2022 走看看