poj1003 Hangover && poj1004 Financial Management

来源:互联网 发布:移动互联网软件开发 编辑:程序博客网 时间:2024/06/05 03:57

这两题比较水,就合在一起了。

1003:

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2+ 1/3= 5/6 card lengths. In general you can make n cards overhang by 1/2+ 1/3+ 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.



Input
The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.
Output
For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.
Sample Input
1.003.710.045.190.00
Sample Output
3 card(s)61 card(s)1 card(s)273 card(s)
题意:给出m,求1/2+1/3+1/4+..+1/(n+1)>=m的最小n。
思路:数据小,模拟。
#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<iostream>#include<queue>#include<map>#include<string>#define ll long longusing namespace std;double n;int main(){ while (scanf("%lf",&n),n){  double m=0,k=2;  int ans=0;  while (m<n){   m+=1/k;   k++; ans++;  }  printf("%d card(s)\n",ans);  } return 0;}

1004:
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.        
Input
The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.
Output
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.
Sample Input
100.00489.1212454.121234.10823.05109.205.271542.25839.1883.991295.011.75
Sample Output
$1581.42
题意:给出12个数,求它们的平均值。
C++里double的scanf读入scanf("%lf",..)
然后很坑,交G++WA了n遍,C++就能过了。
#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<iostream>#include<queue>#include<map>#include<string>#define ll long longusing namespace std;int main(){ double sum,n; sum=(double)0; for (int i=0;i<12;i++){  scanf("%lf",&n);  sum+=n; } printf("$%.2lf\n",sum/(double)12); return 0;}

0 0
原创粉丝点击