[C++]2-4 子序列的和
/*
子序列的和(subsequence)
输入两个整数n<m<10^6,输出1/(n^2) + 1/((n+1)^2) + 1/((n+2)^2) 1/((n+3)^2) + ... + 1/((m)^2)
保留5位小数。输入包含多组数据,结束标记为n=m=0。提示:本题目有陷阱
样例输入:
2 4
65536 655360
样例输出:
Case 1:0.42361
Case 2:0.00001
*/ #include <iostream>
#include <math.h>
using namespace std; int main(){
int n, m, cases = 0;
double result = 0.0;
while(scanf("%d %d", &n, &m) == 2){
if((n == m) && ( m == 0)){
break;
} cases++;
result = 0.0;
for(int i = n; i< m; i++){
result += 1. / (long long)(pow(i, 2));//【key 1】10^6,范围;【key 2】1. 运用浮点数运算,否则会有精度损失
}
printf("Case %d: %.5f\n", cases, result);
} return 0;
}
/*
[input]
2 4
65536 655360
0 0
[output]
Case 1:0.42361
Case 2:0.00001
*/
【参考文献】
刘汝佳.《算法竞赛入门经典》
[C++]2-4 子序列的和的更多相关文章
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
- 51nod1134(最长递增子序列)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1134 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...
随机推荐
- 跟angular2学一键开启项目--关于上个react-redux项目的一键调试
一键调试类似于webpack的hot-loader,但是这个hot-loader并不怎么好用,想省事的同学可以配置一下就完了. 今天介绍browser-sync,用它来一键开启项目.它可以监听任意文件 ...
- WebService学习总结(一)——WebService的相关概念
一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...
- iview tree 获取选中子节点的整条数据链
这样子获取到数据是,checked等于true的,获取不到他的父级,父级的父级 解决办法代码如下: //需要有一个唯一ID //==================================== ...
- 解决VS2012 服务器资源管理器中的表拖不到Linq to sql中
找到C:\Program Files (x86)\Common Files\microsoft shared\Visual Database Tools\dsref80.dll 这个dll文件: 在其 ...
- tyvj/joyoi 2018 小猫爬山
2018,这个题号吼哇! 搜索第一题,巨水. WA了一次,因为忘了还原... #include <cstdio> ; int n, W, ans, weigh[N], cost[N]; i ...
- 洛谷P3168 任务查询系统
题意:有n个任务,第i个的存在时间是li~ri,有个权值.求t时刻第k大的权值. 这毒瘤...本来是前缀和 -> 主席树,我是树套树...然后光荣TLE. 其实很裸.一开始我写的是每个位置维护一 ...
- 导出为word文档
原来用freemarker就可以,真是太简便了.先设计一张文档,然后把要输出的值用freemarker取值表达式获取数据,最后保存为ftl文件,再调整一下就可以了.
- python基础-格式化时间
module datatime用strftime格式化时间import datetimedatetime.datetime.now() 返回microsecond,要修改datetime.dateti ...
- MFC调用halcon生成的cpp内容
打开VS,文件——新建——项目——Visual C++——MFC——MFC应用程序,注意下图,其他默认.窗体1个Button.1个Picture Control [VS配置Halcon] 1.若hal ...
- (链表 递归) leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...