java程序或者asp程序1到33其中6个数相加等于86 150
java程序或者asp程序1到33其中6个数相加等于86;需要用javs或者asp程序写,提供程序代码,谢谢...
java程序或者asp程序1到33其中6个数相加等于86;需要用javs或者asp程序写,提供程序代码,谢谢
展开
2016-03-01 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
java程序参考:
public class FindSubsetsThatSumToATarget {
/**
* The collection for storing the unique sets that sum to a target.
*/
private static HashSet<String> allSubsets = new HashSet<String>();
/**
* The String token
*/
private static final String token = " ";
/**
* The method for finding the subsets that sum to a target.
*
* @param input The input array to be processed for subset with particular sum
* @param target The target sum we are looking for
* @param ramp The Temporary String to be beefed up during recursive iterations(By default value an empty String)
* @param index The index used to traverse the array during recursive calls
*/
public static void findTargetSumSubsets(int[] input, int target, String ramp, int index) {
if(index > (input.length - 1)) {
if(getSum(ramp) == target) {
allSubsets.add(ramp);
}
return;
}
//First recursive call going ahead selecting the int at the currenct index value
findTargetSumSubsets(input, target, ramp + input[index] + token, index + 1);
//Second recursive call going ahead WITHOUT selecting the int at the currenct index value
findTargetSumSubsets(input, target, ramp, index + 1);
}
/**
* A helper Method for calculating the sum from a string of integers
*
* @param intString the string subset
* @return the sum of the string subset
*/
private static int getSum(String intString) {
int sum = 0;
StringTokenizer sTokens = new StringTokenizer(intString, token);
while (sTokens.hasMoreElements()) {
sum += Integer.parseInt((String) sTokens.nextElement());
}
return sum;
}
/**
* Cracking it up here : )
*
* @param args command line arguments.
*/
public static void main(String[] args) {
int [] n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
int counter = 1;
FindSubsetsThatSumToATarget.findTargetSumSubsets(n, 86, "", 0);
for (String str: allSubsets) {
System.out.println(counter + ") " + str);
counter++;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Java5 {
public static void main(String args[]){
for(int i = 1; i <34; i++)
for(int j = 1; j <34; j++)
for (int k = 1; k <34; k++)
for (int a = 1; a <34; a++)
for (int b = 1; b <34; b++) {
int w=86- ( i+j+k+a+b);
if ((w >0)&& (w <34))
System.out.println("the six number are "+i+" "+j+" "+k+" "+a+" "+b+" "+w+" .");
}
}
}
public static void main(String args[]){
for(int i = 1; i <34; i++)
for(int j = 1; j <34; j++)
for (int k = 1; k <34; k++)
for (int a = 1; a <34; a++)
for (int b = 1; b <34; b++) {
int w=86- ( i+j+k+a+b);
if ((w >0)&& (w <34))
System.out.println("the six number are "+i+" "+j+" "+k+" "+a+" "+b+" "+w+" .");
}
}
}
更多追问追答
追问
int w=86- ( i+j+k+a+b);
和System.out.println("the six number are "+i+" "+j+" "+k+" "+a+" "+b+" "+w+" .");
句中的i j k a显示红色错误呀,请解决,谢谢
追答
这个程序我在本地执行过是没有问题的呀。
你将错误copy给我看看。。。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询