Number of 1 in a binary number1 //continue to divide 22 intCount1 (intval)3 {4 intnum =0;5 while(val)6 {7 if(val%2==1)8num++;9Val/=2;Ten } One returnnum; A } - - //bit operation, 31 times the number of shifts the intCount2 (intval) - { - intnum =0; - while(val) + { -num + = va
# Include Code Is the output inconsistent when it is int? Cout
Note:
1) The numonesinbinary function is applicable to positive and negative numbers.
2) [if you have any questions here] I don't understand. The result of the unsigned number and the signed number arithmetic operation should be the unsigned number
1, the following is the general method to find the prime number of 1~n:
The general method of finding the prime number of 1~n
#include
2, for the number of prime screening method, the specific method is not elaborated, Baidu
recently in the beginning to learn Python, at first, many do not understand and many do not, today look at the parameters of the function, function parameters can be set to variable parameters, compared to defining a mutable parameter and defining a list or tuple parameter, just precede the parameter with a * #. combined with the function of finding the power of the past, suddenly think of one, is to find a variable list of all the number
[Classic Interview Questions] [Baidu] There are n points from left to right on the number axis a [0], a [1],..., A [n, number axis
Question
N points a [0], a [1],…, A [n-1], given a rope with a length of L, the rope can cover up to several points.
Train of Thought
Power Sum of natural numbers:
(1)
Recurrence of the bernuoli Number:
B0 = 1
(To satisfy the formula (1), change B1 to 1/2 after obtaining bn)
Reference: https://en.wikipedia.org/wiki/Bernoulli_number
Http://blog.csdn.net/acdreamers/article/details/38929067
Use the score
Solution 1: if the end of the binary is 1, divide it by 2 and more than 1;
Int count (int A) {int num = 0; while (a) {if (a % 2 = 1) + + num; A = A/2 ;} return num ;}
Solution 2: Division using shift operations:
Int count (int A) {int num = 0; while (a) {num + = A 0x01; A >>= 1
/**/
/*Computing: 1 ~ Method for specifying the number of factor I in N*/
# Include
Stdio. h
>
Int
Main (
Void
)
...
{ Int N, I, total = 0 ;Scanf ( " % D " , N, I ); For (; N ;) ... {N/=I;Total+ =N;} Printf ( " % D " , Total ); Return 0 ;}
Application:PS: When I
=
5
Can be used to calculate: N
!
The number
It's actually very easy to suddenly find the problem today
X = rand (1, N) to generate [0, 1] Even random books...
Y = x/sum (x )...
Today, I encountered a problem when I wrote a random simulation program;X1, x2,..., xn random number evenly distributed, xi> = 0;X1 + X2 +... xn = 1;For how to generate such a random
2010 to 11-3018: 40: 24
28. The binary value of an integer indicates the number of values in 1.Question: enter an integer to calculate the number of 1 in the Binary Expression of this integer.For example, input 10. Because the binary value is 1010 and there are two 1 s, Out
Take a 32-bit unsigned integer as an example.
1: int count (unsigned X ){2: x = x-(x> 1) 0x55555555 );3: x = (X 0x33333333) + (x> 2) 0x33333333 );4: x = (x + (x> 4) 0x0f0f0f;5: x = x + (x> 8 );6: x = x + (x> 16 );7: Return X 0x0000003f;8 :}
Here we use the bipartite method. We add two groups, and then add four groups, and then we get the sum of eight groups.
A more clever hakmemAlgorithm
Why do I always make these stupid mistakes, or my logic is not rigorous enough. Try ing ...1#include 2#include 3#include 4#include 5 using namespacestd;6typedefLong LongLL;7LL x;intN;8 intMain ()9 {Ten One while(~SCANF ("%d",N)) { A intsum=0; LL ans; - for(intI=1; i) { -scanf ("%lld",x); the if(sum==0) { -ans=x; -sum++;//easy wrong I was too stupid ... - } +
Two cases: the original order can be disrupted; the original order cannot be disrupted.
The following code is not tested, but it is just a build.
1 /************************************** * *********************************** 2> File Name: rearrangearray. CPP 3> author: zhoukang1991 4> mail: [email protected] 5> created time: thursday, August 28, 2014, 54 seconds, 6 ******************************* **************************************** */7 8 # inclu
Method 1: Enumerate all numbers from 1 to n. Each number calculates the number of 1 s. The result is obtained after the sum. The complexity is O (n * lnn)
Method 2: calculate the total number of occurrences by the
int rand7() { return rand()%7+1; } int rand10(){ int x=0; do { x=(rand7()-1)*7+rand7(); } while(x>40); return x%10+1;}
Analysis: To ensure the even distribution of rand10 () on integers 1-10, you can construct a random integer interval with a 1
From: http://blog.csdn.net/furturerock/article/details/6850634View Code
1 int rand7() 2 { 3 return rand()%7+1; 4 } 5 6 int rand10() 7 { 8 int x=0; 9 do10 {11 x=(rand7()-1)*7+rand7();12 }13 while(x>40);14 return x%10+1;15 }
Analysis: To ensure the even distribution of rand10 () on integers
There is a question in both
Both of these two books provide two algorithms. I think the algorithms in
Method 1:
An intuitive method is to traverse every integer from 1 to n and use a sub-function to find the number of 1 contained in an integer, then, the number of times th
I used to take a written test. One of the questions in the test was to calculate the number of times that 1 appeared within 40 billion. At that time, I only gave the idea. The specific implementation was not written. Now I will list the specific implementation code. The following code can only calculate the number less than the N power of 10, instead of the x * 1
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.