C # for statement--exhaustive

Source: Internet
Author: User

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

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.