15-07-03 statement-for () loop statement-exhaustive method
for () Poor lifting method
Use the loop to go through all the possible situations, and then use the IF condition to filter out the results satisfying the requirements.
For example:
1. Find an even number within 100
for (int i = 0; I <=; i++)
{
if (i% 2 = = 0)
{
Console.WriteLine (i);
}
}
2, Xiao Ming unit sent 50 Yuan shopping card, he went to the supermarket to buy cleaning supplies, one is toothbrush (5 yuan), two is soap (2 yuan), three is toothpaste (10 yuan) how can just put 50 yuan spent.
for (int ys = 0; ys <=; ys++)
{
for (int yg = 0; YG <= 5; yg++)
{
for (int FZ = 0; FZ <= 25;fz++)
{
Double A = ys * 5 + YG * + FZ * 2;
if (a = = 50)
{
Console.WriteLine ("Can buy toothbrush" +ys+ "only, toothpaste" +yg+ "only, soap" +fz+ "only");
}
}
}
}
3, the Big Horse Pack 2 stone grain, the horse carries 1 stone grain, two small horse carries once the grain, must use 100 horse, carries 100 stone grain, how to deploy
for (int d = 0; d <=; d++)
{
for (int x = 0; x <=)
{
for (int y = 0; y <=; y++)
{
Double A = d * 2 + x + y * 0.5;
if (a = = 100)
{
Console.WriteLine ("with" + D + "horse, with" + x + "pony, with" + Y + "pony");
Thread.Sleep (50);
}
}
}
}
4, there are 1 pieces, 2 pieces, 5 pieces of money, make up 20 dollars, there are several ways to
for (int a = 0; a <= 20;a++)
{
for (int b = 0; b <= 10;b++)
{
for (int c = 0; c <= 4;c++)
{
int d = A + b * 2 + c * 5;
if (d = = 20)
{
Console.WriteLine ("Need 1 Yuan" +a+ "Zhang, 2 yuan" +b+ "Zhang, 5 yuan" +c+ "Zhang");
}
}
}
}
5. A reconnaissance team received an urgent task requiring a number of persons to be selected as many as possible in a, B, C, D, E and F six players, subject to the following restrictions:
Scout A and B at least one of the two men; a+b>=1
A and D cannot go together; a+d<=1
A, E and F three shall send two men; a+e+f = = 2
B and C neither go nor go; B+c!=1
C and D Two of people go to one; c+d==1
If D does not go, then E will not go. d+e==0 | | D=1
How many people should I ask for?
for (int a = 0; a <= 1; a++)
{
for (int b = 0; b <= 1; b++)
{
for (int c = 0; c <= 1; c + +)
{
for (int d = 0; d <= 1; d++)
{
for (int e = 0; e <= 1; e++)
{
for (int f = 0; f <= 1; f++)
{
if (a+b >= 1 && a+d <= 1 && a+e+f = 2 && b+c! = 1 && c+d = 1 && (d+e = = 0 || D = = 1))
{
Console.WriteLine ("A:" +a+ "\NB:" +b+ "\NC:" +c+ "\nd:" +d+ "\ne:" +e+ "\NF:" +f ");
}
}
}
}
}
}
}
6.1 () 2 () 3 () 4 = 4; Ask in parentheses I want to fill in (-or +)
for (int a=-1;a<=1;a=a+2)
{
for (int b=-1;b<=1;b=b+2)
{
for (int c=-1;c<=1;c=c+2)
{
int d = 1 + A * 2 + b * 3 + c * 4;
if (d = = 4)
{
Console.WriteLine ("1+" +a*2+ "+" +b*3+ "+" +c*4+ "= 4");
}
}
}
}
15-07-03 statement-for () loop statement-exhaustive for () the exhaustive method uses the cycle to go through the various possible situations, and then uses the if condition to filter out the results satisfying the requirements.