关于C#的编程题
1:计算9+99+.....+999....99(10个)的值2:计算1+1/3+1/5+....+1/51的值,并显示出来3:从键盘输入n个正整数求其平均值...
1:计算9+99+.....+999....99(10个)的值
2:计算1+1/3+1/5+....+1/51的值,并显示出来
3:从键盘输入n个正整数求其平均值 展开
2:计算1+1/3+1/5+....+1/51的值,并显示出来
3:从键盘输入n个正整数求其平均值 展开
1个回答
展开全部
static void Main(string[] args)
{
Console.WriteLine(9L + 99L + 999L + 9999L +
99999L + 999999L + 9999999L +
99999999L + 999999999L + 9999999999L);
}
第二题
static void Main(string[] args)
{
double sum = 0;
for (double i = 1; i <= 51; i += 2)
{
sum += 1 / i;
}
Console.WriteLine(sum);
}
第三题
static void Main(string[] args)
{
int[] inputs = ReadInputValues();
double avgVal = Avg(inputs);
Console.WriteLine(avgVal);
Console.Read();
}
static int[] ReadInputValues()
{
List<int> res = new List<int>();
do
{
string line = Console.ReadLine();
int val = 0;
if (!string.IsNullOrEmpty(line) && int.TryParse(line, out val))
{
res.Add(val);
}
else
{
break;
}
}
while (true);
return res.ToArray();
}
static double Avg(int[] vals)
{
double sum = 0;
for (int i = 0; i < vals.Length; i++)
{
sum += vals[i];
}
return sum / vals.Length;
}
{
Console.WriteLine(9L + 99L + 999L + 9999L +
99999L + 999999L + 9999999L +
99999999L + 999999999L + 9999999999L);
}
第二题
static void Main(string[] args)
{
double sum = 0;
for (double i = 1; i <= 51; i += 2)
{
sum += 1 / i;
}
Console.WriteLine(sum);
}
第三题
static void Main(string[] args)
{
int[] inputs = ReadInputValues();
double avgVal = Avg(inputs);
Console.WriteLine(avgVal);
Console.Read();
}
static int[] ReadInputValues()
{
List<int> res = new List<int>();
do
{
string line = Console.ReadLine();
int val = 0;
if (!string.IsNullOrEmpty(line) && int.TryParse(line, out val))
{
res.Add(val);
}
else
{
break;
}
}
while (true);
return res.ToArray();
}
static double Avg(int[] vals)
{
double sum = 0;
for (int i = 0; i < vals.Length; i++)
{
sum += vals[i];
}
return sum / vals.Length;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询