例7    级数求和

题目描述

已知: Sn =1+1/2+1/3+…+1/n。显然对于任意一个整数 k,当 n 足够大的时候,Sn>k。

现给出一个整数 k,要求计算出一个最小的 n,使得 Sn>k。

输入格式

一个正整数 k

输出格式

一个正整数 n

输入样例

1

输出样例

2

(1)编程思路。

用简单的循环完成多项式求和。循环控制条件为和S<=K。

(2)源程序。

#include <stdio.h>

int main()

{

int k,n;

double s;

s=0;

n=0;

scanf("%d",&k);

do {

n++;

s+=1.0/n;

}while (s<=k);

printf("%d\n",n);

return 0;

}

习题7

7-1  Deck

本题选自北大POJ题库 (http://poj.org/problem?id=1607)

Description

A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table's edge, and half the length of the card hangs over the edge of the table. If the card hung any further out, with its center of gravity off the table, it would fall off the table and flutter to the floor. The same reasoning applies if the card were placed on another card, rather than on a table.

Two playing cards can be arranged, carefully, with short edges parallel to table edges, to extend 3/4 of a card length beyond the edge of the table. The top card hangs half a card length past the edge of the bottom card. The bottom card hangs with only 1/4 of its length past the table's edge. The center of gravity of the two cards combined lies just over the edge of the table.

Three playing cards can be arranged, with short edges parallel to table edges, and each card touching at most one other card, to extend 11/12 of a card length beyond the edge of the table. The top two cards extend 3/4 of a card length beyond the edge of the bottom card, and the bottom card extends only 1/6 over the table's edge; the center of gravity of the three cards lines over the edges of the table.

If you keep stacking cards so that the edges are aligned and every card has at most one card above it and one below it, how far out can 4 cards extend over the table's edge? Or 52 cards? Or 1000 cards? Or 99999?

Input

Input contains several nonnegative integers, one to a line. No integer exceeds 99999.

Output

The standard output will contain, on successful completion of the program, a heading:

Cards Overhang

(that's two spaces between the words) and, following, a line for each input integer giving the length of the longest overhang achievable with the given number of cards, measured in cardlengths, and rounded to the nearest thousandth. The length must be expressed with at least one digit before the decimal point and exactly three digits after it. The number of cards is right-justified in column 5, and the decimal points for the lengths lie in column 12.

Sample Input

1

2

30

Sample Output

Cards  Overhang

1     0.500

2     0.750

30     1.997

(1)编程思路。

本题题意为:将纸牌沿桌子推出,第1张推出桌面1/2,第2张推出第1张1/4,第3张1/6,依次类推。求n张纸牌推出桌面的长度。

即输入正整数n,求S=1/2+1/4+…+1/(2n)的值。

简单循环处理即可。

(2)源程序。

#include <stdio.h>

int main()

{

int n,i;

double s;

printf("Cards  Overhang\n");

while (scanf("%d",&n)!=EOF)

{

s=0;

for (i=1;i<=n;i++)

s+=1.0/(2*i);

printf("%5d  %8.3lf\n",n,s);

}

return 0;

}

7-2  求π的值

题目描述

根据公式

计算π的值,要求精确到最后一项的绝对值小于10–4。

输入格式

输出格式

求得的π的值。

(1)编程思路。

若计算如下多项式

用一个简单循环即可实现。

int n=1;

double p=0.0;

while (1.0/n>=0.0001)

{

p=p+1.0/n;

n+=2;

}

但现在是加1项然后减1项交替进行,如何解决?

一个最简单的办法是用一个变量flag作为符号标志,初始时f=1,运算p=p+1.0*f/n=p+1.0/n;完成加项运算;之后 f=-f,f值为-1,运算p=p+1.0*f/n=p-1.0/n;完成减项运算;再之后 f=-f,f值为1,…。这样,通过f的值在1,-1之间切换从而完成加项与减项的交替进行。

(2)源程序。

#include <stdio.h>

int main()

{

int n=1,f=1;

double p=0.0;

while (1.0/n>=0.0001)

{

p=p+1.0*f/n;

f=-f;

n+=2;

}

printf("%f\n",4*p);

return 0;

}

7-3  u Calculate e

本题选自北大POJ题库 (http://poj.org/problem?id=1517)

Description

A simple mathematical formula for e is

e=Σ0<=i<=n1/i!

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.

Input

No input

Output

Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.

Sample Input

no input

Sample Output

n e

- -----------

0 1

1 2

2 2.5

3 2.666666667

4 2.708333333

...

(1)编程思路。

题目要求根据公式e=1+1/1!+1/2!+…+1/n!求e的值。可以用一个简单循环完成计算。设初始时,e=1.0,p=1,循环程序编写如下:

for (i=1;i<=n;i++)

{

p=p*i;

e=e+1.0/p;

}

注意:不要写成二重循环

for (e=1.0, i=1;i<=n;i++)

{

for (p=1,j=1;j<=i;j++)   // 循环求 i! 的值

p=p*i;

e=e+1.0/p;

}

因为,i!=i*(i-1)!,这样求i!时可以利用前一次求得的(i-1)!,无需每次重新求取。

(2)源程序。

#include <stdio.h>

int main()

{

int i,p;

double e;

printf("n e\n");

printf("- -----------\n");

printf("0 1\n");

e=1.0;

p=1;

for (i=1;i<=9;i++)

{

p=p*i;

e=e+1.0/p;

if (i==1) printf("%d %.0f\n",i,e);

else if (i==2) printf("%d %.1f\n",i,e);

else printf("%d %.9f\n",i,e);

}

return 0;

}

C语言程序设计100例之(7):级数求和的更多相关文章

  1. 黑马程序员——经典C语言程序设计100例

    1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...

  2. C语言程序设计100例之(22):插入排序

    例22  插入排序 问题描述 排序是计算机程序设计中的一种重要操作,它的功能是将一个数据元素或记录的任意序列,重新排列成一个以关键字递增(或递减)排列的有序序列. 排序的方法有很多,简单插入排序就是一 ...

  3. C语言程序设计100例之(9):生理周期

    例9    生理周期 问题描述 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为 23 天.28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如 ...

  4. C语言程序设计100例之(4):水仙花数

    例4    水仙花数 题目描述 一个三位整数(100-999),若各位数的立方和等于该数自身,则称其为“水仙花数”(如:153=13+53+33),找出所有的这种数. 输入格式 没有输入 输出格式 若 ...

  5. C语言程序设计100例之(6):数字反转

    例6    数字反转 题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 输入格式 ...

  6. C语言程序设计100例之(14):丑数

    例14   丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...

  7. C语言程序设计100例之(17):百灯判亮

    例17   百灯判亮 问题描述 有序号为1.2.3.….99.100的100盏灯从左至右排成一横行,且每盏灯各由一个拉线开关控制着,最初它们全呈关闭状态.有100个小朋友,第1位走过来把凡是序号为1的 ...

  8. C语言程序设计100例之(25):确定进制

    例25    确定进制 问题描述 6*9 = 42 对于十进制来说是错误的,但是对于13进制来说是正确的.即 6(13)* 9(13)= 42(13),因为,在十三进制中,42 = 4 * 13 + ...

  9. C语言程序设计100例之(11):求质数

    例11  求质数 问题描述 质数是指除了有1和自身作为约数外,不再有其他约数的数.比如:3.5.7是质数.而9不是质数,因为它还有约数3. 编写程序求给定区间中的所有质数. 输入格式 两个整数a和b, ...

随机推荐

  1. SQL Server如何通过Page_ID找到对应的表

    其实本篇文章算是翻译Finding a table name from a page ID这篇文章,只是不想直接翻译.用自己的理解叙说出来.算是对上一篇博客"SQL Server如何找出一个 ...

  2. java1.8 ConcurrentHashMap 详细理解

    基于 jdk1.8 Java并发包中提供的一个线程安全且高效的HashMap实现,可以完全替代HashTable,在并发编程的场景中使用频率非常之高.可能大多人只是知道它使用了多个锁代替HashTab ...

  3. 用dotnet core 搭建web服务器(一)http server

    环境说明 dotnet core,开发需要安装dotnetcore sdk,运行需要安装 dotnetcore runtime 运行目前几乎支持所有常见平台 开发推荐windows10 平台 首先安装 ...

  4. python __iter__和__getitem__区别

    __getitem__ 单独实现这个魔法函数,可以让这个类成为一个可迭代的对象,并且可以通过使用下标获取类中元素值下标的元素 class Library(object): def __init__(s ...

  5. 【数据结构】什么是二叉查找树(BST)

    什么是二叉查找树(BST) 1. 什么是BST 对于二叉树中的每个节点X,它的左子树中所有项的值都小于X中的项,它的右子树中所有项的值大于X中的项.这样的二叉树是二叉查找树. 以上是一颗二叉查找树,其 ...

  6. .NET多线程知识快速学习

    多线程是一个不会过时的话题,因为每个开发的成长必然要掌握这个知识点,否则半懂不懂怎么保证系统的可靠性和性能,其实在网上随便一搜都会有海量的文章说这个话题,大多数写得很细写得非常好,但发现很少有概览性的 ...

  7. Python笔记:设计模式之状态模式

    状态模式可以看做是在运行时改变对象行为的一种方式.状态模式允许对象在其内部状态变化时改变其行为,此时感觉就像对象本身已经改变了一样. 参与者: State接口:State基类,定义不同状态共同需要执行 ...

  8. 工作总结汇报公司介绍产品宣传品牌展示企业文化PPT模

    清晰明了:在工作总结会议上都是要严肃为主,搞的花里胡哨既不好看也让领导有不好的影响:微粒体:模板样式立体效果非常好,能够一把将观众眼球给吸引住:样式齐全:各种PPT样式都有,能够承载工作汇报各种内容: ...

  9. 微信小程序获取input输入框内容

    1.wxml <input class="weui-input" type='number' bindinput="emailInput"/>   ...

  10. opencv-python 图像处理(五)

    Canny边缘检测 1) 使用高斯滤波器,以平滑图像,滤除噪声. 2) 计算图像中每个像素点的梯度强度和方向. 3) 应用非极大值(Non-Maximum Suppression)抑制,以消除边缘检测 ...