15-07-03 statement-for () loop statement-exhaustive method

Source: Internet
Author: User

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 (int0; i++)            {                if20)                {                    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(intYS =0; Ys <=Ten; ys++)            {                 for(intYG =0; YG <=5; yg++)                {                     for(intFZ =0; FZ <= -; fz++ )                    {                        DoubleA = ys *5+ YG *Ten+ FZ *2; if(A = = -) {Console.WriteLine ("can buy a 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(intD =0; D <= -; d++)            {                 for(intx =0; X <= -; X + +)                {                     for(inty =0; Y <= $; y++)                    {                        DoubleA = d *2+ x + y *0.5; if(A = = -) {Console.WriteLine ("with"+ D +"big Horse, with"+ x +"Pony , with"+ y +"Young Horse"); Thread.Sleep ( -); }                    }                }            }

4, there are 1 pieces, 2 pieces, 5 pieces of money, make up 20 dollars, there are several ways to

             for(intA =0; A <= -; a++ )            {                 for(intb =0; b <=Ten; b++ )                {                     for(intc =0; C <=4; C + + )                    {                        intD = A + b *2+ c *5; if(d = = -) {Console.WriteLine ("It's going to cost 1 bucks ."+a+"Zhang, 2 bucks."+b+"Zhang, 5 bucks."+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(intA =0; A <=1; a++)            {                 for(intb =0; b <=1; b++)                {                     for(intc =0; C <=1; C++)                    {                         for(intD =0; D <=1; d++)                        {                             for(intE =0; E <=1; e++)                            {                                 for(intf =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(inta=-1; a<=1; a=a+2)            {                 for(intb=-1; b<=1; b=b+2)                {                     for(intc=-1; c<=1; c=c+2)                    {                        intD =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 method

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.