当前位置:网站首页>【C#】正序、逆序、最大值、最小值和平均值
【C#】正序、逆序、最大值、最小值和平均值
2022-07-16 13:18: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);
}
}
}
结果

边栏推荐
猜你喜欢

T-无限的路

2022 G3 boiler water treatment examination question bank and simulation examination

20220715 国内Conda不fq安装Pytorch最新版本的方法

树莓派利用OpenCV的图像跟踪、人脸识别等

协作进程【简单总结】

26个顶级开源项目,87个开放任务,阿里巴巴编程之夏2022学生报名通道开启

发论文!AI,机器学习,CV大佬科研项目招生!

层出不穷的终端设备适配需求下 未来的响应式Web设计长什么样?

Robotics XM430-W350 云台电机使用记录

The simple solution the boss wants vs. the needs the programmer understands | cartoon
随机推荐
(pytorch进阶之路五)RNN/LSTM/LSTMP/GRU
Unity game file is large, how to reduce the game file
Method to delete all child nodes of an element
协作进程【简单总结】
云原生:Docker 实践经验(四)docker上部署 redis 三主三从集群
[动态规划]DP20 计算字符串的编辑距离-中等
将c语言文件打包成exe可执行程序
(手工)【sqli-labs46、47】order by 注入、报错回显、POST注入、数字/字符型
串的概念相关及模式匹配
Introduction to replacement technology of SAP ABAP CDs view view
Py之lime:lime库的简介、安装、使用方法之详细攻略
Boost create folder
(Fibonacci sequence) use the function to output the number of Fibonacc within the specified range (PTA)
10个第一、2个第二,达摩院NLP团队在SemEval 2022的夺冠之旅
Resolved SQL_ ERROR_ INFO: “You have an error in your SQL syntax; check the manual that corresponds to your
Native input['file'] uploads the same file, and onchange problem records will not be triggered repeatedly
Dataset: detailed introduction, download and usage of white wine quality dataset
(manual) [sqli labs46, 47] order by injection, error echo, post injection, number / character type
Concept correlation and pattern matching of strings
QT4: develop "work diary" from scratch (1) -worklog project