這道題的目的是統計元素個數爲n的序列的每一個在不同序列長度的連續片段中出現的次數之和。通過舉例可以得出,若當前是第i個數,那麼其總出現次數等於i*(n+1-i)
#include<iostream>
using namespace std;
int main()
{
int n;
double t, s = 0;
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> t;//第i位的值爲t
s += t * i * (n + 1 - i);//第i位的總出現次數爲v*i*(n+1-i)
}
cout.precision(2);
cout <<fixed<< s;
return 0;
}
測試結果如下: