Generate the sum probability distribution of several random numbers

Source: Internet
Author: User
Baidu intern recruitment pen questions:
 
Compile the function Foo (int x, int y, int N)
 
Calculation: X integers with the size of [1, y] are randomly generated. What is the sum of them and the probability of n?
 
Solution: use an array count [m] [k] to generate a random number for a total of m times, and calculate the number of times for K. initialize count [1] [1], count [1] [2] .. count [1] [Y] = 1
 
Random Number generated for the I time: the probability (or number of times) for the sum of I * min to I * Max needs to be changed (min = 1, max = y)
 
Count [I] [J] = count [I-1] [J-1] + Count [I-1] [J-2] .. count [I-1] [J-y + 1]
 
The two-dimensional array can be used with two one-dimensional arrays.
 // Foo. cpp: defines the entry point for the console application.  // # Include" Stdafx. h "# Include <iostream> # include <math. h> using namespace STD; Double Foo ( Int X,Int Y, Int N ){ If (N> X * Y | n <X) Return 0; Double T = POW (( Double ) Y, X ); If (N = x | n = x * Y) Return 1/T; If (X = 1) Return ( Double ) 1/y; Int Flag = 1;Double * P [2]; P [0] = New   Double [X * Y + 1]; P [1] = New   Double [X * Y + 1]; Int I; For (I = 1; I <= x * Y; I ++) {P [0] [I] = 0; P [1] [I] = 0 ;} For (I = 1; I <= y; I ++) {P [0] [I] = 1 ;} For (I = 2; I <= x; I ++ ){ For ( Int J = I; j <= I * Y; j ++) {P [flag] [J] = 0; For ( Int K = 1; k <= Y & J-k> 0; k ++) P [flag] [J] + = P [1-Flag] [J-K];} flag = 1-flag ;} // For (I = x; I <= x * Y; I ++)  // Printf ("% 2D: % F \ n", I, P [1-Flag] [I]);  Double Ret = P [1-Flag] [N]/t; Delete [] P [0]; Delete [] P [1]; Return RET ;} Int Main ( Int Argc, Char * Argv []) {printf (" % F \ n ", Foo (9, 6, 23 )); Return 0 ;}

Reference: http://zhedahht.blog.163.com/blog/static/254111742009101524946359/

 

 

2. design function. The input is a string containing Chinese characters, English letters, numbers, and other characters. It is encoded as GBK. The encoding rules for Chinese characters are assumed to be dual-byte. The height is greater than 0x80, and the low byte is arbitrary.

A) Write a function in a common language (C/C ++/PHP/Java). The function is to extract Chinese characters from the input text in order to form a new text string and return it to the caller.

B) if the caller sometimes wants to get the full Chinese content of the input string and sometimes the English content, how should the function be designed.

C) if the caller wants to obtain one or more of the three types of Characters in the input string that contain Chinese, English, and numbers, how should the function be designed.

Getchar (S, letter | digit | Chinese)

 Enum Type {Chinese = 1, letter = 2, digit = 4 }; Static Inline bool isletter ( Char C ){ Return C> = 'A' & C <= 'Z' | C> = 'A' & C <= 'Z';} string getchar (Const   Char * STR, Int Type = Chinese ){ Int Length = strlen (STR ); Char * TMP = ( Char *) Malloc (Length + 1 )* Sizeof ( Char ); String ret; Int K = 0; For ( Int I = 0; I <length; I ++ ){ If (Type> 0 & 1 ){ If (( Unsigned   Char ) STR [I]> 0x80) {TMP [k ++] = STR [I]; TMP [k ++] = STR [++ I]; Continue ;}} If (Type> 1 & 1) & isletter (STR [I]) {TMP [k ++] = STR [I]; Continue ;} If (Type> 2 & 1) & isdigit (STR [I]) {TMP [k ++] = STR [I]; Continue ;} TMP [k] = '\ 0'; ret. Assign (TMP ); Free (TMP ); Return RET ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.