Browsed by
Tag: 趣题

各以1/2的概率输出0和1

各以1/2的概率输出0和1

在看CLRS中的第五章,发现课后的这道习题非常有意思。 题目:Suppose that you want to output 0 with probability 1/2 and 1 with probability 1/2. At your disposal is a procedure BIASED-RANDOM, that outputs either 0 or 1. It outputs 1 with some probability p and 0 with probability 1 – p, where 0 < p < 1, but you do not know what p is. Give an algorithm that uses BIASED-RANDOM as a subroutine, and returns an unbiased answer, returning 0 with probability 1/2 and 1 with probability...

Read More Read More

一个for语句的趣题

一个for语句的趣题

You should change one character to print “*” 42 times. You can replace, add or remove ONLY one character. Still i cant figured it, i’ve tried over and over again. #include main(){ int i,n = 42; for(i = 0; i < n; i--){ printf("*"); } } Answer 1. 改变循环变量符号。 #include main(){ int i,n = 42; for(i = 0; -i < n; i--){ //added a minus here printf("*"); } } 2. 改变循环条件 #include main(){ int i,n = 42; for(i = 0;...

Read More Read More