- 浏览: 31155 次
- 性别:
- 来自: 合肥
最新评论
/** * 求数值在 1 - 50 之内的任意个数之和为100 。 * 数值:1,2,3,4,5,6....48,49,50 * 取其中的任意个数,但这5个数相加之和要等于100,并将所有这种组合罗列 * 例: 1+10+19+20+50=100 * 3+14+16+27+40=100 */ import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @author Administrator * */ public class NumberSum { //private List<String> list = new ArrayList<String>(); /** * 返回在begin,end之间的count个数的和为sum * @param begin * @param end * @param count * @return */ public List sumList(int begin, int end, int count,int sum) { if (count>=2) { if (count == 2) { List<String> listfirst = new ArrayList<String>(); for (int i=begin; i<=end; i++) { if ((i < (sum-i)) && ((sum-i)<=end) && ((sum-i)>0)) { String str = i + "," + (sum-i); listfirst.add(str); } } return listfirst; } else { count = count - 1; List<String> listlast = new ArrayList<String>(); for (int j=end; j>=begin; j--) { List<String> list = sumList(begin,end,count,sum-j); List<String> sublist = new ArrayList<String>(); for (int k=0; k<list.size(); k++) { String[] number = list.get(k).toString().split(","); int max =0; for (String num : number) { if (max <= Integer.parseInt(num.trim())) { max = Integer.parseInt(num.trim()); } } if (j>max) { String str = list.get(k) + "," + j; // list.set(k, str); //} else { //list.remove(k); sublist.add(str); } } listlast.addAll(sublist); } return listlast; } } return null; } private void display(List list){ for (Object str : list) { System.out.println(str + "sum = 100"); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub NumberSum obj = new NumberSum(); List list = obj.sumList(1, 50, 5, 100); //3 size 208 5size:18583 obj.display(list); System.out.println("list size:" + list.size()); //数值在 1 - 50 之内的任意个数之和为100 // for (int count=3; count<=100; count++) { // List list = obj.sumList(1, 50, count, 100); // obj.display(list); // System.out.println("list size:" + list.size()); // } } }
运行结果:
1,49,50sum = 100
2,48,50sum = 100
3,47,50sum = 100
4,46,50sum = 100
5,45,50sum = 100
6,44,50sum = 100
7,43,50sum = 100
8,42,50sum = 100
9,41,50sum = 100
10,40,50sum = 100
11,39,50sum = 100
12,38,50sum = 100
13,37,50sum = 100
14,36,50sum = 100
15,35,50sum = 100
16,34,50sum = 100
17,33,50sum = 100
18,32,50sum = 100
19,31,50sum = 100
20,30,50sum = 100
21,29,50sum = 100
22,28,50sum = 100
23,27,50sum = 100
24,26,50sum = 100
3,48,49sum = 100
4,47,49sum = 100
5,46,49sum = 100
6,45,49sum = 100
7,44,49sum = 100
8,43,49sum = 100
9,42,49sum = 100
10,41,49sum = 100
11,40,49sum = 100
12,39,49sum = 100
13,38,49sum = 100
14,37,49sum = 100
15,36,49sum = 100
16,35,49sum = 100
17,34,49sum = 100
18,33,49sum = 100
19,32,49sum = 100
20,31,49sum = 100
21,30,49sum = 100
22,29,49sum = 100
23,28,49sum = 100
24,27,49sum = 100
25,26,49sum = 100
5,47,48sum = 100
6,46,48sum = 100
7,45,48sum = 100
8,44,48sum = 100
9,43,48sum = 100
10,42,48sum = 100
11,41,48sum = 100
12,40,48sum = 100
13,39,48sum = 100
14,38,48sum = 100
15,37,48sum = 100
16,36,48sum = 100
17,35,48sum = 100
18,34,48sum = 100
19,33,48sum = 100
20,32,48sum = 100
21,31,48sum = 100
22,30,48sum = 100
23,29,48sum = 100
24,28,48sum = 100
25,27,48sum = 100
7,46,47sum = 100
8,45,47sum = 100
9,44,47sum = 100
10,43,47sum = 100
11,42,47sum = 100
12,41,47sum = 100
13,40,47sum = 100
14,39,47sum = 100
15,38,47sum = 100
16,37,47sum = 100
17,36,47sum = 100
18,35,47sum = 100
19,34,47sum = 100
20,33,47sum = 100
21,32,47sum = 100
22,31,47sum = 100
23,30,47sum = 100
24,29,47sum = 100
25,28,47sum = 100
26,27,47sum = 100
9,45,46sum = 100
10,44,46sum = 100
11,43,46sum = 100
12,42,46sum = 100
13,41,46sum = 100
14,40,46sum = 100
15,39,46sum = 100
16,38,46sum = 100
17,37,46sum = 100
18,36,46sum = 100
19,35,46sum = 100
20,34,46sum = 100
21,33,46sum = 100
22,32,46sum = 100
23,31,46sum = 100
24,30,46sum = 100
25,29,46sum = 100
26,28,46sum = 100
11,44,45sum = 100
12,43,45sum = 100
13,42,45sum = 100
14,41,45sum = 100
15,40,45sum = 100
16,39,45sum = 100
17,38,45sum = 100
18,37,45sum = 100
19,36,45sum = 100
20,35,45sum = 100
21,34,45sum = 100
22,33,45sum = 100
23,32,45sum = 100
24,31,45sum = 100
25,30,45sum = 100
26,29,45sum = 100
27,28,45sum = 100
13,43,44sum = 100
14,42,44sum = 100
15,41,44sum = 100
16,40,44sum = 100
17,39,44sum = 100
18,38,44sum = 100
19,37,44sum = 100
20,36,44sum = 100
21,35,44sum = 100
22,34,44sum = 100
23,33,44sum = 100
24,32,44sum = 100
25,31,44sum = 100
26,30,44sum = 100
27,29,44sum = 100
15,42,43sum = 100
16,41,43sum = 100
17,40,43sum = 100
18,39,43sum = 100
19,38,43sum = 100
20,37,43sum = 100
21,36,43sum = 100
22,35,43sum = 100
23,34,43sum = 100
24,33,43sum = 100
25,32,43sum = 100
26,31,43sum = 100
27,30,43sum = 100
28,29,43sum = 100
17,41,42sum = 100
18,40,42sum = 100
19,39,42sum = 100
20,38,42sum = 100
21,37,42sum = 100
22,36,42sum = 100
23,35,42sum = 100
24,34,42sum = 100
25,33,42sum = 100
26,32,42sum = 100
27,31,42sum = 100
28,30,42sum = 100
19,40,41sum = 100
20,39,41sum = 100
21,38,41sum = 100
22,37,41sum = 100
23,36,41sum = 100
24,35,41sum = 100
25,34,41sum = 100
26,33,41sum = 100
27,32,41sum = 100
28,31,41sum = 100
29,30,41sum = 100
21,39,40sum = 100
22,38,40sum = 100
23,37,40sum = 100
24,36,40sum = 100
25,35,40sum = 100
26,34,40sum = 100
27,33,40sum = 100
28,32,40sum = 100
29,31,40sum = 100
23,38,39sum = 100
24,37,39sum = 100
25,36,39sum = 100
26,35,39sum = 100
27,34,39sum = 100
28,33,39sum = 100
29,32,39sum = 100
30,31,39sum = 100
25,37,38sum = 100
26,36,38sum = 100
27,35,38sum = 100
28,34,38sum = 100
29,33,38sum = 100
30,32,38sum = 100
27,36,37sum = 100
28,35,37sum = 100
29,34,37sum = 100
30,33,37sum = 100
31,32,37sum = 100
29,35,36sum = 100
30,34,36sum = 100
31,33,36sum = 100
31,34,35sum = 100
32,33,35sum = 100
list size:208
发表评论
-
Java面向对象特性
2011-03-20 21:21 795Java面向对象特性 1.继承 ... -
Java 集合框架小结
2011-03-20 21:21 806Java 集合框架(Java Collecti ... -
Java多线程机制
2011-03-20 21:18 858一 线程的基本概念 线 ... -
Java代码编写的30条建议
2011-03-20 21:16 646(1) 类名首字母应该大写 ... -
文件目录打印程序
2011-03-20 21:15 772实现了一个文件目录打 ... -
一个简单的邮件收发程序
2011-03-20 21:13 1034package net.csdn.blog.johnsto ... -
开发人员常去的网站
2011-03-20 21:08 1004http://www.gotapi.com/ 语言 ... -
用Dom4j解析XML文件
2011-03-20 21:00 1399用Dom4j解析XML文件的简单实例。 package c ... -
JAVA中preparedStatement和Statement的区别?
2011-03-19 18:21 665一.代码的可读性和可维 ... -
MyEclipse8.5学习手册
2011-03-06 23:50 2295MyEclipse8.5支持Struts2.1.8.1 ... -
Java编程思想学习
2010-12-04 22:06 1201一 基础知识点 1.面向对 ... -
Java编码规范
2010-12-04 21:56 844一 命名惯例 1)类:名 ...
相关推荐
"一些java简单程序"这个主题,显然聚焦于帮助初学者理解Java编程基础。在这个压缩包中,"study path"可能是一个目录或者文件,它可能包含了学习Java的路径或者一系列逐步进阶的代码示例。 在Java编程中,初学者通常...
一个简单的java程序,主要用到了 继承,计算圆的面积,圆柱 的面积等等。。。
"java简单实例程序源代码"这个压缩包包含了一系列章节相关的Java实例源代码,适合初学者和有经验的开发者用来加深对Java语言的理解。以下是这些章节可能涉及的重要知识点的详细解释: 1. **CH11**: 这个章节可能...
总结来说,"java 绘图板简易程序"是一个用Java编写的GUI应用,它利用Swing或JavaFX库提供了用户交互的绘图功能。源代码和编译后的字节码文件分别展示了程序的设计思路和可执行形态。理解和分析这些文件可以帮助我们...
Java简易聊天程序是一种基于Java编程语言实现的交互式通信应用,它允许用户通过网络进行文本聊天。这个程序的核心概念和关键技术主要包括多线程、网络编程、输入/输出流以及简单的用户界面设计。以下是对这些关键...
"几个简单的java小程序"这个标题暗示了我们将会看到一系列小型的Java应用程序,这些程序可能涵盖基础到进阶的编程概念。这些小程序可能是为了教学或测试目的,通过它们,我们可以深入理解Java编程的核心概念。 首先...
### Java版简易计算器程序设计知识点解析 #### 一、项目概述 本项目为一个简易计算器的设计与实现,采用Java语言开发。该计算器具备基本的算术运算功能,并且集成了记忆存储功能,能够满足日常计算的基本需求。项目...
我用JAVA的servlet写的这个小聊天程序,总共才几KB,但可以在不同的电脑上进行多人对话,删除记录等,我把数据库验证登陆写进去了,你若不想要这个登陆程序可以删掉,那样就不需要登陆直接开始聊天了。这个程序结构...
JAVA简单记事本程序设计实验报告是一份详细记录了如何使用Java编程语言开发一个简易记事本应用程序的文档。这个程序旨在实现基本的文本编辑功能,包括文件操作(新建、打开、保存、退出)和编辑操作(剪切、拷贝、...
这个“经典java小程序源代码合集”是为Java初学者准备的宝贵资源,它包含了一系列在学习过程中可能会遇到的经典示例,有助于深入理解Java语言的核心概念。 首先,让我们探讨Java的基础知识。Java程序由类(class)...
Java语言因其简单、面向对象、分布式、解释执行、稳健、安全、体系结构中立、可移植、高性能、多线程和动态等特性而受到青睐。 5. Java编程语言与C++的关系:文档中出现了Java和C++的比较,体现了Java语言在设计上...
本资源提供了一个简单的JAVA Application和JAVA Applet的示例代码,演示了如何编写、编译和运行这些程序。 五、编程实现 本资源还提供了两个编程实现的示例,演示了如何使用JAVA语言实现随机产生20个整数存放到...
【Java简易聊天程序】是一个基于GUI(图形用户界面)和SOCKET通信技术的编程实践项目。这个程序展示了如何在Java环境中构建一个简单的聊天应用程序,允许两个或更多的用户通过网络进行实时交流。以下是对该项目涉及...
这些小程序虽然简单,但它们涵盖了Java的基础知识,如基本数据类型、控制流(条件语句和循环)、输入/输出操作以及简单的算术运算。对于初学者来说,这些都是非常实用的练习,有助于理解和巩固Java编程的基本概念。...
* 简单性(Simple):Java语言语法和语义都比较单纯,容易学习和使用。 * 面向对象(Object-oriented):Java语言支持面向对象的编程,具有模块化性质和信息隐藏能力,满足面向对象的封装要求,同时支持继承和多态。...
java简单程序,有利于初学者,对于初学者有较大帮助
现在,我们将深入探讨Java源程序和简单检索的相关知识点。 首先,Java源程序是程序员用Java语法编写的应用程序,这些程序在编译后会转换为字节码(`.class`文件),由Java虚拟机(JVM)执行。Java源代码必须遵循...
【标题】:“java简单界面的ATM程序” 这个标题表明我们正在讨论一个使用Java编程语言实现的自动取款机(ATM)模拟程序。在Java中,开发这样的界面程序通常涉及Swing或JavaFX库来创建图形用户界面(GUI)。ATM程序...
Java扫雷简易实例程序是一种基于Java编程语言实现的简化版经典扫雷游戏。这个程序的特点是简洁明了,没有复杂的用户界面,旨在帮助初学者理解基础的Java编程概念和逻辑控制。下面我们将深入探讨该程序可能包含的关键...