Poor lift Iteration while

Source: Internet
Author: User

The For Loop has two classes:

exhaustive: go through all the possible situations and use the IF condition to filter out conditions that satisfy the condition.

Example: 1. The unit to send a 150 yuan shopping card, take to the supermarket to buy three types of washing supplies. Shampoo 15 yuan, soap 2 yuan, toothbrush 5 yuan. Please just spend 150 yuan, how many kinds of buy law, no kind of buying method are all buy a few samples?

Shampoo x 10
Toothbrush y 30
Soap Z 75
int ci = 0;
int Biao = 0;
for (int x = 0; x <= × x + +)
{
for (int y = 0; y <=; y++)
{
for (int z = 0; z <=; z++)
{
ci++;
if (x + y * 5 + z * 2 = = 150)
{
biao++;
Console.WriteLine ("{0} Buy Method: Shampoo {1} bottle, toothbrush {2} branch, soap {3} block. ", Biao, X, Y, z);
}
}
}
}
Console.WriteLine ("There are a total of {0} buying methods.") ", Biao);
Console.WriteLine (CI);
Console.ReadLine ();

2 hundred Chicken Hundred Money: The Rooster 2 text Money One, the hen 1 text money One, the chicken half article money one, altogether only 100 text money, how in the case to gather enough 100 chickens to just spend 100 money?

int n = 0;
for (int g = 0; G * 2 <=; g++)
{
for (int m = 0; M <=; m++)
{
for (int x = 0; x * 0.5 <=)
{
if (G * 2 + M + x * 0.5 = = && g + m + x = = 100)
{
Console.WriteLine ("{0} species: {1} only cocks, {2} only hens, {3} chickens only", N, G, M, X);
n++;
}
}
}
}
Console.WriteLine ("There is a total of {0} possibilities", n);
Console.ReadLine ();

3. Big horse Camel 2 stone grain, medium llama 1 stone grain, two pony 1 stone grain, want to use 100 horse, camel 100 stone grain, how to distribute?

4. There are 1 cents, 2 cents, 5 cents coins, to be combined out of 2 cents, there are several combinations, how many each?

iteration: from the initial situation according to the law constantly solve the intermediate situation , the final derivation of the results.

Example: 1. Five children lined up and asked how big the first one was, the first one was two years older than the second, asked how big the second one was, and the second said it was two years older than the third one ... And so on, asked the 5th child, said that he was 3 years old.

Q How old is the first child?

int n = 1;
int a = 3;
while (n < 5)
{
A + = 2;
n++;
}
Console.WriteLine ("The first child {0} years old", a);
Console.ReadLine ();

2. Paper can be folded in an infinite number of times, the paper thickness of 0.07 mm. How many times do I have to fold at least 8848?

Double height = 0.07;//8848m=8848000
int ci = 0;
while (height <= 8848000)
{
ci++;
Height *= 2;//height=height*2;
}
Console.WriteLine (CI);
Console.ReadLine ();

While loop

It's actually a variant of the For loop.
for (int i = 1; i<=5;i++)
{
Circulation body;
}

The For loop above can be written as
int i= 1;
for (;i<=5;)
{
Circulation body;
i++;
}

Written while is the following style
int i= 1;
while (expression (i<=5))
{
Circulation body;
State change (i++);
}

Do
{
Circulation body;
State change (i++);
}while (expression (i<=5))
Note: The Do-while is a full-unsatisfied expression, and I'll do it again .

Jump statement:
break: Jump out of the loop
continue: Jump out of this cycle and continue the next cycle .

Poor lift Iteration while

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.