- Enumeration algorithm:
hundred dollars to buy chicken
Ì example of enumeration algorithm: The problem is as follows: Some 3 people have 100 yuan to buy 100 chickens, of which the Rooster is 5 yuan each, the hen for 3 yuan each, the small chicken is 3 only 1 dollars, ask how many cocks can buy, hen, chicken?
int x, y, Z;
for (x = 0; x <=)
for (y = 0; y <=; y++)
for (z = 0; z <=; z++)
if ((x + y + z = =) && (5 * x + y * 3 + Z/3 = +) && (z% 3 = = 0))
Console.WriteLine ("Rooster |:{0} only, female chickens: {1} only, chickens: {2} only", X, Y, z);
The number of executions performed by the above operation is 21*34*101=71114
The idea of optimizing the problem is as follows:
The problem, because three kinds of chickens and is fixed, so long as the enumeration of the two chickens (x, y), the third chicken can be based on the agreed conditions (Z=100-X-Y), thus narrowing the scope of the enumeration into a double loop. Z is chosen because of the large value of z and the better effect of the optimization, the number of cycles at this time becomes: 21*34=714.
int x, y, Z;
for (x = 0; x <=)
for (y = 0; y <=; y++)
{
Z=100-x-y;
if ((x + y + z = =) && (5 * x + y * 3 + Z/3 = +) && (z% 3 = = 0))
Console.WriteLine ("Rooster |:{0} only, female chickens: {1} only, chickens: {2} only", X, Y, z);
}
Further optimization of the problem:
If we consider the further optimization of the enumeration algorithm from the point of view of mathematics, the efficiency of the program will be further optimized.
According to test instructions: The constraint is: 5x+3y+z/3=100;x+y+z=100; to go to a Z to get 7x+9y=100;x+y+z=100, so as long as the X (up to 14), the value of y,z can be determined naturally.
Calculates the number of occurrences of digit X in 1 to N, where N〉=9,x is 0-9
Method 1: Use the enumeration algorithm to separate the numbers on each of the data in the 1-n range, see if it is X, and then count.
int cnt = 0, I, K, n, X;
n = Convert.ToInt32 (Console.ReadLine ());
x = Convert.ToInt32 (Console.ReadLine ());
for (i=1;i<=n;i++)
{
K=i;
for (; k>0;k/=10)
if (k%10==x)
cnt++;
}
Console. WriteLine ("{0}", CNT);
The disadvantage of this method is that time complexity is too high, the time complexity of the number of occurrences of x in the calculation city is O (log10n), and the time complexity of x in all the numbers is O (nlog10n).
The optimal thinking of the algorithm is as follows:
Using the mathematical formula to calculate the final result directly, one time to find out the number of x in a nobody, as the occurrence of Budweiser is the number of times, in addition to get the final result. The range of x here is 1-9, because =0 does not conform to the following rules and requires a separate calculation.
The first rule is as follows:
(1) from 1 to 10, any x in their single digit has appeared 1 times
(2) from 1 to 100, in their 10 digits, any x appears 10 times
(3) from 1 to 1000, in their hundreds, any x appears 100 times
And so on, from 1 to 10i, in the second digit of their left number (right number i), any x appears 10i-1 times.
Computes the number of x numbers that are included in the right-i-bit algorithm:
(1) Take the number of the left (high) of the first I, multiply 10i-1, and get the base value a.
(2) Take the first digit and calculate the correction value:
If it is greater than X, the result is a+10i-1;
If it is less than x, the result is a.
If equal to X, then I is the right (low) number, set to B, and the final result is a+b+1. The complexity of Time is
O (log10n)
The code is as follows:
int cnt = 0, I, K, n, X, C;
n = Convert.ToInt32 (Console.ReadLine ());
x = Convert.ToInt32 (Console.ReadLine ());
for (i = 1; (k = n/i)! = 0; I*=10)
{
CNT + = (K/10) * i;
c = k% 10;
if (c> k)
CNT + = i;
else if (c = = x)
CNT + = N-k * i + 1;
}
Console.WriteLine ("{0}", CNT);
2017-9-16c# Notes (enumeration algorithm, hundred dollar buy chicken)