C# 实验三 判断一个字符、判断三角形、千名学生、a+aa+aaa+aaaa、求数列相加、约瑟夫环

题目

《C# 实验三 判断一个字符、判断三角形、千名学生、a+aa+aaa+aaaa、求数列相加、约瑟夫环》
以下为实现代码

1 判断一个字符

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

//用户输入一个字符,判断是数字、大写字母、小写字母还是其他字符
namespace 第三章作业
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;

            label1:

            Console.WriteLine("请输入一个字符:");
            str=Console.ReadLine();

            char key=(char)0;

            //判断格式正确
            if (str.Length > 1)
            {
                Console.WriteLine("你输入的太多了");
                goto label1;
            }
            else if (str.Length == 0)
            {
                Console.WriteLine("你并没有输入");
                goto label1;
            }
            else key = str[0];

            //判断ascii
            if(key>='0'&&key<='9')
            {
                Console.WriteLine("数字");
            }
            else if (key >= 'a' && key <= 'z')
            {
                Console.WriteLine("小写字母");
            }
            else if (key >= 'A' && key <= 'Z')
            {
                Console.WriteLine("大写字母");
            }
            else 
            {
                Console.WriteLine("其他");
            }

            goto label1;
        }
    }
}

2 判断三角形

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

namespace 三角形
{
    class Program
    {
        static void Main(string[] args)
        {
            double a, b, c;

            //输入
            Console.WriteLine("请输入一条边长:");
            a = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入一条边长:");
            b = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入一条边长:");
            c = double.Parse(Console.ReadLine());

            //判断
            if (a <= 0 || b <= 0 || c <= 0)
            {
                Console.WriteLine("边长不能为0或负数");
            }
            if (a + b > c && a + c > b && b + c > a)//两边之和大于第三遍
            {
                if (a == b || b == c || a == c)
                {
                    Console.WriteLine("等腰三角形");
                    if (a * a + b * b - c * c <= 0.001 || b * b + c * c - a * a <= 0.001 || a * a + c * c - b * b <= 0.001)
                    {
                        Console.WriteLine("直角三角形");
                    }
                }
                else if (a * a + b * b == c * c || a * a == b * b + c * c || a * a + c * c == b * b)
                {
                    Console.WriteLine("直角三角形");
                }
                else Console.WriteLine("普通三角形");
            }
            else Console.WriteLine("不是三角形");

        }
    }
}

3 千名学生

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

namespace _3_千名学生
{
    class Program
    {
        static void Main(string[] args)
        {
            int total;

            Console.WriteLine("100~2000之间满足要求的数有:");
            for (total = 100; total < 2000; total++)
            {
                if (total % 5 == 2 && total % 7 == 3 && total % 3 == 1)
                {
                    Console.WriteLine(total);
                }
            }
        }
    }
}

4 a+aa+aaa+aaaa

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

namespace _4_a_aa_aaa_aaaa
{
    class Program
    {
        static void Main(string[] args)
        {
            int total = 0;
            Console.WriteLine("a+aa+aaa+aaaa计算");

            //输入重复次数
            int n;
            Console.WriteLine("请输入n");
            n = int.Parse(Console.ReadLine());

            //输入数字
            int a;
            Console.WriteLine("请输入a");
            a = int.Parse(Console.ReadLine());

            //相加
            int i;
            int j;
            int curNum = 0;
            Console.WriteLine("计算结果:");

            for (i = 0; i < n; i++)//第i个数字
            {
                curNum = a;
                for (j = 0; j < i; j++)
                {
                    curNum *= 10;
                    curNum += a;
                }
                total += curNum;
                Console.Write(curNum);
                if (i != n - 1) Console.Write(" + ");
                else if (i == n - 1) Console.Write(" = ");
            }
            Console.WriteLine(total);
        }
    }
}


5 求数列相加

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

//测试:n=6
//答案:0.383333333333333333333333
namespace _5_求数列相加
{
    class Program
    {
        static void Main(string[] args)
        {
            //输入n
            int n;
            Console.WriteLine("请输入n");
            n = int.Parse(Console.ReadLine());

            //n+1项相加
            int i;
            double up;
            double down;
            int flag = 1;
            double total = 0;

            for (i = 0; i <= n; i++)
            {
                up = (i == 0 ? 1 : i);           //分母
                down = i + 1;                    //分子
                total += (up / down) * flag;     //总数

                flag *= -1;
            }
            Console.WriteLine("total=" + total);
        }
    }
}

6 约瑟夫环

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

namespace _6_10同学报数
{
    class Program
    {
        static void Main(string[] args)
        {
            //输入总数
            int total;
            Console.WriteLine("请输入总人数:");
            total = int.Parse(Console.ReadLine());

            bool[] a = new bool[total];

            //输入从第几个开始数
            int begin;
            Console.WriteLine("从第几个人开始:");
            begin = int.Parse(Console.ReadLine());

            //数到几
            int count;
            Console.WriteLine("从1数到几:");
            count = int.Parse(Console.ReadLine());

            //全部为true
            for (int i = 0; i < total; i++)
            {
                a[i] = true;
            }

            //开始删除
            int iter = begin - 1;
            int countnum = 1;
            int left = total;
            while (left != 1)               //如果剩一个人,停止
            {
                if (a[iter] == false)
                {
                    iter++;
                    iter %= total;
                    continue;
                }

                if (countnum == 0)          //如果当前人报3,删除这个人
                {
                    a[iter] = false;
                    left--;
                    Console.Write(iter + 1 + " ");
                }

                iter++;                     //下一个人
                iter %= total;

                countnum++;                 //下一个人报的数
                countnum %= count;
            }

            //剩下的数字
            for (int i = 0; i < total; i++)
            {
                if (a[i] == true)
                {
                    Console.Write("剩下的人是:" + (i + 1) + "\n");
                    break;
                }
            }
        }
    }
}

    原文作者:约瑟夫环问题
    原文地址: https://blog.csdn.net/sinat_42483341/article/details/88727452
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞