if和for的案例

  /*香皂2 牙刷5 洗发水15 共100买法?

            for (int i = 1; i < 7;i++ )

            {

                for (int m = 1; m <20; m++)

                {

                    for (int n= 1; n< 50; n++)

                    {

                        if(i*15+m*5+n*2==100)

                        {

                           // Console.WriteLine("洗发水"+  i + "牙刷" + m + "香皂" + n);

                            //   Console.WriteLine("洗发水{0}牙刷{1}香皂{2}",i,m,n);

                           Console.WriteLine("香皂{2}洗发水{0}牙刷{1}",i,m,n); 按前边的顺序显示,赋值看中括号的顺序i给0,m给1,n给2

                        }

            

                    }

                }

 

 

            }

            */

 

 

 

 

 

 

 

            /*买鸡 公鸡2元母鸡一元小鸡5角 共一百元买法?

 

            for (int i = 1; i < 50; i++)

            {

                for (int m = 1; m <100; m++)

                {

                    for (int n = 2; n <200; n=n+2)

                    {

                        if (i * 2 + m * 5 + n /2 == 100)

                        {

                            Console.WriteLine("公鸡" + "母鸡" + m + "小鸡" + n);

 

                        }

                    }

                }

 

 

            }*/

 

 

 

     Abcdef    共六个人,选择某一部分人去执行任务条件如下

Ab至少去一个,bc至少去一个,cd两人中去一个,

Ad不能在一起,aef中要派两人去,若d不去那e也不去

求组合

将0代替不去,1代替去,变成算数形式

            

          

          /*  for (int a = 0; a <= 1; a++)

                for (int b = 0; b <= 1; b++)

                    for (int c = 0; c <= 1; c++)

                        for (int d = 0; d <= 1; d++)

                            for (int e = 0; e <= 1; e++)

                                for (int f = 0; f <= 1; f++)

                                {

                                    if (

                                    a + b > 0 &&

                                    a + d != 2 &&

                                        a + e + f == 2 &&

                                        b + c > 0 &&

                                        c + d == 1)

                                    {

                                        if (d == 0&&e==0)

                                        {

                                           

                                            Console.WriteLine("A" + a + "B" + b + "C" + c + "D" + d + "E" + e + "F" + f);

 

                                        }

                                        else if (d == 1&& d+e>=1)

                                        {

 

                                            Console.WriteLine("A" + a + "B" + b + "C" + c + "D" + d + "E" + e + "F" + f);

 

                                        }

 

                                    }

 

                                }*/

          

            /*

             求一百以内的质数   学习断点记录思路

            for (int i = 1; i < 100; i++)

            {

                int k = 0;

                for (int j = 2; j < i; j++)

                {

                    if (i % j == 0)

                    {

                        k++;

                    }

                }

                if (k == 0)

                {

                    Console.Write(i + " ");

                }

            }

            */

 

           

            /*

             求一到一百之间的质数

            for (int i = 1; i <=100; i++)

            {

                int k = 0;//能整除i的数值的个数

                for (int j = 1; j <= i; j++)

                {

                    if (i % j == 0)

                    {

                        k++;

                    }

                }

                if (k == 2)

                {

                    Console.Write(i + " ");

                }

            }

            */

 

 

 

            /*

             求输入一个数的! 例3!=3*2*1

            

            int s=1;

            int m = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= m;i++ )

            {

                s =s * i;

            }

            Console.WriteLine(m+"!="+s);

            */

 

 

 

 

            /*

             求输入某一数的!和   例 3!的和=3!+2!+1!

            int s = 1,n=0;

            int m = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= m; i++)

            {

                s = s * i;

                n = n + s;

            }

            Console.WriteLine(n);

 

            */ 

 

原文地址:https://www.cnblogs.com/wanwuguizong/p/3924762.html