您的位置:首页 > 编程语言 > Java开发

广州某公司笔试题(英文)-java算法实现

2008-08-27 16:48 337 查看
Arithmetic

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"));
}

/*==============================================
function:findSum
creator:(name)
create time:(time)
updator name:(name)
update time:(time)
version number:1.0
description:find all the possible assembled from the character string that the sum of which
equal to the appoint value
input parameter:Double he //appoint value
String shuzu //a character string divide by comma
output parameter:result
output format:for 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"));
}
/*
* ============================================== function:findSum
* creator:(name) create time:(time) updator name:(name) update time:(time)
* version number:1.0 description:find all the possible assembled from the
* character string that the sum of which equal to the appoint value input
* parameter:Double he //appoint value String shuzu //a character string
* divide by comma output parameter:result output format:for 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;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐