[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 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...
随机推荐
- [luogu4860][Roy&October之取石子II]
题目链接 思路 这个题和上个题类似,仔细推一下就知道这个题是判断是否是4的倍数 代码 #include<cstdio> #include<iostream> #define f ...
- T4 反射实体模型生成代码(Demo)
1.新建一个T4 Script <#@ template language="C#" debug="True" #> <#@ output ...
- django(六)之ORM数据库操作
https://www.cnblogs.com/haiyan123/p/7732190.html 一.ORM介绍 ORM——object relation mapping 映射关系: 表名 ----- ...
- Python基础-元组、列表、字典
元组tuple 元组被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组.例:(1,2,3)("a","b","c&q ...
- Redis在python中的使用
一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- eclipse添加market ,maven
添加market 转载自http://blog.csdn.net/buptdavid/article/details/42423247 Eclipse Marketplace是个插件应用商店,很实用的 ...
- springcloud的finchley.RC2的bug
https://blog.csdn.net/qq_14809913/article/details/80606772 https://www.cnblogs.com/Little-tree/p/916 ...
- (链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 如何在Mac上安全彻底的卸载软件?
文章来源:知乎 收录于:风云社区(SCOEE)[提供mac软件下载] 更多专题,可关注小编[磨人的小妖精],查看我的文章,也可上[风云社区 SCOEE],查找和下载相关软件资源. (一)综合类: 新买 ...
- Redis分布式锁----悲观锁实现,以秒杀系统为例
摘要:本文要实现的是一种使用redis来实现分布式锁. 1.分布式锁 分布式锁在是一种用来安全访问分式式机器上变量的安全方案,一般用在全局id生成,秒杀系统,全局变量共享.分布式事务等.一般会有两种实 ...