您的位置:首页 > 其它

SPOJ15710 Iterated sums

2016-04-17 10:31 323 查看
Please compute the sum of squares for the given numbers: a, a+1, ...,
b-1, b.

Input

Two numbers: a and b separated by space, where 1 <= a <=
b <=100.

Output

Computed sum: a*a + (a+1)*(a+1)+ ... +(b-1)*(b-1) +b*b

Example

Input:
1 4

Output:
30

Example 2

Input:
5 6

Output:
61


简单,直接贴代码

代码如下:

<?php
$debug = false;

$file = STDIN;
if ($debug)
{
$file = fopen('./spoj.txt', 'r');
}

while (($line = fgets($file)) !== false)
{
$line = trim($line);
if (empty($line)) break;
sscanf($line, "%d%d", $a, $b);
$sum = 0;
for ($i = $a; $i <= $b; $i++)
{
$sum += $i * $i;
}
echo $sum.PHP_EOL;

}
if ($debug) fclose($file);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: