1. 编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。
using System; namespace Text { class Program { static void main(string[] args) { float L; double S; float a, b, c, z, y; Console.WriteLine("请选择计算三角形或者长方形,三角形输入1,长方形输入2:"); char x = char.Parse(Console.ReadLine()); switch (x) { case '1': L1: Console.WriteLine("请输入三角形的三边长"); Console.WriteLine("请输入三角形的a边:"); a = float.Parse(Console.ReadLine()); Console.WriteLine("请输入三角形的b边:"); b = float.Parse(Console.ReadLine()); Console.WriteLine("请输入三角形的c边:"); c = float.Parse(Console.ReadLine()); if (a + b <= c || a + c <= b || b + c <= a) { Console.WriteLine("该三角形不成立,请重新输入:"); goto L1; } else { L = (a + b + c) / 2; S = Math.Sqrt(L * (L - a) * (L - b) * (L - c)); Console.WriteLine("该三角形周长为:{0}\n面积为:{1}", 2 * L, S); } break; case '2': Console.WriteLine("请分别输入长方形的长和宽:"); z = float.Parse(Console.ReadLine()); y = float.Parse(Console.ReadLine()); L = (z + y) * 2; S = z * y; Console.WriteLine("该长方形周长为:{0}\n面积为:{1}", L, S); break; default: Console.WriteLine("输入错误,请重新选择"); break; } Console.ReadKey(); } } }
- 编写一个控制台应用程序,可根据输入的月份判断所在季节。 、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _20153236_sy1_2
{
class Program
{
static void Main(string[] args)
{
int i;
Console.WriteLine("***********欢迎进入季节判断系统(气象划分法)***************");
while (true)
{
Console.WriteLine("菜单:");
Console.WriteLine(" 1.1月 2.2月 3.3月 ");
Console.WriteLine(" 4.4月 5.5月 6.6月 ");
Console.WriteLine(" 7.7月 8.8月 9.9月 ");
Console.WriteLine(" 10.10月 11.11月 12.12月 ");
Console.WriteLine(" 13.退出 ");
Console.WriteLine("请输入月份序号:");
i = int.Parse(Console.ReadLine());
switch (i)
{
case 3:
case 4:
case 5: Console.WriteLine("此月份在春季。。。"); break;
case 6:
case 7:
case 8: Console.WriteLine("此月份在夏季。。。"); break;
case 9:
case 10:
case 11: Console.WriteLine("此月份在秋季。。。"); break;
case 12:
case 1:
case 2: Console.WriteLine("此月份在冬季。。。"); break;
case 13: Console.WriteLine("退出成功,再见!!!"); break;
default: Console.WriteLine("输入错误!!!"); break;
}//switch结束
if (i == 13) break; //退出系统
}//while结束
}
}
}
3. 编写程序,用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两
个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至
少有多少个。
运行代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _20153236_sy1_3
{
class Program
{
static void Main(string[] args)
{
int sum=2; //鸡蛋个数总量
bool flag = false;
while (sum < int.MaxValue&&flag==false) { //循环条件:鸡蛋总量没有超出int所表示最大值,且没找到一个符合条件的
if (sum % 2 == 1 && sum % 3 == 1 && sum % 4 == 1) //满足题目条件,已找到
{
Console.Write("这篮鸡蛋至少有{0}", sum);
Console.WriteLine("个。");
flag = true;
}
else //没找到,增加鸡蛋数量
sum++;
}//while循环结束
}
}
}
4.编写程序,计算数组中奇数之和和偶数之和。
运行代码:
using System;
using System.Collections.Generic;
namespace Test_1_4
{
class Program
{
static void Main(string[] args)
{
List<string> str = new List<string>();
int len = 0;
int oddsum = 0;
int evensum = 0;
Console.WriteLine("请输入数组:");
while (true)
{
string input = Console.ReadLine();
if (input.Equals("") == false)//如果不是输入为空则增加输入记录
str.Insert(len++, input);
else
break;
}
string[][] every = new string[len][];//交叉数组,行固定,为上面得到的行数,每一行的长度不定(每行字符间以空格或其他分割)
for (int i = 0; i < len; i++)
{
every[i] = str[i].Split();// C#对空格的分割方式之一
}
for (int i = 0; i < len; i++)
{
for (int j = 0; j < every[i].Length; j++)
{
int aa;
aa = int.Parse(every[i][j]);
if ((aa % 2) == 1)
{
oddsum += aa;
}
else
{
evensum += aa;
}
}
}
Console.WriteLine("奇数之和等于:");
Console.WriteLine(oddsum);
Console.WriteLine("偶数之和等于:");
Console.WriteLine(evensum);
Console.ReadKey();
}
}
}
实现截图:
5. 编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该
列上最小。有可能数组没有鞍点)。要求:
u u 二维数组的大小、数组元素的值在运行时输入;
u u 程序有友好的提示信息。
运行代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _sy1_5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("计算矩阵鞍点(行最大,列最小)");
while (true)
{
Console.WriteLine("任意键继续,0退出");
string select = Console.ReadLine();
if (select == "0")
{
Console.WriteLine("已退出!");
break;
}
else
{
int[,] arr = new int[100, 100];//该数组
int m, n;
Console.Write("请输入二维数组的行数(小与100):");
m = int.Parse(Console.ReadLine());//矩阵行数
Console.Write("请输入二维数组的列数(小与100):");
n = int.Parse(Console.ReadLine());//矩阵列数
int i, j;
//输入该矩阵
Console.WriteLine("请输入" + m + "x" + n + "的矩阵");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arr[i, j] = int.Parse(Console.ReadLine());
//Convert.ToInt32(Console.Read());
}
}
//输出打印该矩阵
for (i = 0; i < m; i++)
{
if (i == m / 2)
{
Console.Write("A = ");
}
for (j = 0; j < n; j++)
{
if (i == m / 2)
{
Console.Write(arr[i, j] + " ");
}
else
{
Console.Write(" " + arr[i, j]);
}
}
Console.WriteLine();
}
int row_max;//先找到该行的最大值
int[] an_point = new int[100];//存储矩阵中所有鞍点
int count = 0;//鞍点个数
int temp1 = 0, temp2 = 0;//暂时保存该行最大值的位置
int[] x = new int[100];//保存鞍点的横坐标
int[] y = new int[100];//保存鞍点的纵坐标
bool flag = false;//判断是否是鞍点
for (i = 0; i < m; i++)
{
row_max = arr[i, 0];//令某行最大的等于该行第一个数
for (j = 0; j < n; j++)
{
if (row_max < arr[i, j])
{
row_max = arr[i, j];//找到该行最大值
//记录该数的位置
temp1 = i;
temp2 = j;
}
}
//找到最大值后判断该值在该列是否是最小值
for (int k = 0; k < m; k++)
{
if ((arr[k, temp2] < row_max) || (arr[k, temp2] == row_max && k != temp1))//不是最小值
{
flag = false;
break;
}
else//是最小值
{
flag = true;
}
}
//是鞍点
if (flag == true)
{
count++;//鞍点个数加一
an_point[count] = arr[temp1, temp2];//保存鞍点
//保存鞍点位置
x[count] = temp1 + 1;
y[count] = temp2 + 1;
}
}
if (count == 0)
{
Console.WriteLine("该矩阵无鞍点!");
}
else
{
Console.WriteLine("该矩阵有" + count + "个鞍点");
Console.WriteLine("分别是:");
for (int p = 1; p <= count; p++)
{
Console.Write("A[" + x[p] + "," + y[p] + "]=");
Console.WriteLine(an_point[p]);
}
}
}
}//结束
}
}
}
浙公网安备 33010602011771号