111,093
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T392514995
{
class Program
{
static double[] VectorAdd(double[] v1, double[] v2)
{
if (v1 == null || v2 == null)
throw new NullReferenceException("向量数组为空");
if (v1.Length == 0 || v1.Length != v2.Length)
throw new ArgumentException("两个向量长度不相等或者长度为0");
return v1.Zip(v2, (x, y) => x + y).ToArray();
}
static double Product(double[] v1, double[] v2)
{
if (v1 == null || v2 == null)
throw new NullReferenceException("向量数组为空");
if (v1.Length == 0 || v1.Length != v2.Length)
throw new ArgumentException("两个向量长度不相等或者长度为0");
return v1.Zip(v2, (x, y) => x * y).Sum();
}
static void Main(string[] args)
{
double[] v1 = { 1.2, 3.4, 2.1 };
double[] v2 = { 3.5, -1.7, 0 };
Console.WriteLine("向量1:({0})", string.Join(",", v1.Select(x => x.ToString())));
Console.WriteLine("向量2:({0})", string.Join(",", v2.Select(x => x.ToString())));
Console.WriteLine("向量相加:({0})", string.Join(",", VectorAdd(v1, v2).Select(x => x.ToString())));
Console.WriteLine("点积:{0}", Product(v1, v2));
}
}
}
向量1:(1.2,3.4,2.1)
向量2:(3.5,-1.7,0)
向量相加:(4.7,1.7,2.1)
点积:-1.58
Press any key to continue . . .
根据csdn的规则,结帖后可以返还给你一半的得分,也就是你结帖后可以得到200分,可以继续提问了。