ITPUB??ì3
ITPUB论坛 » Java企业开发 » 想练手的进来(转贴)

新一届的微软MVP评选已经开始,欢迎各位推荐!

标题: 想练手的进来(转贴)
离线 Tom Zhang
老会员



精华贴数 1
个人空间 0
技术积分 1268 (1362)
社区积分 663 (1193)
注册日期 2001-10-15
论坛徽章:3
ITPUB元老会员2006贡献徽章授权会员   
      

发表于 2002-1-7 20:17 
想练手的进来(转贴)

昨天去面试人家出了这样一道题,觉得挺简单的,但就是编不出来,只好麻烦各位高手了。~v~
给定两个正整数m,n(m>=n!),将m拆成n个数相加:m=a(1)+a(2)+...+a(n),使之满足:a(1)<a(2)<...<a(n);
编成列出所有的拆法.
例如:若m=7,n=3则只有一种拆法:
7=1+2+4



并测试:m:100,n:4,Total No应为:5952,请打出清单,统计所用时间。




__________________
没了
只看该作者    顶部
离线 Tom Zhang
老会员



精华贴数 1
个人空间 0
技术积分 1268 (1362)
社区积分 663 (1193)
注册日期 2001-10-15
论坛徽章:3
ITPUB元老会员2006贡献徽章授权会员   
      

发表于 2002-1-10 16:13 
这里的气氛为什么这样差啊?

同样的贴子在CSDN上有很多人跟贴,这里的人是不是都只想讨论问题啊?
不做些东西,怎能有所收获呢?CSDN上还是VC的版块,我看了,就用JAVA做了,为什么这儿就没人写啊?


__________________
没了
只看该作者    顶部
离线 hlcathy
初级会员



精华贴数 0
个人空间 0
技术积分 70 (21405)
社区积分 11 (10023)
注册日期 2001-12-12
论坛徽章:0
      
      

发表于 2002-1-10 16:47 
看到题就头大。

很好奇你面试的工作是什么东东??


只看该作者    顶部
离线 hlcathy
初级会员



精华贴数 0
个人空间 0
技术积分 70 (21405)
社区积分 11 (10023)
注册日期 2001-12-12
论坛徽章:0
      
      

发表于 2002-1-10 16:55 
这题在学校时做过。有规律的。
只是个算法的问题
多写几个例子,就能找到规律。懒得想了。估计是MS的面试。哈哈


只看该作者    顶部
离线 Arraymacsboy
初级会员



精华贴数 0
个人空间 0
技术积分 12 (73240)
社区积分 0 (49474)
注册日期 2001-12-12
论坛徽章:0
      
      

发表于 2002-1-10 18:00 
无非就是惩罚原理

以100,4为例:
24x23=552.

先将100分成两分,每份为50的可能性是25种组合,注意25+25不算,因为25==25.

所以就只有24种可能,而第二分不可以与第一分相同,所以只有23中可能了。

因该是微软的题,呵呵。


只看该作者    顶部
离线 oramind
版主


精华贴数 0
个人空间 0
技术积分 2138 (741)
社区积分 148 (2791)
注册日期 2001-9-25
论坛徽章:4
ITPUB元老管理团队2006纪念徽章会员2006贡献徽章授权会员  
      

发表于 2002-1-12 04:00 
Re: 这里的气氛为什么这样差啊?



QUOTE:
最初由 Tom Zhang 发布
同样的贴子在CSDN上有很多人跟贴,这里的人是不是都只想讨论问题啊?
不做些东西,怎能有所收获呢?CSDN上还是VC的版块,我看了,就用JAVA做了,为什么这儿就没人写啊?


this is not a java issue, this is a math problem.
i know a lot of excellent java programmers who will not be able to solve this kind of question, and who won't even bother.


__________________
※※※※※※※※※※※※※※※在UNIX版发贴必读!否则关帖※※※※※※※※※※※※※※※http://www.itpub.net/showthread.php?s=&threadid=38248
只看该作者    顶部
离线 Tom Zhang
老会员



精华贴数 1
个人空间 0
技术积分 1268 (1362)
社区积分 663 (1193)
注册日期 2001-10-15
论坛徽章:3
ITPUB元老会员2006贡献徽章授权会员   
      

发表于 2002-2-26 11:15 
我的程序,大家参考,请多指教


__________________
没了
只看该作者    顶部
离线 TRYIT
一般会员



精华贴数 0
个人空间 0
技术积分 266 (7312)
社区积分 15 (8608)
注册日期 2002-2-26
论坛徽章:1
授权会员     
      

发表于 2002-2-26 13:18 
no meaning

it seems no meaning for this program


只看该作者    顶部
离线 chen09
中级会员



精华贴数 0
个人空间 0
技术积分 454 (4274)
社区积分 5 (15002)
注册日期 2002-2-14
论坛徽章:0
      
      

发表于 2002-2-26 13:52 
用到回溯的概念。
一般而言,回溯的问题比递归难一点。而且,为了节省资源,只用循环不用函数自身调用。
几年前考高级程序员时做了大量有关回溯的试题,你的问题也是其中之一。
有空俺会回味一下,写一下你的题。
不过,如果你很急的话,就去找一下高级程序员的题库。


__________________
- In the world, I have nothing to do, just life.
只看该作者    顶部
离线 chen09
中级会员



精华贴数 0
个人空间 0
技术积分 454 (4274)
社区积分 5 (15002)
注册日期 2002-2-14
论坛徽章:0
      
      

发表于 2002-2-26 21:11 
to: Tom Zhang

不要这么说嘛!
oramind说得不错,这是算法问题,不是Java问题,而且这题太简单,一个循环就结束了,没有意思。
不过你既然这么说,俺就写一遍好了。
把下面存成:Math1.java
编译:javac -d . Math1.java

m=100,n=4时
运行:java per.chen09.itpub.Math1 100 4
结果:
..
..
..
..
=======================================
Sum : 100       Split : 4
Total result: 5952      Used Time : 20


m=100,n=5时
运行:java per.chen09.itpub.Math1 100 5
结果:
..
..
..
..
Sum : 100       Split : 5
Total result: 25337     Used Time : 90
Programed by Chen09.

以下java代码:
------------------------------------------------------------
//split number
//give M,N
//let m=a(1)+a(2)+...+a(n), a(1)<a(2)<...<a(n)
// etc. 7=1+2+4

package per.chen09.itpub;

import java.util.ArrayList;

class Math1
{
        //the last process time in call splitNumber function.
        private static long longProcTime;
       
        //There are just test code it main function.
        //The program will not use Junit for test, so must put test code there.
        public static void main(String[] strArgs)
        {
                System.out.println();

               
                ArrayList al = Math1.splitNumber(strArgs[0],strArgs[1]);
                int intSize = al.size();
                for(int i = 0; i<intSize;i++)
                {
                        int[] intArrayTemp = (int[])al.get(i);
                        System.out.print("result "+(i+1) +" : ";
                        for(int j=0;j<intArrayTemp.length;j++)
                        {
                                System.out.print(intArrayTemp[j]+" ";
                        }
                       
                        System.out.println();
                }
               
                System.out.println("=======================================";

                System.out.println("Sum : " + strArgs[0] + "\tSplit : " + strArgs[1]);
                System.out.println("Total result: "+intSize+"\tUsed Time : "+ Math1.getProcTime());
                System.out.println("Programed by Chen09.";
               
        }
       
        //if paramates' type is String
        public static ArrayList splitNumber(String strM, String strN)
        {
                return splitNumber(Integer.parseInt(strM),Integer.parseInt(strN));
        }
       
        //the type of ArrayList's item is int[]
        public static ArrayList splitNumber(int intM, int intN)
        {
                long longBeginTime = System.currentTimeMillis();
                ArrayList alResults = new ArrayList();
               
                int[] intResult = new int[intN];
               
                int intPoint = 0;
                intResult[intPoint] = 0;
                int intRemain = intM;
               
                while(intPoint >= 0)
                {
                        intResult[intPoint]++;
                        intRemain = intRemain- intResult[intPoint];
                        //System.out.println(intRemain);

                        if(intPoint==intN-2)        //arrive the last
                        {
                                intResult[intPoint+1] = intRemain;

                                intRemain = intRemain + intResult[intPoint];
                               
                                //add the result array
                                alResults.add(intResult);

                                //create new result array, and copy from old
                                intResult = backupIntArray(intResult);

                                if(intResult[intPoint+1] - intResult[intPoint] <= 2)
                                {
                                        //not result any more, intPoint be back.
                                        intPoint--;
                                        intRemain = intRemain + intResult[intPoint];
                                       
                                }

                               
                        }
                        else if(getSum(intResult[intPoint]+1,intN-intPoint-1) > intRemain)
                        {
                                //not arrive the last, but not result
                                intRemain = intRemain + intResult[intPoint];
                                intPoint--;
                                if(intPoint>=0)
                                        intRemain = intRemain + intResult[intPoint];

                        }
                        else
                        {
                                //have result
                               
                                //set the next item
                                intResult[intPoint+1] = intResult[intPoint];
                                intPoint++;

                        }
                }
               
                       
                //save the time of process
                setProcTime(System.currentTimeMillis() - longBeginTime);
               
                return alResults;
        }
       
        //get process time
        public static long getProcTime()
        {
                return longProcTime;
        }
       
        //set process time
        protected static void setProcTime(long longProcTime)
        {
                Math1.longProcTime = longProcTime;
        }
       
        //get the sum = intBase + (intBase + 1) + ... + (intBase + intCount)
        private static int getSum(int intBase,int intCount)
        {
                return (intBase+ (intBase + intCount - 1))*intCount/2;
        }
       
        //clone a int array
        private static int[] backupIntArray(int[] intOldArray)
        {
                int intCount = intOldArray.length;
                int[] intNewArray = new int[intCount];
                for(int i=0;i<intCount;i++)
                        intNewArray = intOldArray;
                return intNewArray;
        }
       
}


__________________
- In the world, I have nothing to do, just life.
只看该作者    顶部
相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问