[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 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...
随机推荐
- Python数据结构之栈的实现
一图胜千言,看图! 代码code: #coding:utf-8 #常见数据结构之栈的实现 class Stack(): #创建Stack类 def __init__(st,size): st.stac ...
- codeforces Hello 2019(未写完)
A. Gennady and a Card Game a题惯例签到题 题意:给你一张牌,再给你5张牌,判断能不能出一次牌... 所以只要检查第二行中的某个卡是否与第一行中的卡具有共同字符 有就输出YE ...
- bzoj2007 NOI2010 海拔(对偶图)
80分(最小割)思路 先考虑如果没有题目中东南角为\(1\)那个限制的话会怎样. 那么只要让每个点的海拔都是\(0\)就行了.这样不论怎样走,最后的答案都是0. 然后再考虑那个东南角为\(1\)的限制 ...
- (转)从一道面试题彻底搞懂hashCode与equals的作用与区别及应当注意的细节
背景:学习java的基础知识,每次回顾,总会有不同的认识.该文系转载 最近去面试了几家公司,被问到hashCode的作用,虽然回答出来了,但是自己还是对hashCode和equals的作用一知半解的, ...
- Hibernate4
内容简介:1.使用log4j的日志存储,2.一对一关系,3.二级缓存 1 整合log4j(了解) l slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是 ...
- KMP之计算Next数组
KMP的Next数组:模式串的前缀与后缀的“相交”长度 KMP算法步骤: 1.先算next数组 2.若失配(此时模式串下标为j),利用Next数组求出失配后滑动的新位置 a.Next[j] \geq ...
- Luogu P3181 [HAOI2016]找相同字符 广义$SAM$
题目链接 \(Click\) \(Here\) 设一个串\(s\)在\(A\)中出现\(cnt[s][1]\)次,在\(B\)中出现\(cnt[s][2]\)次,我们要求的就是: \[\sum cnt ...
- JDBC工具类
package com.shundong.uitl; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...
- cucumbe无法识别中文场景的问题
import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucu ...
- Go strings.Builder
Go strings.Builder 字符串拼接操作优化 最开始的时候,可能会使用如下的操作: package main func main() { ss := []string{ "sh& ...