nyoj 122-Triangular Sums (数学之读懂求和公式的迭代)
2024-10-06 12:53:01
122-Triangular Sums
内存限制:64MB
时间限制:3000ms
特判: No
通过数:5
提交数:7
难度:2
题目描述:
The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T(4):
X
X X
X X X
X X X X
X X
X X X
X X X X
Write a program to compute the weighted sum of triangular numbers:
W(n) = SUM[k = 1…n; k * T(k + 1)]
输入描述:
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow. Each dataset consists of a single line of input containing a single integer n, (1 ≤ n ≤300), which is the number of points on a side of the triangle.
输出描述:
For each dataset, output on a single line the dataset number (1 through N), a blank, the value of n for the dataset, a blank, and the weighted sum ,W(n), of triangular numbers for n.
样例输入:
复制
4
3
4
5
10
样例输出:
1 3 45
2 4 105
3 5 210
4 10 2145
C/C++ AC:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <climits> using namespace std;
const int MY_MAX = ;
int T[MY_MAX] = {}, W[MY_MAX] = {}, n; void cal_W()
{
T[] = , T[] = ;
for (int i = ; i < MY_MAX - ; ++ i)
{
W[i] = W[i - ] + i * T[i + ];
T[i + ] = T[i + ] + i + ;
}
} int main()
{
cal_W();
cin >>n;
for (int i = ; i <= n; ++ i)
{
int temp;
scanf("%d", &temp);
printf("%d %d %d\n", i, temp, W[temp]);
}
}
最新文章
- Animation
- 《Android深度探索HAL与驱动开发》第一章阅读心得
- 实验12:Problem C: 重载字符的加减法
- 输入的全角字符转换成半角字符--css、js、ASP.NET
- JS模块化工具requirejs教程(一):初识requirejs
- 正整数的n次方求和
- mysql中显示方式的切换
- Jersey的RESTful简单案例demo
- php header设置页面编码
- Linux指令--which,whereis,locate,find
- Spark内存管理-UnifiedMemoryManager和StaticMemoryManager
- Android 仿百合网超火爆社交app首页滑动效果
- python -yield理解
- CentOS开机提示kernel panic - not syncing: Attempted to kill init! 解决方法
- 了解一下vue源码中vue 的由来
- 使用gitbook plugin
- .net core内部分享ppt
- Linux常用命令大全(新手入门)
- 拯救U盘之——轻松修复U盘“无法访问”的故障
- 【Ubuntu】录屏软件