(Pat 甲)1104 Sum of Number Segments

這道題的目的是統計元素個數爲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;
}

測試結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章