joj 1032 deck 重心的计算

1032: Deck


Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
15s 8192K 1819 601 Standard

Scenario

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
3
4
30

Sample Output

The line of digits is intended to guide you in proper output alignment, and is not part of the output that your solution should produce.

# Cards  Overhang 
    1     0.500
    2     0.750
    3     0.917
    4     1.042
   30     1.997

看了半天没有看懂题意,没办法

/*
	Scenario(题目描述)

如果小心摆放一张卡片,就能让它宽的那侧和桌子的一侧平行,并且,卡片的一半是能伸出桌子外面的。如果再把卡片往外放,卡片的重心脱离桌子,卡片就会从桌子上掉下来。
同理,如果卡片是被放在另一张卡片上,而不是桌子上,情况亦然。

仔细摆放两张卡片,两张卡片宽的那一侧都要与桌沿平行,要将卡片的3/4伸出桌面。上面那张卡片伸出的要比底下那张的边缘长一半。底下那张只把卡片长度的1/4伸出桌沿。
两张卡片的重心恰好位于桌沿。

三张卡片也可以类似于这样摆放,每张卡片只能接触到其中一张。要将一张卡片的11/12伸出桌面,上面两张卡片伸出的要比底下那张的边缘长3/4,底下那张只把卡片长度的1/6伸出桌沿。
三张卡片的重心恰好位于桌沿。

如果你一直堆积卡片,每张卡片的边缘都要平行,而且每张卡片上下都只与一张卡片接触,4张卡片能伸出桌沿多长的距离?52张呢?1000张呢?99999张呢?

Input

输入包含若干个非负整数,每个数占一行,且每个整数不超过99999.

Output

输出将包含:

在成功完成的程序中,有一个标题:# Cards Overhang(两个单词中间有两个空格)

接下来,每一行包含每个输入的整数以及由卡片数算出来的可达到的伸长的最大长度,为使卡片的长度更加精确,务必要四舍五入到小数点后三位.
卡片长度必须由至少一位数字在小数点之前和精确的小数点后三位来表达.卡片数要右对齐到第五列,表示长度的小数点与第十二列对齐.

解题思路:

其实看懂了题目也很简单,感觉跟各大OJ上的HangOver这道题差不多,题目要求我们算出卡片的长度,1张卡片的时候长度是1/2,2张卡片的时候长度为3/4,3张卡片的时候长度为11/12.
其实就是算1/2+1/4+1/6+...+1/n的和,1张卡片是1/2,2张卡片是1/2+1/4,n是以+2递增的,用循环就可以AC了~(还要注意下输出格式!!!)



从上面的卡片往下看,设前n-1个超越的长度是L(n-1),则再加上最下面的一个共n个超越的长度设为L(n), 可将上面的n-1个看成一个整体,最下面的单独看,
则由力矩平衡可得,(n-1)m×(L(n)-L(n-1))=m×[1/2-(L(n)-L(n-1))]  (其中m为各个卡片的质量),化简下即得递推关系式:L(n)-L(n-1)=1/2n
*/

#include <stdio.h>

int main()
{
	int n;
	printf("# Cards  Overhang\n");
	while(scanf("%d",&n) != EOF)
	{
		double sum = 0.0;
		for(int i=1;i<=n;i++)
			sum += 1.0/(2*i);
		printf("%5d     %.3lf\n",n,sum);
	}
}
 

你可能感兴趣的:(计算)