For loop. For and then the TAB key automatically generates the following
for (int i = 0/* initial conditions */; I < length/* cycle condition */; i++/* status change */)
{
}
Exhaustive: Enumerate all the possibilities.
① I have a yuan, two yuan, five yuan, how many kinds of possibilities, combined up to 20 yuan.
Formula: 1*x+2*y+5*z=20
int a1 = 1, a2 = 2, a3 = 5, B = 20,c=0;
for (int x = 1, x <=20; x + +)//$ 1 possibility
{
for (int y = 1; y <=; y++)//2 yuan possibility
{
for (int z = 0; z <= 4; z++)//5 Yuan Possibilities
{
if (b = = A1 * x + a2 * y + A3 * z)
{
Console.WriteLine (x+ "Zhang Yuan +" +y+ "Zhang two Yuan +" +z+ "Zhang five yuan =" + "20 Yuan");
C + +;
}
}
}
}
Console.WriteLine ("A total of" +c+ ");
Console.readkey ();
Ii
Hundred Chicken Hundred article: Rooster 2, hen 1, chicken half article, each kind at least one, 100 article buys 100 chicken how many possible?
int c=0;
for (int x = 1; x * 2 <=; × x + +)//The possibility of a rooster
{
for (int y = 1; y*1 <=; y++)//The possibility of hens
{
for (int z = 1; z*0.5 <=; z++)//Chicken possibilities
{
if (x*2+y*1+z*0.5==100&&x+y+z==100)//need to buy 100 chickens, spend 100 article
{
Console.WriteLine (x + "only rooster +" + y + "only hen +" +z+ "only Chick");
C + +; Possibility
}
}
}
}
Console.WriteLine ("A common" +c+ "kind of possibility");
Console.readkey ();
C # for statement--exhaustive