<C#任务导引教程>练习五
//27,创建一个控制台应用程序,声明两个DateTime类型的变量dt,获取系统的当前日期时间,然后使用Format格式化进行规范
using System;
class Program
{
static void Main()
{
DateTime dt = DateTime.Now;
string strDate = String.Format("{0:D}", dt);
Console.WriteLine("今天的日期是:" + strDate);
}
}
//28,创建一个控制台应用程序,声明一个string类型变量str1,并初始化为“我钟爱C#语言程序设计”,然后用copy方法复制字符串str1,并赋值给字符串str2,最后输出字符串str2
using System;
class Program
{
static void Main()
{
string str1 = "我钟爱C#语言程序设计";
string str2;
str2 = string.Copy(str1);
Console.WriteLine(str2);
}
}
//29,CopTo方法
using System;
class Program
{
static void Main()
{
string str = "我钟爱C#语言程序设计";
Console.WriteLine("原字符串:\n" + str);
char[] myChar = new char[5];
str.CopyTo(0, myChar, 0, 5);//CopyTo(int需要复制的字符的起始位置,cha[]目标字符数名,指定目标数组中的开始存放位置,int指定要复制的字符个数)
Console.WriteLine("复制的字符串:");
Console.WriteLine(myChar);
}
}
我可以给你们大概算一下有多少个模块 就单纯说些代码的 硬件部门不算
构成设定CLI ,装置维护CLI,环境设定CLI,装置信息查看CLI.快照备份CLI,数据分割CLI,资源分割CLI,性能优化CLI。。。
server, mainserver 设定server,状态监视server,性能监视server,snapshotserver,备份还原server......
还有各种外部tools,还不算OS部门和FW部门
//30,字符串的加密与解密示例
using System;
class Program
{
static void Main()
{
string list = "kczutmhsuasasahsuihsuw";
char[] str = new char[80];
int i, j;
Console.Write("请输入一小写字母串(长度小于80):");
string c = Console.ReadLine();
Console.Write("加密后成为:");
for (i = 0; i < c.Length; i++)
{
str[i] = c[i];
j = str[i] - 97;
str[i] = list[j];
Console.Write("{0}", str[i]);
}
Console.WriteLine();
}
}
//31,字符串的解密示例
using System;
class Program
{
static void Main()
{
string list = "qwertyuioplkjhgfdsazxcvbnm";
char[ ] str = new char[80];
int i, j;
Console.Write("请输入需解密的字符串:");
string c = Console.ReadLine();
Console.Write("原字符串是:");
for (i = 0; i < c.Length; i++)
{
str[i] = c[i];//
j = 0;
while (str[i] != list[j])
j++;
str[i] = (char)(j + 97);
Console.Write("{0}",str[i]);
}
Console.WriteLine();
}
}
//32,有三个字符串,要求找出其中最大者
using System;
class Program
{
static void Main()
{
Console.WriteLine("请先后输入三个字符串,每输入一个请按Enter键确认!");
string str;
Console.Write("请输入第1个字符串:");
string a = Console.ReadLine();
Console.Write("请输入第2个字符串:");
string b = Console.ReadLine();
Console.Write("请输入第3个字符串:");
string c = Console.ReadLine();
int m = String.Compare(a, b);
if (m > 0)
str = String.Copy(a);
else
str = String.Copy(b);
int n = String.Compare(c, str);
if (n > 0)
str = String.Copy(c);
Console.WriteLine("最大的字符串是:{0}", str);
}
}
//33,选择排序
using System;
class Program
{
static void Main()
{
string[ ] names = new string[5];
string max;
int i, j;
for(i=0;i<5;i++)
{
Console.Write("请输入{0}个国家的名字:",i+1);
names[i] = Console.ReadLine( );
}
for (i = 0; i < names.Length - 1; i++)
{
for(j=i+1;j<names.Length;j++)
{
int m = String.Compare(names[i], names[j]);
if (m < 0)
{
max = String.Copy(names[i]);
names[i] = String.Copy(names[j]);
names[j] = String.Copy(max);
}
}
}
Console.WriteLine("排序结果:");
for (i = 0; i < 5; i++)
{
Console.Write("{0} ", names[i]);
}
Console.WriteLine();
}
}
//34.ArrayList数组集合
using System;
using System.Collections;//ArrayList位于Collections中
class Program
{
static void Main()
{
ArrayList myAL = new ArrayList( );
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
Console.WriteLine("myAL Count: {0}", myAL.Count);//显示ArrayList的元素个数
Console.Write(" Value:");
foreach (Object obj in myAL)
Console.Write(" {0}", obj);
Console.WriteLine();
}
}
//35,输出一个表格的表头,调用方法
using System;
class Program{
static void printstar()
{
Console.WriteLine("*************");
}
static void print_message()
{
Console.WriteLine("* XSCJFXTJB *");
}
static void Main()
{
printstar();
print_message();
printstar();
}
}
//36,方法返回值,return语句的一半格式
using System;
class MainClass
{
static int power(int x, int n)
{
int pow = 1;
while (n > 0)
{
n--;
pow *= x;
}
return pow;
}
static void Main()
{
int n = 3;
int x = 4;
char c = 'a';
Console.WriteLine("power({0},{1},{2})", x, n, power(x, n));
Console.WriteLine("power('{0}',{1})={2}", c, n, power(c, n));
Console.WriteLine("power({0},{1}))={2}", n,x, power(n,x));
}
}
//37,输入两个整数,通过方法的调用输出其中较大的数,求两个整数中的较大数,用方法完成
using System;
class Program
{
static int max(int x, int y)
{
return (x >= y) ? x : y;
}
static void Main()
{
Console.WriteLine("请输入两个整数:");
Console.Write("a= ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("b= ");
int b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0:D}和{1:D}较大的数是:{2:D}",a,b,max(a,b));
}
}
//38,试编程实现方法的嵌套调用
using System;
class Program
{
static void prnline( )
{
Console.Write(" - ");
}
static void print()
{
Console.Write(" * ");
prnline();
return;
}
static void Main()
{
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
print();
}
Console.WriteLine( );
}
}
}
//39,阶乘
using System;
class MainClass
{
static int fac(int n)
{
int y;
if (n == 1)
y = 1;
else
y = n * fac(n - 1);
return y;
}
static void Main()
{
Console.WriteLine("4!={0}",fac(4));
}
}
//40,n+n^2+n^3+---n^n
//n(1+n(1+n(1+n----)))
using System;
class MainClass
{
static int fun(int n, int m)
{
if (m == 1)
return n;
else
return n * (1 + fun(n, m - 1));
}
static void Main()
{
Console.WriteLine("n=1:{0}", fun(1, 10));
Console.WriteLine("n=2:{0}", fun(2, 10));
Console.WriteLine("n=3:{0}", fun(3, 10));
}
}
//41,汉诺塔问题
using System;
class Program
{
static void hanoi(int n, char A, char B, char C)
{
if (n == 1)
Console.WriteLine("将第{0}个盘子从{1}柱搬到{2}柱上;", n, A, C);
else
{
hanoi(n-1,A,C,B);
Console.WriteLine("将第{0}个盘子从{1}柱搬到{2}柱上",n,A,C);
hanoi(n-1,B,A,C);
}
}
static void Main()
{
char A = 'A', B = 'B', C = 'C';
Console.WriteLine("请输入A柱上的盘子总数:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("当A柱上有{0}个盘子时,移动步骤依次为:", n);
hanoi(n, A, B, C);
}
}
//42,引用参数ref使用示例
using System;
class Program
{
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
static void Main()
{
int i = 1, j = 2;
Swap(ref i,ref j);
Console.WriteLine("i={0},j={1}", i, j);
}
}
//43,输出参数out使用示例
using System;
class Program
{
static void Divide(int x, int y, out int result, out int reminder)
{
result = x / y;
reminder = x % y;
}
static void Main()
{
int res, rem;
Divide(10, 3, out res, out rem);
Console.WriteLine("res={0} rem={1}", res, rem);
}
}
//44.用引用参数ref实现对8名学生各两门成绩的统计,求每门成绩的最低分
using System;
class Program
{
static void Main()
{
int[,] score = { { 98, 82 },{80,76}, { 96, 80 }, { 88, 90 }, { 98, 87 }, { 75, 84 }, { 68, 92 }, { 83, 66 } };
int min1 = score[0, 0];
int min2 = score[0, 1];
minmum(score, ref min1, ref min2);
Console.WriteLine("课程1最低分:" + min1 + "\n课程2最低分" + min2);
}
static void minmum(int[,] score, ref int min1, ref int min2)
{
for (int i = 1; i < score.GetLength(0); i++)
if (score[i, 0] < min1)
min1 = score[i, 0];
for (int i = 1; i < score.GetLength(0); i++)
if (score[i, 1] < min2)
min2 = score[i,1];
}
}
//45,用输出参数out实现对8名学生各两门成绩的统计,求每门的最高分
using System;
class Program
{
static void Main()
{
int[ , ]score={{98,82},{80,76},{96,80},{88,90},{98,87},{75,84},{68,92},{83,66}};
int max1, max2;
maxmum(score, out max1, out max2);
Console.WriteLine("课程1最高分:" + max1 + "\n课程2最高分:" + max2);
}
static void maxmum(int[,] score, out int max1, out int max2)
{
max1 = score[0, 0];
max2 = score[0, 1];
for (int i = 1; i < score.GetLength(0); i++)
if (score[i, 0] > max1)
max1 = score[i, 0];
for (int i = 1; i < score.GetLength(0); i++)
{
if (score[i, 1] > max2)
max2 = score[i, 1];
}
}
}
//46,定义了3个学生代表,每个学生4次测试成绩对应一个3x4的数组,求所以学生各次考试成绩中的最高成绩
using System;
class Program
{
static void Main()
{
int[,] s = new int[3, 4] { { 68, 77, 73, 86 }, { 87, 96, 78, 89 }, { 90, 70, 81, 86 } };
Console.WriteLine("最高成绩是:{0}", max(s, 3, 4));
}
static int max(int[,] g, int p, int t)
{
int m = 0;
for(int i=0;i<p;i++)
for(int j=0;j<t;j++)
if(g[i,j]>m)
m=g[i,j];
return m;
}
}
//47,输出双精度示例
using System;
class Program
{
static void Main()
{
double a = 123.456789012345;
Console.WriteLine(a);
Console.WriteLine("{0:F6}", a);
Console.WriteLine("{0:F2}", a);
}
}
//48,设置输出值得货币值与输出宽度示例
using System;
class Program
{
static void Main()
{
int a = 80;
int b = 8000;
Console.WriteLine("{0:C2}", a);
Console.WriteLine("{0:C2}", b);
Console.WriteLine("{0,5}", a);
Console.WriteLine("{0,6}", b);
}
}
//49,利用控制符完成两位小数输出
using System;
class Program
{
static void Main()
{
float x = 168.56f;
Console.WriteLine("每件平均单价为:{0:F2}", x / 3);
Console.WriteLine("每件平均单价为:{0:C2}", x / 3);
}
}
//50,由键盘输入一个华氏温度,将其转化为摄氏温度
using System;
class Program
{
static void Main()
{
Console.Write("请输入华氏温度:");
String s = Console.ReadLine();
float f = float.Parse(s);
float c=(f-32)/1.8f;
Console.WriteLine("相应的摄氏温度是:{0:F1}", c);
}
}
//51,由键盘输入三角形的三条边a,b,c,试编程用海伦公式计算任意三角形的面积
using System;
class Program
{
static void Main()
{
double s, area;
Console.WriteLine("请输入三角形的三条边长:");
Console.Write("a= ");
string x = Console.ReadLine();
Console.Write("b= ");
string y = Console.ReadLine();
Console.Write("c= ");
string z= Console.ReadLine();
double a = double.Parse(x);
double b = double.Parse(y);
double c = double.Parse(z);
s = 1.0 / 2 * (a + b + c);
area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
Console.WriteLine("a={0:F2} b={0:F2} c={0:F2} s={3:F2}", a, b, c, s);
Console.WriteLine("area={0:F2}", area);
}
}
//52.设计一个方法,根据三角形的三边长求面积,如果不能构成三角形,给出提示信息
using System;
class Program
{
static double TriangleArea(float a, float b, float c)
{
if ((a + b <= c) || (a + c <= b) || (b + c) <= a)
return -1;
double s = (a + b + c) / 2.0;
return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}
static void Main()
{
Console.WriteLine("请输入a,b,c:");
Console.Write("a= ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("b= ");
int b = Convert.ToInt32(Console.ReadLine());
Console.Write("c= ");
int c = Convert.ToInt32(Console.ReadLine());
double area = TriangleArea(a, b, c);
if(area==-1)
Console.WriteLine("{0},{1},{2}不能构成三角形!",a,b,c);
else
Console.WriteLine("边长为{0},{1},{2}三角形的面积:{3:F1}",a,b,c,area);
}
}
//53,用方法求两个不等长的数组元素的平均值
using System;
class Program
{
static void Main()
{
double[] a = new double[5] { 2, 3, 6, 8, 10 };
double[] b = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double aver1, aver2;
aver1 = average(a, 5);
Console.WriteLine("a数组元素的平均值:{0}", aver1);
aver2 = average(b,10);
Console.WriteLine("a数组元素的平均值:{0}", aver2);
}
static double average(double[] array, int len)
{
double sum = 0;
for (int i = 0; i < len; i++)
{
sum += array[i];
}
return sum / len;
}
}
<C#任务导引教程>练习五的更多相关文章
- WCF入门教程(五)配置文件
WCF入门教程(五)配置文件 服务协定以及实现写好后,需要将相关服务公布出去,就需要HOST来承载,供客户端来调用. 承载服务有两种方式,一种通过配置文件,一种通过代码进行配置.上一章已经介绍了代码方 ...
- [分享] 史上最简单的封装教程,五分钟学会封装系统(以封装Windows 7为例)
[分享] 史上最简单的封装教程,五分钟学会封装系统(以封装Windows 7为例) 踏雁寻花 发表于 2015-8-23 23:31:28 https://www.itsk.com/thread-35 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK
Senparc.Weixin.MP SDK已经涵盖了微信6.x的所有公共API. 整个项目的源代码以及已经编译好的程序集可以在这个项目中获取到:https://github.com/JeffreySu ...
- Docker入门教程(五)Docker安全
Docker入门教程(五)Docker安全 [编者的话]DockOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第五篇,介绍了Docker的安全问题,依然是老话重谈,入门者可以通 ...
- PyCharm 教程(五)断点 调试
PyCharm 教程(五)断点 调试 PyCharm 作为IDE,断点调试是必须有的功能.否则,我们还真不如用纯编辑器写的快. [运行]和[调试]前的设置,详见前面的文章,helloword. 1,设 ...
- 无废话ExtJs 入门教程十五[员工信息表Demo:AddUser]
无废话ExtJs 入门教程十五[员工信息表Demo:AddUser] extjs技术交流,欢迎加群(201926085) 前面我们共介绍过10种表单组件,这些组件是我们在开发过程中最经常用到的,所以一 ...
- Adafruit的树莓派教程第五课:使用控制电缆
Adafruit的树莓派教程第五课:使用控制电缆 时间 2014-05-09 01:11:20 极客范 原文 http://www.geekfan.net/9095/ 主题 Raspberry PiM ...
- HMM 自学教程(五)前向算法
本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...
- Netty4.x中文教程系列(五)编解码器Codec
Netty4.x中文教程系列(五)编解码器Codec 上一篇文章详细解释了ChannelHandler的相关构架设计,版本和设计逻辑变更等等. 这篇文章主要在于讲述Handler里面的Codec,也就 ...
随机推荐
- Kubernetes全栈架构师(资源调度下)--学习笔记
目录 StatefulSet扩容缩容 StatefulSet更新策略 StatefulSet灰度发布 StatefulSet级联删除和非级联删除 守护进程服务DaemonSet DaemonSet的使 ...
- Semi-supervised semantic segmentation needs strong, varied perturbations
论文阅读: Semi-supervised semantic segmentation needs strong, varied perturbations 作者声明 版权声明:本文为博主原创文章,遵 ...
- Elasticsearch 存储成本省 60%,稿定科技干货分享
背景 稿定科技旗下稿定设计产品是一个聚焦商业设计的多场景在线设计平台,打破了软硬件间的技术限制,汇集创意内容与设计工具于一体,为不同场景下的设计需求提供优质的解决方案,满足图片.视频等全类型媒介的设计 ...
- 微服务+异步工作流+ Serverless,Netflix 决定弃用稳定运行 7 年的旧平台
作者 | Frank San Miguel 策划 | 田晓旭 2021 年,Netflix 会将大部分的工作负载从 Reloaded 转移到 Cosmos 平台.Cosmos 是一个计算平台,它将微服 ...
- 题解 [HAOI2012]道路
题目传送门 题目大意 给出一个 \(n\) 个点 \(m\) 条边的有向图,问每一条边在多少个最短路径中出现. \(n\le 1500,m\le 5000\) 思路 算我孤陋寡闻了... 很显然,我们 ...
- IEEE 754舍入的问题
写在前面 本文的舍入方法只适用于保留0位或1位小数,个人水平所限,暂时没有发现保留更多小数位的舍入的规律- IEEE 754的舍入模式 IEEE 754标准提供了2类,5种舍入模式,在默认情况下一般是 ...
- 浏览器有别_HTTP报文的回车换行
本来以为浏览器HTTP报文的生成应该是完全一致的.但最近在做一个项目的时候,发现Safari和Chrome提交同一份表单,后端的处理结果不一致.看提交结果呢,是因为Safari多了个回车.由于原项目的 ...
- ZK(ZooKeeper)分布式锁实现
点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你. 本文中案例都会在上传到git上,请放心浏览 git地址:https://github.com/muxiaonong/Zo ...
- ab矩阵(实对称矩阵)
今天在做题时巧遇了很多此类型的矩阵,出于更快解,对此进行学习.(感谢up主线帒杨) 1.认识ab矩阵 形如:主对角线元素都是a,其余元素都是b,我们称之为ab矩阵(默认涉及即为n×n阶) 2.求|A| ...
- [CSP-S 2021] 廊桥分配 题解
写篇题解来纪念我炸掉的CSP 唯一会做的题代码写挂了(痛苦面具 思路 我看到这道题第一眼想到的是线段树,感觉可以用线段树维护飞机入站到出战的这段时间,想了半天想不到代码怎么写. 国内机场与国外机场要分 ...