For poor lift, iteration

Source: Internet
Author: User

Types of problems that loops can solve
Exhaustive, go through all the possible situations, and use the IF condition to filter out conditions that satisfy the condition.
Unit to send a 150-dollar shopping card,
Take the supermarket to buy three kinds of cleaning supplies.
Shampoo 15 yuan, soap 2 yuan, toothbrush 5 yuan.
Just spend 150 yuan, how many kinds of buying method,
How do you buy every kind of buying method?
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 ();


Iterative, from the initial situation according to the law and constantly solve the intermediate situation, the final derivation of the results.
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 ();

Hundred Chicken Hundred Money: Rooster 2 text Money one, hen 1 money one,
Chicken Half-text money, a total of only 100 cents,
How do you just spend 100 cents on a 100 chicken?

int ci = 0;
for (int a = 0; a <=; a++)
{
for (int b = 0; b <=; b++)
{
for (int c = 0; c <=; C + +)
{
if (A * 2 + b * 1 + c * 0.5 = = 100)
{
if (A + b + c = = 100)
{
ci++;
Console.WriteLine ("A total of {0} kinds of buying method: Rooster {1} only, hen {2} only, chick {3} Only", Ci, A, b, c);
}
}
}

}
}
Console.ReadLine ();


Existing 1 points, 2 points, 5 cents countless
For the composition of 1 yuan, how many combinations of ways

int Biao = 0;
for (int a = 0; a <=; a++)
{
for (int b = 0; b <=; b++)
{
for (int c = 0; c <=; C + +)
{
if (A * 1 + b * 2 + c * 5 = = 100)
{
biao++;
Console.WriteLine ("A total of {0} combinations: one cent of {1}, 2 points of {2},5 {3}", Biao, A, b, c);
}
}
}
}
Console.WriteLine (Biao);
Console.ReadLine ();


Five kids lined up and asked how big the first one was,
The first one says two years older than the second one and asks how big the second one is,
The second one says 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 a = 3;
for (int i = 5; i > 1; i--)
{
A + = 2;
}
Console.WriteLine (a);

For poor lift, iteration

Related Article

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.