JACK_C#_for循环

来源:互联网 发布:057型护卫舰数据和航速 编辑:程序博客网 时间:2024/06/08 01:49
// 作业A
            #region 1、编程将所有“水仙花数”打印出来,并打印其总个数。“水仙花数”是一个各个位立方之和等于该整数的三位数。
            int count = 0;
            for (int i = 100; i < 1000; i++) {
            double a = i / 100;
            double b = (i / 10) % 10;
            double c = i % 10;
                if(i == Math.Pow(a,3) + Math.Pow(b,3) + Math.Pow(c,3))   //可以用int强转
                {
                    ++count;
                    Console.WriteLine ("水仙花数{0}",i);
                }
            }
            Console.WriteLine ("总个数:{0}",count);
            #endregion

            #region 2、自定义一个数组并求数组中的所有元素最大值、最小值、平均值及各元素之和。
            int[] arr = {234,5,24,77,98,4,25};
            int max = arr[0];
            int min = arr[0];
            int sum = 0;
            for (int i = 0; i < arr.Length; ++i) {
                if(max < arr[i])
                    max = arr[i];
                if(min > arr[i])
                    min = arr[i];
                sum += arr[i];
            }
            Console.WriteLine ("最大值为{0},最小值为{1},平均值为{2}",max,min,sum/arr.Length);
            #endregion

            #region 3、已知abc+cba = 1333,其中a,b,c均为一位数,编程求出满足条件的a,b,c所有组合。
            for (int a = 0; a < 10; a++) {
                for (int b = 0; b < 10; b++) {
                    for (int c = 0; c < 10; c++) {
                        if (a * 100 + b * 10  + c + c * 100 + b * 10 + a == 1333){
                            Console.WriteLine ("{0}{1}{2}",a,b,c);
                        }
                    }
                }
            }
            #endregion


// 作业B
            #region 1、一个球从100m高度自由落下,每次落地后反跳回原来高度的一半,再落下,再反弹。求它在第10次落地时,共经过多少米?第10次反弹多高
            float sum = 100.0f;
            float rebound = 100.0f;
            for (int i = 1; i < 10; i++) {
                rebound = rebound / 2.0f;
                sum += rebound * 2.0f;      //第10次落地经过距离,不包括第10次反弹的距离
            }
            Console.WriteLine ("第10次落地时共经过{0}米,第10次反弹{1}",sum,rebound / 2.0f);
            #endregion
//
            #region 2、输入n,分别用*输出边长为n的实心菱形。例如:n = 3时,输出
//                           空格        星号         行数                          
//              *             2           1           1
//             ***            1           3           2
//            *****           0           5           3        
//             ***            1           3           4
//              *             2           1           5
//                          |n-i|    2n-1-|2n-2i|     i  
            Console.WriteLine ("n:");
            int n = int.Parse(Console.ReadLine());
            for (int i = 1; i < 2 * n; ++i) {
                int a = 0;
                while(a < Math.Abs(n-i))
                {
                    a++;
                    Console.Write(" ");
                }
                for (int j = 0; j < 2 * n - 1 - Math.Abs(2 * n  - 2 * i); ++j) {
                    Console.Write ("*");
                    }
                Console.WriteLine ();
                }
            #endregion
原创粉丝点击