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();
}
}
}
}
}