1. 程式人生 > >Sum of AP series——AP系列之和

Sum of AP series——AP系列之和

clu 找到 ima 約束 區別 系列 .cn common 小數

A series with same common difference is known as arithmetic series. The first term of series is ‘a‘ and common difference is d. The series looks like a, a + d, a + 2d, a + 3d, . . . Find the sum of series.具有相同共同差異的系列被稱為算術系列。系列的第一個術語是“ a ”,共同的區別是d該系列看起來像a + d,a + 2d,a + 3d。找到系列的總和。

Input : a = 1
        d 
= 2 n = 4 Output : 16 1 + 3 + 5 + 7 = 16 Input : a = 2.5 d = 1.5 n = 20 Output : 335

Input:
The first line consists of an integer T i.e number of test cases. The first line and only line of each test case consists of three integers a,d,n.

輸入:
第一行由整數T即測試用例數組成。每個測試用例的第一行和第一行由三個整數

a,d,n組成

Output:
Print the sum of the series. With two decimal places.

輸出:
打印系列的總和。有兩位小數。

Constraints:
1<=T<=100
1<=a,d,n<=1000

約束:
1 <= T <= 100
1 <= a,d,n <= 1000

Example:

Input:
2
1 2 4
2.5 1.5 20

Output:
16.00
335.00

其實,就是一個等差數列的求和問題,不過需要註意的是輸出是兩位小數。

下面是我的代碼實現:

#include <stdio.h>
#include 
<stdlib.h> int main() { int n,i,j; scanf("%d",&n); for(i=0;i<n;i++) { float a,d,n; scanf("%f %f %f",&a,&d,&n); printf("%.2f\n",n*a+n*(n-1)*d/2); } return 0; }

然後竟然出現了錯誤?一起來看一下:

技術分享

好的,發現了問題應該在類型轉換上面。

如有疑問,請留言。

Sum of AP series——AP系列之和