1、using System.Collections.Generic;using System.Linq;using System.Text;namespace _2_1 class Program static void Main(string args) const double PI = 3.14159; double r, perimeter, area, volume; Console.Write (请输入半径:); String s = Console.ReadLine(); r = double.Parse(s); Console.WriteLine(圆的半径为=0,r); peri
2、meter = 2 * PI * r; area = PI * r * r; volume = 4 / 3 * PI * Math.Pow(r, 3);圆的周长为=0,面积为=1,perimeter ,area );球体的体积=0,volume ); Console.ReadLine(); 2-2求三角形的周长和面积namespace _2_2 double a, b, c, p, h, area; Console.Write(请输入三角形的边a: string s = Console.ReadLine(); a = double.Parse(s); Console .Write (请输入三角
3、形的边b: s = Console.ReadLine(); b = double.Parse(s); 请输入三角形的边c: c = double.Parse(s); if (a 0 & b c a + b c & a + c b & b + c a)三角形三边分别为:a=0,b=1,c=2,a,b,c); p = a + b + c; h = p / 2; area = Math.Sqrt(h * (h - a) * (h - b) * (h - c);三角形的周长=0,面积为=1,p,area); else Console.WriteLine(无法构成三角形! Console.ReadKey
4、();2-3分段函数的实现运行结果namespace _2_3_2 double x, y;请输入x: x = double.Parse(s); y = (x * x - 3 * x) / (x + 1) + 2 * Math.PI + Math.Sin(x); if(x=0) y=Math.Log(-5*x)+6*Math .Sqrt (Math .Abs (x)+Math .Pow (Math.E,4)-Math .Pow (x+1,3);方法二: else y=Math.Log(-5*x)+6*Math .Sqrt (Math .Abs (x)+Math .Pow (Math .E,4)
5、-Math .Pow (x+1,3);方法三: y=(x=0)?(x * x - 3 * x) / (x + 1) + 2 * Math.PI + Math.Sin(x):Math.Log(-5*x)+6*Math .Sqrt (Math .Abs (x)+Math .Pow (Math .E,4)-Math .Pow (x+1,3);方法四: Console .ReadKey ();2-4三个数比较大小namespace _2_4 int a, b, c, a1, b1, c1, t, Nmax, Nmin, Nmid; Random rMun = new Random(); a = rMu
6、n.Next(101); b = rMun.Next(101); c= rMun.Next(101);原始值: a1 = a; b1 = b; c1 = c; b) t = a; a = b; b = t; c) a = c; c = t; if (b t = b; b = c; Console.WriteLine(方法一)升序值:),a,b,c); a = a1; b = b1; c = c1; Nmax =Math .Max (Math .Max (a,b),c); Nmin = Math.Min(Math.Min(a, b), c); Nmid = a + b + c - Nmax -
7、Nmin; a = Nmin; b = Nmid; c = Nmax;(方法二)升序值:2-5求解一元二次方程namespace Console2_5 double a, b, c, delta, x1, x2, realPart, imagePart;请输入系数a:请输入系数b:请输入系数c: if(a=0) if(b=0) Console.WriteLine(此方程无解!此方程的解为:0,-c/b); else delta=b*b-4*a*c; if(delta x1=(-b+Math.Sqrt(delta)/(2*a); x2=(-b-Math.Sqrt(delta)/(2*a);此方程
8、有两个不等实根:0,1,x1,x2); if(delta =0)Console.WriteLine(此方程有两个相等实根:,-b/(2*a); realPart=-b/(2*a); imagePart=Math.Sqrt(-delta)/(2*a);0+1i和0-1i,realPart,imagePart);2-6switch语句实现多重分支namespace ConsoleApplication1 int i;输入一个数字(17): i = int.Parse(s); switch(i) case 1:对应的星期为:星期一 break; case 2:星期二 case 3:星期三 case
9、4:星期四 case 5:星期五 case 6:星期六 case 7:星期日 default:输入错误! break ;2-7分别使用if语句和switch语句实现多分支结构运行结果(if语句)namespace Console2_7 int c; double f = 0;请输入有固定工资收入的党员的月工资: int salary = int.Parse(Console.ReadLine(); if (salary 1500) c = 15; else c = (salary - 1) / 100; switch (c) case 0: f = 0.5 / 100 * salary; f =
10、 1.5 / 100 * salary; case 8: case 9: case 10: case 11: case 12: case 13: case 14: f = 2.0 / 100 * salary; case 15: f = 3.0 / 100 * salary;月工资=0,交纳党费=1, salary, f);法二代码:(if语句)namespace _2_7_2请输入有固定收入的党员的月工资: salary 400 &= 600) f = 1.0 / 100 * salary; 600 &= 800) f = 1.5 / 100 * salary; 800 &= 1500) f
11、 = 2.0 / 100 * salary; 1500) f = 3.0 / 100 * salary;月工资输入有误!月工资=0,交纳党费=1实验三:3-1namespace _3_1 int i, n, fac = 1; string s; n = -1; while (n 0)请输入非负整数n: n = int.Parse(s); /方法一:for循环 for (i = 1; i = n; i+) fac *= i; For循环:0!=1, n, fac); /方法二:while语句循环 i = 1; fac = 1; while (i = n) fac *= i+; while循环:
12、/方法三:do.while语句循环 do fac *= i; i+; while (i = n); do.while循环:3-2namespace _3_2 int f1 = 1, f2 = 1, f3, num = 2;0,5t0,5t, f1, f2); f3 = f1 + f2; while (f3 = 10000)0,5t, f3); num+; if (num % 5 = 0) Console.WriteLine(); f1 = f2; f2 = f3;3-3、鸡兔同笼问题namespace _3_3 int c, r;请输入总头数: int h = int.Parse(s); in
13、t f = 1; while (f % 2 != 0)请输入总脚数(必须是偶数): f = int.Parse(s);利用循环 bool solution = false; for (c = 0; c =0&c方法二:,c ,r); solution =true ;solution) Console.WriteLine(3-4利用级数和求PI运行结果:namespace Consolejishuhe3_4 float n, t,pi; int s; pi = 0; t = 1; n = 1; s = 1; while (Math.Abs(t) = Math.Pow(10, -6) pi += t; n += 2; s = -s; t = s / n; pi *= 4;pi=0, pi);3-5namespace ConsoleG_3_6
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1