无意间看到一道题,本以为很简单,可想了半天也没什么好的方法,不知那位有高招?

ychx7 2009-02-12 09:25:57

利用递规算法编写以下程序,计算用不同的正整数加出10000有多少种方法:
10000
=1+9999
=2+9998
=1+2+9997
(注:次序无关,即1+9999与9999+1认为是一种方法
...全文
399 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
fpwcs 2009-02-14
  • 打赏
  • 举报
回复
是不是费不离齐数啊,1+2+3+4+5+6+...........?
hbwhwang 2009-02-13
  • 打赏
  • 举报
回复
刚才的代码忘记“(注:次序无关,即1+9999与9999+1认为是一种方法”了,改了一下


package csdn.arithmetic;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

/**
* 利用递规算法编写以下程序,计算用不同的正整数加出10000有多少种方法: 10000 =1+9999 =2+9998 =1+2+9997
* (注:次序无关,即1+9999与9999+1认为是一种方法
* http://topic.csdn.net/u/20090212/21/d74a1994-58ce-4b9b-9a9c-74b487e7e81e.html
*
*/
public class SplitInteger {
private static List<Integer> copyList(List<Integer> used) {
List<Integer> newList = new ArrayList<Integer>(used.size());
for (Integer i : used) {
newList.add(i);
}
return newList;
}

private static String print(int sum, List<Integer> used, int x, int y) {
StringBuilder sb = new StringBuilder();
sb.append(sum).append("=");
List<Integer> temp = copyList(used);
temp.add(x);
temp.add(y);
Collections.sort(temp);
for (int i = 0; i < temp.size(); i++) {
sb.append(temp.get(i));
sb.append((i == temp.size() - 1) ? "" : "+");
}
return sb.toString();
}

private static void splitIt(int sum, int num, List<Integer> used,List<String> result) {
for (int i = 1; i <= num / 2; i++) {
List<Integer> temp = copyList(used);
if (!temp.contains(i) && !temp.contains(num - i)
&& (i != (num - i))) {
String ss=print(sum, temp, i, num - i);
if (!result.contains(ss)){
result.add(ss);
}
temp.add(i);
splitIt(sum, num - i, temp,result);
}
}
}

/**
* @param args
*/
public static void main(String[] args) {
List<String> result=new ArrayList<String>();
splitIt(10, 10, new ArrayList<Integer>(),result);
for (String s:result){
System.out.println(s);
}
}
}
lanseqiuyu 2009-02-13
  • 打赏
  • 举报
回复
public class Counter
{
public static int add(int start,int last)
{
int sum = 0;
for (int x = start;x <= last;++x) {
int y = start + last - x;
if (x > y) {
break;
} else if (x == y){
sum += 1;
break;
} else {
sum += add(x,y - x) + 1;
}
}

return sum;
}

public static void main(String[] args)
{
System.out.println(add(1,9999));
}
}
hbwhwang 2009-02-13
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.List;

/**
* 利用递规算法编写以下程序,计算用不同的正整数加出10000有多少种方法:
10000
=1+9999
=2+9998
=1+2+9997
(注:次序无关,即1+9999与9999+1认为是一种方法
http://topic.csdn.net/u/20090212/21/d74a1994-58ce-4b9b-9a9c-74b487e7e81e.html
*
*/
public class SplitInteger {
private static List<Integer> copyList(List<Integer> used){
List<Integer> newList=new ArrayList<Integer>(used.size());
for (Integer i:used){
newList.add(i);
}
return newList;
}

private static void print(int sum,List<Integer> used,int x,int y){
System.out.print(sum+"=");
if (used!=null&&used.size()>0){
for (int i=0;i<used.size();i++){
System.out.print(used.get(i)+"+");
}
}
System.out.println(x+"+"+y);
}

private static void splitIt(int sum,int num,List<Integer> used){
for (int i=1;i<=num/2;i++){
List<Integer> temp=copyList(used);
if (!temp.contains(i)&&!temp.contains(num-i)&&(i!=(num-i))){
print(sum,temp,i,num-i);
temp.add(i);
splitIt(sum,num-i,temp);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
splitIt(20,20,new ArrayList<Integer>());

}

}
bigbro001 2009-02-13
  • 打赏
  • 举报
回复
经典的记号~
捏造的信仰 2009-02-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ychx7 的回复:]
应该不对吧,相加的时候数值应该不允许重复才对
[/Quote]那就是说 10 不能拆分成 5+5 了?
melody1128 2009-02-13
  • 打赏
  • 举报
回复
mark
wyquan101 2009-02-13
  • 打赏
  • 举报
回复
CSDN上看了一篇文章,深受感动。
《现在的孩子怎么了》http://topic.csdn.net/u/20090109/15/19f950b4-656e-4d26-9d77-93783da54156.html
这里只提供思路。敬请谅解。

比如开始的数据是n,多少种方法为f,先为0;
1,把n除以2,如果整除f += n/2 - 1,否则f = n/2(向下取整,舍去小数).
说明:①、拿n = 10000,除2为5000,那么可以有一下的算法
n = 1 + 9999;
...
n = 5000 + 5000(这一个不符合题意,即:f += n/2 - 1)
②、拿n = 9999,除2为4999.5,向下取整为4999,那么可以有一下的算法
n = 1 + 9998;
...
n = 4999 + 5000(这一个符合题意,即:f += n/2)
2,然后 n = n / 2(向下取整),再执行第一步,知道n = 1.

还有一些细节,大家再思考思考吧。
wuhailin2005 2009-02-13
  • 打赏
  • 举报
回复
mousefog 2009-02-13
  • 打赏
  • 举报
回复
mark
renzaijiang 2009-02-13
  • 打赏
  • 举报
回复

using System;
using System.Collections;
class numbersplit
{
static void dec(int num,int i,int[] s,int count)
{
if(num==0)
{
if(count>1) //除去该数本身
{
for(int t=0;t<count;t++)
{
Console.Write("{0} ",s[t]);
}
Console.Write("\n");
}
return;
}
int temp=num;
for(int t=i;t<=temp;t++)
{
s[count]=t;
dec(num-t,t+1,s,count+1);
}
}
static void Main()
{
dec(30,1,new int[10000],0);//用30测试的
}

爱摸鱼de老邪 2009-02-13
  • 打赏
  • 举报
回复
转换为二进制位来考虑怎么样?例如1楼谈到的7:00000111(8位),考虑如下拆分:
1.首先拆分2个数(选择值为1的位的个数为标准),放入下面两个集合:
A,00000110;B,00000001
如果A0跟B0不一样,则拆分成功;
2.再拆分A0
A1,00000100;A2,00000010
如果拆分得到A1,A2的序列在A中不存在,则A0拆分成功;
4.收敛条件:得到AX序列中不存在2个值为1的位。
以上均没有考虑符号位,仅为个人意见。
shenkaitong68 2009-02-13
  • 打赏
  • 举报
回复
不会
zhangchaokun 2009-02-12
  • 打赏
  • 举报
回复
1楼说的对,你再仔细看看,一定要求加数不能重复吗?
kao331431214 2009-02-12
  • 打赏
  • 举报
回复
renzaijiang 2009-02-12
  • 打赏
  • 举报
回复
void dec(int num,int i)
{
if(num==0)
return;
for(int t=i;t<10000;t++)
{
cout<<i;
num-=t;
dec(num-t,i+1);
}
}
dec(1,10000);

以前做过 还要想想
ychx7 2009-02-12
  • 打赏
  • 举报
回复
应该不对吧,相加的时候数值应该不允许重复才对
云上飞翔 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 ychx7 的帖子:]

利用递规算法编写以下程序,计算用不同的正整数加出10000有多少种方法:
10000
=1+9999
=2+9998
=1+2+9997
(注:次序无关,即1+9999与9999+1认为是一种方法
[/Quote]
答:这是典型的整数拆分,若是拆分10000,则拆分数则很大.因此,拆分数值不能太大.今以7为例,运行程序.原理上是相同的.
参考代码:

//递归与栈的运用。
//整数的拆分

public class Split
{
static int[] stack = new int[100];//定义栈最大100
static int top = -1;//栈顶指针
static int n;//对n进行拆分
public static void main(String[] args)
{
n=7;
split(n);//对整数7进行拆分
}//main


static void split(int n)
{
split(n,n);//对n进行拆分。
}//split

static void split(int n,int k)
{//对n进行拆分。拆分出的最大数不能超过k

if( n<=0 )
{//
printStack();//栈中已有一个拆分。打印出来
return ;
}
for(int i=k;i>=1;i-- )
{
stack[++top]=i;//i已拆分出。进栈。
split(n-i, min(i,n-i) );//对剩下的n-i继续拆分
top--;//i出栈。继续下一个i-1的拆分。
}//for(i)
}//split

static int min(int m,int n)
{
return m<n?m:n;
}//min


static void printStack()
{
System.out.print(n+"=");
for( int i=0;i<=top;i++ )
{
System.out.print(stack[i]+"+");
}//for
System.out.println("0");
}//printStack
}//class

程序运行结果:
7=7+0
7=6+1+0
7=5+2+0
7=5+1+1+0
7=4+3+0
7=4+2+1+0
7=4+1+1+1+0
7=3+3+1+0
7=3+2+2+0
7=3+2+1+1+0
7=3+1+1+1+1+0
7=2+2+2+1+0
7=2+2+1+1+1+0
7=2+1+1+1+1+1+0
7=1+1+1+1+1+1+1+0

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧