c#程序设计第三章练习

例题:

 

proj3-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            x = int.Parse(Console.ReadLine());
            if (x < 0) x = -x;
            Console.WriteLine("绝对值为{0}", x);
        }
    }
}

proj3-2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            x = int.Parse(Console.ReadLine());
            if (x < 0) Console.WriteLine("绝对值为{0}", -x);
            else Console.WriteLine("绝对值为{0}", x);
        }
    }
}

proj3-3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int year, rem4, rem100, rem400;
            Console.Write("输入年份:");
            year = int.Parse(Console.ReadLine());
            rem400 = year % 400;
            rem100 = year % 100;
            rem4 = year % 4;
            if ((rem400 == 0) || ((rem4 == 0) && (rem100 != 0)))
                Console.WriteLine("{0}是闰年", year);
            else
                Console.WriteLine("{0}不是闰年", year);
        }
    }
}

proj3-4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_4
{
    class Program
    {
        static void Main(string[] args)
        {
            float x;
            Console.Write("分数:");
            x = float.Parse(Console.ReadLine());
            if (x >= 90) Console.WriteLine("等级为A");
            else if (x >= 80) Console.WriteLine("等级为B");
            else if (x >= 70) Console.WriteLine("等级为C");
            else if (x >= 60) Console.WriteLine("等级为D");
            else Console.WriteLine("等级为E");
        }
    }
}

proj3-5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_5
{
    class Program
    {
        static void Main(string[] args)
        {
            char ch;
            Console.Write("课程代号:");
            ch=(char)Console.Read();
            switch (ch)
            {
                case 'm':
                case 'M':
                case 'w':
                case 'W':
                    Console.WriteLine("8学分");
                    break;
                case 'p':
                case 'P':
                case 'c':
                case 'C':
                    Console.WriteLine("5学分");
                    break;
                case 'e':
                case 'E':
                    Console.WriteLine("6学分");
                    break;
                default:
                    Console.WriteLine("输入的课程代号不正确");
                    break;
            }
        }
    }
}

proj3-6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_6
{
    class Program
    {
        static void Main(string[] args)
        {
            int digit, num;
            Console.Write("输入一个整数:");
            num = int.Parse(Console.ReadLine());
            Console.Write("反向显示结果:");
            while (num != 0)
            {
                digit = num % 10;  //依次求个位、十位、…上的数字digit
                num = num / 10;
                Console.Write(digit);
            }
            Console.WriteLine();
        }
    }
}

proj3-7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_7
{
    class Program
    {
        static void Main(string[] args)
        {
            int digit, num;
            Console.Write("输入一个整数:");
            num = int.Parse(Console.ReadLine());
            Console.Write("反向显示结果:");
            do
            {
                digit = num % 10;
                num = num / 10;
                Console.Write(digit);
            } while (num != 0);
            Console.WriteLine();
        }
    }
}

proj3-8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_8
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            for (i = 1; i <= 9; i++)
            {
                for (j = 1; j <= i; j++)
                    Console.Write("{0}×{1}={2} ", i, j, i * j);
                Console.WriteLine();
            }
        }
    }
}

proj3-9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_9
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, i;
            bool prime = true;
            Console.Write("输入一个大于3的正整数:");
            n = int.Parse(Console.ReadLine());
            for (i = 3; i <= Math.Sqrt(n); i++)
                if (n % i == 0)
                {
                    prime = false;
                    break;
                }
            if (prime) Console.WriteLine("{0}是素数", n);
            else Console.WriteLine("{0}不是素数", n);
        }
    }
}

proj3-10

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_10
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0, n = 1;
            while (n != 0)              //循环
            {
                Console.Write("输入一个整数(以0表示结束):");
                n = int.Parse(Console.ReadLine());
                if (n < 0) continue;     //开始下一次循环
                sum += n;
            }
            Console.WriteLine("所有正数之和={0}", sum);
        }
    }
}

proj3-11

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_11
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0, n = 0;
            while (true)
            {
                sum += n * n;
                if (sum > 1000) goto end;
                n++;
            }
        end: Console.WriteLine("最大的n为:{0}", n - 1);
        }
    }
}

proj3-12

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proj3_12
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0, n = 0;
            do
            {
                sum += n * n;
                if (sum > 1000) break;
                n++;
            } while (sum < 1000);
            Console.WriteLine("最大的n为:{0}", n - 1);
        }
    }
}

编程题:

3-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exci3_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, s1 = 0, s2 = 0;
            do
            {
                n = int.Parse(Console.ReadLine());
                if (n % 2 == 1)
                    s1 += n;
                else
                    s2 += n;
            } while (n != 0);
            Console.WriteLine("奇数之和={0}", s1);
            Console.WriteLine("偶数之和={0}", s2);
        }
    }
}

 

3-2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exci3_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, i, j, s = 0;
            Console.Write("n:");
            n = int.Parse(Console.ReadLine());
            for (i = 1; i <= n; i++)
                for (j = 1; j <= i; j++)
                    s += j;
            Console.WriteLine("s={0}", s);
        }
    }
}

 

3-3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exci3_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, c, n;
            Console.Write("n:");
            n = int.Parse(Console.ReadLine());
            if (n > 13)
                Console.WriteLine("输入的数值太大!");
            else
            {
                for (i = 0; i <= n - 1; i++)
                {
                    for (j = 1; j < 15 - i; j++)
                        Console.Write("  ");          //每次循环显示2个空格
                    c = 1;
                    Console.Write("{0}   ", c);
                    for (j = 1; j <= i; j++)
                    {
                        c = c * (i - j + 1) / j;
                        if (c < 100)
                            if (c < 10)
                                Console.Write("{0}   ", c);  //显示3个空格
                            else
                                Console.Write("{0}  ", c);   //显示2个空格
                        else
                            Console.Write("{0} ", c);         //显示1个空格
                    }
                    Console.WriteLine();
                }
            }
        }
    }
}

3-4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exci3_4
{
    class Program
    {
        static void Main(string[] args)
        {
            double pi = 0.0;
            int i;
            for (i = 1; i <= 2000; i++)
                if (i % 2 == 1)
                    pi = pi + 1.0 / (2 * i - 1);
                else
                    pi = pi - 1.0 / (2 * i - 1);
            pi = 4 * pi;
            Console.WriteLine("π={0}", pi);
        }
    }
}

实验题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace experment3
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, a, b, c;
            for (i = 100; i <= 999; i++)
            {
                n = i;
                c = n % 10; n = n / 10;
                b = n % 10; n = n / 10;
                a = n;
                if (a * a * a + b * b * b + c * c * c == i)
                {
                    Console.WriteLine("{0}的立方+{1}的立方+{2}的立方 = {3}", a, b, c, a * a * a + b * b * b + c * c * c);
                    //Console.Write("{0} ", i);
                }
            }
            Console.WriteLine();
        }
    }
}

 

posted @ 2020-09-28 09:03  ~fyd~  阅读(121)  评论(0编辑  收藏  举报