Csharp_pta2

C#实验1-2

7-1 C# 1.6 求孪生素数

编写控制台应用程序,查找一个用户输入的正整数区间中的孪生素数(孪生素数就是指相差2的素数对)。

输入格式:

用户在一行中输入两个正整数,中间用一个空格间隔。

输出格式:

如果第二个数小于或等于第一个数,则输出“Inputting illegal characters”
如果第二个数大于第一个数,则每行输出一组两个数的闭区间中的孪生素数对,两个数之间用一个空格间隔。

输入样例:

在这里给出一组输入。例如:

3 13

输出样例:

在这里给出相应的输出。例如:

3 5
5 7
11 13

注意两点 :

  • list 需要 using System.Collections.Generic

  • 关于 split

  static void Main(string[] args)
  {
      String[] input = Console.ReadLine().Split(' ');
     foreach(var x in input)
      {
          Console.WriteLine(x);
      }
  }

stdin :
12 23 34 45 56 6552523

stdout :
12
23
34
45
56
6552523

using System;
using System.Collections.Generic;

namespace Solution
{
    class Program
    {

        static bool is_prime(int x)
        {
            if (x == 1) return false;
            for(int i = 2; i <= x / i; i++)
            {
                if (x % i == 0) return false;
            }
            return true;
        }
        static void Main(string[] args)
        {
            String[] input = Console.ReadLine().Split(' ');
            int l, r;
            l = Convert.ToInt32(input[0]);
            r = Convert.ToInt32(input[1]);
            if(r <= l)
            {
                Console.WriteLine("Inputting illegal characters");
                return;
            }
            List<int> list = new List<int>();
            for(int i = l; i <= r; i++)
            {
                if (is_prime(i))
                {
                    list.Add(i);
                }
            }
            for(int i = 0; i < list.Count - 1; i++)
            {
                int number1 = list[i];
                int number2 = list[i + 1];
                if (number2 - number1 == 2)
                {
                    Console.WriteLine(number1 + " " + number2);
                }
            }
        }
    }
}

7-2 C# 1.7 查找最高分及学生

编写控制台应用程序,实现如下功能:
1)输入学生姓名(英文字母组成)和考试成绩(推荐保存到结构体数组中)。
2)求最高分并输出对应的姓名(推荐使用foreach语句)。

输入格式:

第一行输入学生人数n
然后,每一行输入一位同学的姓名和其考试成绩,姓名和考试成绩之间用一个空格间隔。
一共输入n位同学的姓名和成绩。

输出格式:

第一行输出最高分。
第二行按用户录入信息的先后次序输出得最高分的同学姓名,不同同学姓名之间用一个空格间隔。

输入样例:

在这里给出一组输入。例如:

3
Lucy 89
Mark 70
Tom 89

输出样例:

在这里给出相应的输出。例如:

89
Lucy Tom

using System;
using System.Collections.Generic;

namespace StudentScores
{
    class Program
    {
        struct Student
        {
            public string Name;
            public int Score;
        }

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());

            Student[] students = new Student[n];

            for (int i = 0; i < n; i++)
            {
                string input = Console.ReadLine();
                int spaceIndex = input.IndexOf(' ');

                students[i].Name = input.Substring(0, spaceIndex);
                students[i].Score = Convert.ToInt32(input.Substring(spaceIndex + 1));
            }

            int maxScore = int.MinValue;
            foreach (var student in students)
            {
                if (student.Score > maxScore)
                {
                    maxScore = student.Score;
                }
            }
            Console.WriteLine(maxScore);

            List<String> res = new List<string>();

            foreach (var student in students)
            {
                if (student.Score == maxScore)
                {
                    res.Add(student.Name);
                }
            }
            for(int i = 0; i < res.Count; i++)
            {
                Console.Write(res[i]);
                if (i != res.Count - 1) Console.Write(" ");
                else Console.WriteLine();
            }
        }
    }
}

7-3 C# 1.8 体型判断

分支判断

using System;

namespace Solution
{
    class Program
    {

        static void Main(string[] args)
        {
            double h, w, t;
            h = Convert.ToDouble(Console.ReadLine());
            w = Convert.ToDouble(Console.ReadLine());
            t = w / h / h;
            if(t < 18)
            {
                Console.WriteLine("thin");
            }
            else if(t >= 18 && t < 25)
            {
                Console.WriteLine("standard");
            }
            else if(t >= 25 && t < 27)
            {
                Console.WriteLine("little fat");
            }
            else
            {
                Console.WriteLine("fat");
            }
        }
    }
}

7-4  C# 1.9 判定闰年

using System;

namespace Solution
{
    class Program
    {

        static void Main(string[] args)
        {
            int year;
            year = Convert.ToInt32(Console.ReadLine());
            if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }
}

7-5 C# 1.10 计算分段函数

与 7-3 重题

using System;

namespace Solution
{
    class Program
    {

        static void Main(string[] args)
        {
            double x;
            x = Convert.ToDouble(Console.ReadLine());
            if (x >= 0.4 && x < 1.4)
            {
                Console.WriteLine(1.0 - 2.0 * x);
            }
            else if (x >= 2.4 && x < 4.4)
            {
                Console.WriteLine(x);
            }
            else if(x >= 5.4 && x < 6.4)
            {
                Console.WriteLine(1.0 + 2.0 * x);
            }
        }
    }
}

7-6 C# 1.11 打印杨辉三角

主要在于二维数组的应用

声明 : int[][] triangle = new int[1000][];

使用之前对于每一维 : triangle[i] = new int[1000];

using System;

namespace Solution
{
    class Program
    {

        static void Main(string[] args)
        {
            int n;
            n = Convert.ToInt32(Console.ReadLine());

            int[][] triangle = new int[1000][];

            // 初始化
            triangle[1] = new int[1000];
            triangle[1][1] = 1;
            
            // 递推
            for (int i = 2; i <= n; i++)
            {
                triangle[i] = new int[1000];
                for(int j = 1; j <= i; j++)
                {
                    triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
                }
            }
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(triangle[i][j]);
                    if (j != i) Console.Write(" ");
                    else Console.WriteLine();
                }
            }
        }
    }
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
21043779_csharp_opcclient_rcw_code.zip_opc是一个C#语言编写的OPC客户端的代码包。OPC(OLE for Process Control,过程控制的OLE)是一种标准化的通信协议,用于在工业自动化系统中实现设备之间的数据交换和通信。 这个代码包包含了一个C#语言编写的OPC客户端的实现代码。在这个代码包中,我们可以看到一些主要的功能和类,用于与OPC服务器进行连接、读取数据、写入数据等操作。这些代码可以帮助开发人员快速实现自己的OPC客户端应用。 在使用这个代码包时,我们可以根据自己的需要进行一些配置和修改。首先,我们需要配置OPC服务器的相关信息,如服务器IP地址、端口号、用户名和密码等。然后,我们可以根据需要进行数据的读取和写入操作,并对读取到的数据进行相应的处理和展示。 这个代码包使用了C#语言来实现,所以在使用之前,我们需要先安装相应的开发环境和依赖库。同时,我们还需要了解一些OPC的基本概念和原理,以便更好地理解和使用这个代码包。 总而言之,21043779_csharp_opcclient_rcw_code.zip_opc是一个用于实现OPC客户端的C#代码包,可以帮助开发人员快速构建自己的OPC客户端应用。通过这个代码包,我们可以连接OPC服务器,读取和写入数据,并对数据进行相应的处理和展示。在使用之前,我们还需要进行一些配置和准备工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值