Triangular Sums

描述

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

Write a program to compute the weighted sum of triangular numbers:

W(n) = SUM[k = 1…nk * 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
 1 import java.text.NumberFormat;
 2 import java.util.Arrays;
 3 import java.util.Scanner;
 4
 5 public class Main {
 6     public static void main(String[] args) {
 7         Scanner scanner=new Scanner(System.in);
 8         int T;
 9         int n;
10         int k;
11         int i;
12         int temp;
13         int sum;
14         int time=1;
15
16         T=scanner.nextInt();
17         while(true){
18             if(T==0)
19                 break;
20             T--;
21
22             n=scanner.nextInt();
23
24             sum=0;
25             for(k=1;k<=n;k++){
26                 temp=0;
27                 for(i=1;i<=k+1;i++)
28                     temp+=i;
29
30                 sum+=k*temp;
31             }
32             System.out.println(time+" "+n+" "+sum);
33             time++;
34         }
35     }
36 }
 
时间: 2024-08-03 05:16:27

Triangular Sums的相关文章

南阳122(Triangular Sums)

Triangular Sums 时间限制:3000 ms  |  内存限制:65535 KB 难度: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): XX XX X XX X X X Write a

POJ 3086 Triangular Sums

Triangular Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6371   Accepted: 4529 Description 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 po

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

nyoj 122-Triangular Sums (数学之读懂求和公式的迭代)

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

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using

LightOJ1125 Divisible Group Sums

Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you can choose in NCM ways. You will have to determine how many of these chosen groups have a sum, which is divisible by D. Input Input starts with an inte

洛谷P1466 集合 Subset Sums

洛谷P1466 集合 Subset Sums这题可以看成是背包问题 用空间为 1--n 的物品恰好填充总空间一半的空间 有几种方案 01 背包问题 1.注意因为两个交换一下算同一种方案,所以最终 要 f [ v ] / 2 2.要开 long long 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string&g

Codeforces 223APartial Sums 数论+组合数学

题意很简单,求不是那么好求的,k很大 要操作很多次,所以不可能直接来的,印象中解决操作比较多无非线段树 循环节 矩阵 组合数等等吧,这道题目 也就只能多画画什么 的了 就以第一个案例为主吧 , 3 1 2 3 k我们依据画的次数来自己定好了 下面的每个数表示这个位置的 数由最初的 数组num[]中多少个数加起来得到的 当k为0的时候呢,就是 1 1 1 k为1的时候呢 1 2 3 k为2的时候呢 1 3 6 那么k为3的时候 1 4 10 这里看一下 从数组下标0开始,那么其实就是 C(i +

CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforces Round 26 题意: 设定数组 y = f(x) 使得 y[i] = sum(x[j]) (0 <= j < i) 求初始数组 A0 经过多少次 f(x) 后 会有一个元素 大于 k 分析: 考虑 A0 = {1, 0, 0, 0} A1 = {1, 1, 1, 1} -> {C(