Siyang's profileSiyang的小尾羊圈PhotosBlogListsMore Tools Help

Blog


    November 06

    一日3题

    第一题。给一个数组a[1]到a[n] : 例如 1,2,3,4,5,6

    现在随机生成a的一个permutation: b[1]到b[n] (例如:3 1 5 2 4 6)

    问, a和b数组在每一位上都不相同的概率是多少?假设a本身没有重复的数

     

    我的解法:

    主问题:F(n) = 给定长度为n的a数组,b数组有多少种取法

    辅助问题:结果用f(n)表示。 b数组是{1….i-1,x,i+1…n}的一个排列,其中x!=i,满足a,b在每一位上都不相同,有多少种b?例如,a = 1,2,3,4; b是{1,2,5,4}的一个排列。换句话说,组成b的元素中,有且只有一个数不在a中。

    这样定义了F(n),f(n)后,很显然有递推关系:

    F(n) = (n-1) * f(n-1)    //解释:第一位有n-1种选择,任意一种选择后,问题变为一个 n-1规模的辅助问题

    f(n) = F(n-1) + (n-1)*f(n-1)   //情况一,在b数组的第i位置填入x,考虑剩下的n-1个位置,即是一个n-1规模的主问题;情况二,i位置填入非x的数,考虑剩下的n-1个位置,即是一个n-1规模的辅助问题。

    简化一下表达式就是:

    F(n) = (n-1)(F(n-1)+F(n-2))

     

     

    第二题,一个binary tree,逆序打印BFS序列。不能同时用两段存储空间(不同时用queue和stack)

    解法,用一个vector(array)模拟queue+stack。queue的push操作即vector的push_back,等效于q.pop()+stack.push()的操作则是,vector的index往前走一步!最后把vector从尾到头打印一遍即可。

     

     

    第三题,网上看的答案,超级巧妙,生成一个0-255 二进制数有多少位是1的查询表

    static int BitSetCount256[256] =

    {

    #define B2(n) n, n+1, n+1, n+2,

    #define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2),

    #define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2),

    B6(0), B6(1), B6(1), B6(2)

    }

    不得不说,这个宏递归的方法用的太妙了!!!

    附带赞一个巧妙度略低一些的计算二进制数有多少位1的方法

    int bitSetCount(unsigned int i){
    int c=0;

    while(i){

    c++;

    i &= (i-1);  //这一步很赞,每次保证清除最低一位1;

    }

    return c;

    }

    Comments (2)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    chenwei Wangwrote:
    第一个题目,用容斥原理就可以解,更简单。比如考长度为4的序列a,随机乱序排列后,得到的b序列有4!种等概序列。
    b和a至少有1位相同的b序列的个数为C(4,1)*3!;
    b和a至少有2位相同的b序列的个数为C(4,2)*2!;
    b和a至少有3位相同的b序列的个数为C(4,3)*1!;
    b和a至少有4位相同的b序列的个数为C(4,4)*0!;
    因此b和a每一位都不同的b序列的个数为C(4,0)*4!-C(4,1)*3!+C(4,2)*2!-C(4,3)*1!+C(4,4)*0!=9,因此b和a每位都不同的概率为9/4!。更一般的对于n长序列a,这个结果即为\sum_{k=0}^n (-1)^k*C(n,k)*(n-k)! /n!。
    4 days ago
    siwen sunwrote:
    再接再厉阿 :)
    Nov. 7

    Trackbacks

    The trackback URL for this entry is:
    http://siyangxie.spaces.live.com/blog/cns!4F9E80B98C7A107!527.trak
    Weblogs that reference this entry
    • None