/*
子序列的和(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 子序列的和的更多相关文章

  1. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  2. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  3. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  4. [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 ...

  5. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  6. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  7. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  9. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

  10. 51nod1134(最长递增子序列)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1134 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...

随机推荐

  1. MongoDb 命令查询所有数据库列表

    原文:http://blog.csdn.net/huxu981598436/article/details/47216493 MongoDb 命令查询所有数据库列表 CODE: > show d ...

  2. Java io概述

    内容来源:http://ifeve.com/java-io/ Java IO 概述 输入流可以理解为向内存输入,输出流可以理解为从内存输出 Java的IO包主要关注的是从原始数据源的读取以及输出原始数 ...

  3. Vue -- 双向过滤器去除html标签

    <div id="box"> <input type="text" v-model="msg | filterHtml"& ...

  4. Django 配置QQ邮箱连接

    首先要在settings.py内进行配置 # 邮件服务配置文件 EMAIL_USE_SSL = True # 邮箱服务 EMAIL_HOST = 'smtp.qq.com' # 端口号 EMAIL_P ...

  5. Freemarker 的 Eclipse 插件 安装

    clipse版本(目前最新oxygen) 如果你的eclipse版本为Oxygen "Help" ---> "Eclipse Marketplace..." ...

  6. Python3的第一个程序

    软件:Anaconda Navigator集成开发环境中的Spyder,自带Python,无需单独安装. 注意:Python对缩进和空格敏感,代码必须对齐(包括注释),否则出现错误unexpected ...

  7. (简单贪心) 发工资咯:) hdu2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. GD32 ------ 使用外部中断,中断函数需要延时才能读到真正电平

    MCU:GD32F103RCT6 中断引脚没有外界上拉电阻 中断配置如下: RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFI ...

  9. 多态(instanceof)

    多态调用的三种格式 * A:多态的定义格式: * 就是父类的引用变量指向子类对象 父类类型 变量名 = new 子类类型(); 变量名.方法名(); * B: 普通类多态定义的格式 父类 变量名 = ...

  10. 微信小程序中出现:脚本错误或者未正确调用 Page()

    错误: ​ 解决方法: //在页面当中的.js文件里面加上 Page({ })即可