2017年6月1日 星期四

ITSA 50補 - [Problem 3] 面積逼近 - 參考答案

Difficulty: Easy
Ref: ITSA 50補 - [Problem 3] 面積逼近
/*******************************************************/
/* [Problem 3] 面積逼近                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/06/01                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 

int main()
{
 int m;
 scanf("%d", &m);
 while (m--) {
  int n;
  scanf("%d", &n);
  double k = 1.0 / n;
  printf("%.4lf\n", k * k * k * n * (n + 1) * (2 * n + 1) / 6);
 }
 return 0;
}
Debug: I/O
(1/n)*(1/n)*(1/n) + 2*(1/n)*2*(1/n)*(1/n) + 3*(1/n)*3*(1/n)*(1/n) + ... + (1/n)^5
= (1*1 + 2*2 + 3*3 + ... + n*n) * (1/n)^3 。
觀察1~n個長方形的算式時會發現答案可以利用平方和公式來解。
6
2
3
4
11
17
28
0.6250
0.5185
0.4688
0.3802
0.3633
0.3514

沒有留言:

張貼留言