博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2924 Gauß in Elementary School(我的水题之路——n到m的连和)
阅读量:4070 次
发布时间:2019-05-25

本文共 1783 字,大约阅读时间需要 5 分钟。

Gauß in Elementary School
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7873   Accepted: 3530

Description

Johann Carl Friedrich Gauß (1777 – 1855) was one of the most important German mathematicians. For those of you who remember the Deutsche Mark, a picture of him was printed on the 10 – DM bill. In elementary school, his teacher J. G. Büttner tried to occupy the pupils by making them add up the integers from 1 to 100. The young Gauß surprised everybody by producing the correct answers (5050) within seconds.

Can you write a computer program that can compute such sums really quickly?

Given two integers n and m, you should compute the sum of all the integers from n to m. In other words, you should compute

Input

The first line contains the number of scenarios. Each scenario consists of a line containing the numbers n and m (−109 ≤ n ≤ m ≤ 109).

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print the sum of all integers from n to m. Terminate the output for the scenario with a blank line.

Sample Input

31 100-11 10-89173 938749341

Sample Output

Scenario #1:5050Scenario #2:-11Scenario #3:440625159107385260

Source

, Darmstadt, Germany
求从n到m的所有元素之和。
用__int64,套用连和公式:
sum = (n+m) * (m - n) / 2;
注意点:
1)输入输出格式%I64d。
2)n、m也要用__int64类型。
代码(1AC):
#include 
#include
#include
int main(void){ int ii, casenum; __int64 n, m, i; __int64 sum; scanf("%d", &casenum); for (ii = 1; ii <= casenum; ii++){ scanf("%I64d%I64d", &n, &m); if (n > m){ i = m; m = n; n = i; } sum = (n + m) * (m - n + 1) / 2; printf("Scenario #%d:\n%I64d\n\n", ii, sum); } return 0;}

转载地址:http://bloji.baihongyu.com/

你可能感兴趣的文章
ceph - crushmap 扩容记录
查看>>
openstack 管理二十三 - nova compute 连接 ceph 集群
查看>>
openstack 管理二十四 - ceph 与 vm 连接测试记录
查看>>
openstack 管理二十五 - rpm 方式部署 openstack(架构说明)
查看>>
openstack 管理二十六 - rpm 方式部署 openstack [mariadb]
查看>>
openstack 管理二十八 - rpm 方式部署 openstack [keystone]
查看>>
openstack 管理二十九 - rpm 方式部署 openstack [glance]
查看>>
openstack 管理三十二 - rpm 方式部署 openstack [neutron]
查看>>
openstack 管理三十一 - rpm 方式部署 openstack [nova]
查看>>
openstack 管理三十三 - rpm 方式部署 openstack [compute]
查看>>
openstack 管理三十四 - neutron dhcp agent 管理
查看>>
logstash + grok 正则语法
查看>>
bandwitdthd 监控
查看>>
rrdtool-1.4.5 compile in rhel6
查看>>
nginx 技巧
查看>>
oracle omf
查看>>
自定义 mrtg 数据
查看>>
rsync , rsync + ssh, rsync + lsyncd 多种同步方案与比较
查看>>
rhel6 网卡定义注意事项
查看>>
ceph 数据恢复检测
查看>>