当前位置:网站首页>【C#】正序、逆序、最大值、最小值和平均值
【C#】正序、逆序、最大值、最小值和平均值
2022-07-27 17:13:00 【华为云】
正序、逆序
输入用空格分隔的五个整数,分别输出正序、逆序、平均值和最大值。
首先输入整数
将长度为5,即可以输入五个整数,并用空格隔开
int[] myArray = new int[5]; string[] str =
Console.ReadLine().Split(’ ');
输出正序
Array.Sort(myArray);
Console.Write(“正序:”);
for (int i = 0; i < 5; i++)
{
Console.Write(myArray[i]);
if (i != 4) Console.Write(",");
sum += myArray[i];
}
输出逆序
Array.Reverse(myArray);
Console.Write(“逆序:”);
for (int i = 0; i < 5; i++)
{
Console.Write(myArray[i]);
if (i != 4) Console.Write(",");
}
输出平均数
double sum = 0;
double average;
for (int i = 0; i < 5; i++)
{sum += myArray[i]; } average = sum / 5; string strAverage = average.ToString("0.0");
输出最大值
int max; Array.Sort(myArray); max = myArray[4];
代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp2{ class Program { static void Main(string[] args) { Console.WriteLine("请输入用空格分隔的5个整数:"); int[] myArray = new int[5]; string[] str = Console.ReadLine().Split(' '); try { for (int i = 0; i < 5; i++) { myArray[i] = int.Parse(str[i]); } } catch { Console.WriteLine("输入错误!"); return; } double sum = 0; double average; int max; Array.Sort(myArray); Console.Write("正序:"); for (int i = 0; i < 5; i++) { Console.Write(myArray[i]); if (i != 4) Console.Write(","); sum += myArray[i]; } max = myArray[4]; average = sum / 5; string strAverage = average.ToString("0.0"); Console.WriteLine(); Array.Reverse(myArray); Console.Write("逆序:"); for (int i = 0; i < 5; i++) { Console.Write(myArray[i]); if (i != 4) Console.Write(","); } Console.WriteLine(); Console.WriteLine("平均值:" + strAverage); Console.WriteLine("最大值:" + max); Console.ReadKey(); } }}运行结果

最大值、最小值和平均值
代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp5{ class Program { static void Main(string[] args) { double j = 1; int[] arr = new int[100]; int sum = 0, maxl = 0, minl = 0; Console.WriteLine("请输入数字,输入-1结束输入"); arr[0] = Int32.Parse(Console.ReadLine()); sum += arr[0]; maxl = arr[0]; minl = arr[0]; for (int i = 1; i < 100; i++) { arr[i] = Int32.Parse(Console.ReadLine()); if (arr[i] == -1) { break; } sum += arr[i]; maxl = Math.Max(maxl, arr[i]); minl = Math.Min(minl, arr[i]); ++j; } double ave = (double)sum / j; Console.WriteLine("最大值"+maxl); Console.WriteLine("最小值"+minl); Console.WriteLine("平均值"+ave); } }}结果

边栏推荐
- 链表~~~
- ContextMenu (context menu)
- RadioGroup(单选框)
- 【深度学习基础知识 - 43】优势比的概念
- 归一化(Normalization)和标准化(Standardization)
- Ericsson admitted bribery in China and other five countries and paid a fine of $1.06 billion to the United States
- 强化学习介绍
- Arrayadapter (array adapter) and simpleadapter (simple adapter)
- Unity-FairyGUI播放视频(Lua)
- GestureDetector(手势识别)
猜你喜欢
![[basic knowledge of deep learning - 42] detailed explanation of logistic regression](/img/74/d7d1562ada4671864961721b9a1baf.png)
[basic knowledge of deep learning - 42] detailed explanation of logistic regression

Application pool has been disabled

静态试验。2021.01 .13

GestureOverlayView(手势识别2)

Marqueetextview (running lantern)

Gesturedetector (gesture recognition)

An unknown fastcgi error occurred in IIS: 0x80070005

JS 事件监听 鼠标 键盘 表单 页面 onclick onkeydown onChange

Intent(有无返回值得跳转)

SQlife(数据库)
随机推荐
四大组件之ContentProvider
Remember an unprepared internship interview
vlan试验2021.1.14
JS 寻找所有节点sibling childNodes children
Oracle 简单的高级查询
Oracle XE版安装与用户操作
pytorch乘法以及广播机制
10.31静态路由的扩展配置
VALN 11.9
Map和Set
Radiogroup (radio box)
Intent (whether there is return value to jump)
Intel releases horse ridge chip: 22nm process, which can control multiple qubits
全局函数
View pagoda PHP extension directory
内置模块10.18
Come to sword finger offer 03. Repeated numbers in the array
Gestureoverlayview (gesture recognition 2)
【深度学习基础知识 - 38】L1正则化和L2正则化的区别
What's new in helix QAC 2022.2, the ace code static testing tool (2)