第十二天
- 今天被老师给的视频“带”去做了水题。
- 今日收获:(好像没收获)强化了格式化输出信息的能力
第一题:POJ1005
题目描述(看原题好一点):
一个在被河水侵蚀的河岸住一辈子的人要我计算出他家什么时候被河水吞没。河水吞没的面积形状呈半圆形扩散,每年增加50平方英里。
输入:
第一行给出整数M,接下来输入M行坐标。以半圆中心为原点,河岸为x轴,每行给出两个浮点数,此为他家的坐标(单位为英里)
输出:
至少经过N年后,他家被吞没。输出对于每个坐标,像样例输出那样输出至少经过N年,他家被吞没。
最后一行输出END OF OUTPUT.
样例输入:
2
1.0 1.0
25.0 0.0
样例输出:
Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.
END OF OUTPUT.
- 思路:根据坐标求半径,然后算面积,再用面积除以50,向上取整。
- 代码:
//POJ1005
#include<iostream>
#include<cmath>
using namespace std;
double area(double x,double y)
{
double tmp = sqrt(x*x+y*y);
return tmp*tmp*3.14159*0.5;
}
int main()
{
int n;
cin>>n;
double x,y;
for(int i =1;i<=n;++i){
cin>>x>>y;
cout<<"Property "<<i<<": This property will begin eroding in year "<<ceil(area(x,y)/50)<<'.'<<endl;
}
cout<<"END OF OUTPUT."<<endl;
return 0;
}
第二题:POJ1003
题目描述:
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.
输入
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.
输出
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.
样例输入
1.00
3.71
0.04
5.19
0.00
样例输出
3 card(s)
61 card(s)
1 card(s)
273 card(s)
#include<iostream>
using namespace std;
//test() 用来打表,提交代码时不用执行
void test()
{
int i = 2;
//double m = 1;
double result = 0;
for(;i<278;++i)
{
result+=1.0/i;
cout<<result<<',';
}
//cout<<result<<' '<<i<<endl;
return;
}
int main()
{
double result[] = {0.0,0.5,0.833333,1.08333,1.28333,1.45,1.59286,1.71786,1.82897,1.92897,2.01988,2.10321,2.18013,2.25156,2.31823,2.38073,2.43955,2.49511,2.54774,2.59774,2.64536,2.69081,2.73429,2.77596,2.81596,2.85442,2.89146,2.92717,2.96165,2.99499,3.02725,3.0585,3.0888,3.11821,3.14678,3.17456,3.20159,3.2279,3.25354,3.27854,3.30293,3.32674,3.35,3.37273,3.39495,3.41669,3.43796,3.4588,3.47921,3.49921,3.51881,3.53804,3.55691,3.57543,3.59361,3.61147,3.62901,3.64625,3.6632,3.67987,3.69626,3.71239,3.72827,3.74389,3.75928,3.77443,3.78935,3.80406,3.81855,3.83284,3.84692,3.86081,3.87451,3.88802,3.90136,3.91451,3.9275,3.94032,3.95298,3.96548,3.97782,3.99002,4.00207,4.01397,4.02574,4.03737,4.04886,4.06022,4.07146,4.08257,4.09356,4.10443,4.11518,4.12582,4.13635,4.14676,4.15707,4.16728,4.17738,4.18738,4.19728,4.20708,4.21679,4.22641,4.23593,4.24536,4.25471,4.26397,4.27314,4.28223,4.29124,4.30017,4.30902,4.31779,4.32649,4.33511,4.34366,4.35213,4.36053,4.36887,4.37713,4.38533,4.39346,4.40152,4.40952,4.41746,4.42533,4.43315,4.4409,4.44859,4.45622,4.4638,4.47132,4.47878,4.48619,4.49354,4.50084,4.50809,4.51528,4.52243,4.52952,4.53656,4.54355,4.5505,4.55739,4.56424,4.57105,4.5778,4.58451,4.59118,4.5978,4.60438,4.61092,4.61741,4.62386,4.63027,4.63664,4.64297,4.64926,4.65551,4.66172,4.6679,4.67403,4.68013,4.68619,4.69221,4.6982,4.70415,4.71007,4.71595,4.7218,4.72761,4.73339,4.73914,4.74486,4.75054,4.75619,4.76181,4.76739,4.77295,4.77847,4.78397,4.78943,4.79487,4.80027,4.80565,4.811,4.81631,4.82161,4.82687,4.8321,4.83731,4.84249,4.84765,4.85278,4.85788,4.86296,4.86801,4.87303,4.87803,4.88301,4.88796,4.89288,4.89778,4.90266,4.90752,4.91235,4.91716,4.92194,4.9267,4.93144,4.93616,4.94085,4.94553,4.95018,4.95481,4.95942,4.964,4.96857,4.97311,4.97764,4.98214,4.98663,4.99109,4.99554,4.99996,5.00437,5.00875,5.01312,5.01747,5.0218,5.02611,5.0304,5.03467,5.03893,5.04316,5.04738,5.05159,5.05577,5.05994,5.06409,5.06822,5.07233,5.07643,5.08051,5.08458,5.08863,5.09266,5.09668,5.10068,5.10466,5.10863,5.11258,5.11652,5.12044,5.12434,5.12824,5.13211,5.13597,5.13982,5.14365,5.14747,5.15127,5.15506,5.15883,5.16259,5.16634,5.17007,5.17378,5.17749,5.18118,5.18485,5.18852,5.19217,5.1958,5.19943,5.20304};
double tmp;
int left = 0;
int right = 279;
int mid;
while(cin>>tmp&&tmp-0.0>1e-6)
{
left = 0;
right = 276;
while(left+1<right)
{
mid = (left+right)/2;
if(result[mid]>tmp)
{
right = mid;
}
else{
left = mid;
}
}
cout<<left+1<<" card(s)"<<endl;
}
return 0;
}
第三题:POJ1004-求平均数
题目简化描述:
给定十二个浮点数,求其平均数
输入
给出十二行,每行一个包含小数点后两位的浮点数
输出
先输出‘$’,再输入的十二个浮点数的平均数,要求四舍五入到小数点后两位
- 思路:
小数点定点输出+判断是否需要四舍五入(可能我忘了有四舍五入的函数)
代码:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double a;
double tmp = 0;
for(int i =0;i<12;++i)
{
cin>>a;
tmp+=a;
}
tmp/=12;
if(int(tmp*1000)%10>4)
cout<<'$'<<fixed<<setprecision(2)<<tmp+0.01<<endl;
else cout<<'$'<<fixed<<setprecision(2)<<tmp<<endl;
return 0;
}