这两题比较水,就合在一起了。
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
1.00 3.71 0.04 5.19 0.00
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 long using 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
100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75
$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 long using 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; }