zoukankan      html  css  js  c++  java
  • 广州某公司笔试题(英文)java算法实现

     

    Arithmetic<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    Please complete the function according to the document demand

     

    public class Ncsmsoft {

           public static void main(String[] args) {

                  //test result

                  System.out.print(findSum(35,"12,60,8,-8,99,15,35,17,18"));

           }

       

           /*==============================================

                   functionfindSum

                    creator(name)

                create time(time)

               updator name(name)

                update time(time)

             version number1.0

                descriptionfind all the possible assembled from the character string that the sum of which

                              equal to the appoint value

             input parameterDouble he     //appoint value

                             String shuzu  //a character string divide by comma

           output parameterresult

              output formatfor example "12+8+15;35;17+18"

                    testor (name)

           =================================================*/

           private static String findSum(double he,String shuzu){

                  StringBuffer result=new StringBuffer();

                  //Please input your program here without changing of the other code

                 

                 

     

                    //return your result as the format "12+8+15;35;17+18"

                  return result.toString();

           }

    }

     

    answer:

    package com.softeem.demo;

     

    import java.util.ArrayList;

    import java.util.Arrays;

     

    public class Ncsmsoft {

           public static void main(String[] args) {

                  // test result

                  System.out.print(findSum(35, "12,60,8,-8,99,15,35,17,18"));

           }

           /*

            * ============================================== functionfindSum

            * creator(name) create time(time) updator name(name) update time(time)

            * version number1.0 descriptionfind all the possible assembled from the

            * character string that the sum of which equal to the appoint value input

            * parameterDouble he //appoint value String shuzu //a character string

            * divide by comma output parameterresult output formatfor example

            * "12+8+15;35;17+18" testor (name)

            * =================================================

            */

           private static String findSum(double he, String shuzu) {

                  StringBuffer result = new StringBuffer();

                  // Please input your program here without changing of the other code

                  String[] strs = shuzu.split(",");

                  int[] a = new int[strs.length];

                  for (int i = 0; i < a.length; i++) {

                         a[i] = Integer.parseInt(strs[i]);

                  }

                  Arrays.sort(a); // 对初始数据进行排序,便于算法的实现

                  ArrayList<Integer> okList = new ArrayList<Integer>(); // 存放的是当前找到的合法的元素,这些元素的和小于SUM

                  find(a, he, 0, okList, result); // 通过递归的方式进行查找

                  // return your result as the format "12+8+15;35;17+18"

                  return result.toString();

           }

           /**

            * 递归函数,每一次的动作很简单,在已经找到的n个元素的基础上,寻找第n+1个元素

            */

           private static void find(int[] a, final double SUM, int cur,

                         ArrayList<Integer> okList, StringBuffer result) {

                  int beg = okList.size() == 0 ? 0 : okList.get(okList.size() - 1) + 1; // 当前元素的查找范围的起始位置

                  for (int i = beg; i < a.length; i++) { // 从起始位置到结束位置,查找合适的元素

                         cur += a[i]; // 在前面元素的和的基础上,加上当前元素

                         if (cur < SUM) { // 如果仍然小于SUM,证明当前元素(n+1)合法,继续寻找第n+2个元素

                                okList.add(i);

                                find(a, SUM, cur, okList, result);

                                cur -= a[i]; // 消除第i个元素的影响,为了试验第i+1个元素做准备

                                okList.remove(okList.size() - 1); // 消除第i个元素的影响,为了试验第i+1个元素做准备

                         } else if (cur == SUM) { // 如果等于SUM,证明找到了表达式

                                for (int x : okList) { // 构造表达式,并存入result

                                       result.append(a[x]);

                                       result.append("+");

                                }

                                result.append(a[i]);

                                result.append(";");

                                break; // 回溯到上一个状态

                         } else { // 如果大于SUM,也回溯到上一个状态

                                break;

                         }

                  }

           }

    }

  • 相关阅读:
    在 Win7 下使用 VS2005 编译 Chrome
    C++ Qt 05:Qt布局管理器 荒 木 博客园
    电商们都挺贼的,便宜的东西都不肯卖,比如牙膏,谁要用一支20多的呀,3-4块的中华没人进货,
    python 下载 国内
    爱慕集团以及爱慕在线科技有限公司简介 Aimerman
    Build Instructions (Windows) The Chromium Projects
    C++ Qt 03:MainWindow和Qt Gui编程
    今天 穿vancl的衣服撞衫了,ooooooooppppppppppppppps
    2006年世界杯小组赛日程表
    C++ 虚函数表解析
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9663801.html
Copyright © 2011-2022 走看看