例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
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;
}
习题3
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 beyon