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;

                         }

                  }

           }

    }

  • 相关阅读:
    linux部署nuxt.js项目
    vue---el-table设置表头居中,内容列居中/左对齐/右对齐
    偶然看到的jquery选择器性能问题
    关于js中的回调函数问题
    html5图片上传(搬砖)
    css上传文件样式元素样式美化
    小记--转自张鑫旭的css命名规则
    关于nodeJS 在webstorm中的服务器配置
    关于window上的github 上传本地文件--傻瓜式教程
    关于PS的基本操作
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9663801.html
Copyright © 2011-2022 走看看