码迷,mamicode.com
首页 > 其他好文 > 详细

for语句应用举例140819

时间:2014-08-19 18:11:35      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:blog   http   for   2014   amp   log   line   ad   

1、求阶乘

            Console.WriteLine("输入一个数");

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

            int s = 1;

 

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

            {

                s = s * i;

            }

            Console.WriteLine("结果:" + s);

例:5!

bubuko.com,布布扣

 

 

2、求阶乘的和

            Console.WriteLine("输入一个数:");

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

            int a = 0;

 

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

            {

                int s = 1;

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

                {

                    s = s * j;

                }

                a = a + s;

            }

 

            Console.WriteLine("结果:" + a);

例:5!+4!+3!+2!+1!=

bubuko.com,布布扣

 

 

3、找出100以内质数,并求和

            int sum=0;

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

            {

                int a = 0;

 

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

                {

                    if (i % j == 0)

                    {

                        a++;

                    }

                }

                if (a == 2)

                {

                    sum = sum + i;

 

                    Console.Write(i + "\t");

                }

            }

    Console.Write("总和:"+sum);

bubuko.com,布布扣

 

 

4、100元购物券买香皂(2元)、牙刷(5元)、洗发水(15元),每样至少买一个,正好花光,求所有可能

            int n = 1;

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

            {

                for (int j = 1; 5 * j < 100; j++)

                {

                    for (int k = 1; 15 * k < 100; k++)

                    {

                        if (2 * i + 5 * j + 15 * k == 100)

                        {

                            Console.WriteLine(n);

                            Console.WriteLine("香皂:" + i);

                            Console.WriteLine("牙刷:" + j);

                            Console.WriteLine("洗发水:" + k);

                            n++;

                        }

                    }

                }

            }

 

 

5、公鸡2文,母鸡1文,小鸡半文,每种至少买一只,100文钱买100只鸡,求所有可能

            int n = 1;

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

            {

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

                {

                    for (int k = 1; 0.5 * k < 100; k++)

                    {

                        if (2 * i + j + 0.5 * k == 100 && i + j + k == 100)

                        {

                            Console.WriteLine(n);

                            Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只", i, j, k);  //"{0}",占位符,必须从0开始

 

                            n++;

                        }

                    }

                }

            }

for语句应用举例140819,布布扣,bubuko.com

for语句应用举例140819

标签:blog   http   for   2014   amp   log   line   ad   

原文地址:http://www.cnblogs.com/phantom-k/p/3922497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!