来源:点击打开链接
山东省第三届ACM省赛的题。题不难,是个人就能读懂,但是WA的特别多,现场最多的队WA了26次,好多B题(亿级DP+贪心)改过了的队栽在了这个看似简单的模拟上。
注意点:
1、小数点可以没有,用小数点和星号做分隔符的可以省省了。
2、空格没说几个,用getline()相当麻烦,不如手动输入字符串数组。
3、inches不是唯一分隔符,手机名称中可能带inches字样,所以应该find_last_of("inches")(我没有用这个方法)
4、注意0.00的结果。
5、手机名称中如果有N个空格 输出的时候输出一个空格。
掌握了这些,估计模拟题的细节就毕业了,贵在心细。。
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctype.h>
using namespace std;
int main()
{
int testcase;
char p;
cin>>testcase;
for(int casepos=1;casepos<=testcase;casepos++)
{
string tar[1000];
int numstart,numend,namepos,typepos,xing;
double inches,height,wide,result;
string inchesstr,heightstr,widestr,needc;
int t=0;
while(1)
{
cin>>tar[t];
t++;
if(tar[t-1]=="inches")
{
numstart=t-2;
typepos=t;
}
p=getchar();
if(p=='\n')
break;
}
//cout<<numstart<<endl;
inchesstr=tar[numstart];
inches=atof(inchesstr.c_str());
//cout<<inches<<endl;
needc=tar[typepos];
xing=needc.find('*',0);
for(int j=0;j<xing;j++)
heightstr+=needc[j];
for(int j=xing+1;j<needc.size();j++)
widestr+=needc[j];
height=atof(heightstr.c_str());
wide=atof(widestr.c_str());
if(fabs(inches)<=1e-9)
{
result=0;
}
else
result=sqrt((height*height)+(wide*wide))/inches*1.0;
for(int i=typepos+1;i<t;i++)
{
for(int k=0;k<tar[i].size();k++)
{
tar[i][k]=tolower(tar[i][k]);
}
}
cout<<"Case "<<casepos<<": The ";
for(int i=typepos+1;i<t;i++)
cout<<tar[i]<<" ";
cout<<"of";
for(int j=0;j<numstart;j++)
cout<<" "<<tar[j];
cout<<"'s PPI is "<<setiosflags(ios::fixed)<<setprecision(2)<<result<<"."<<endl;
}
return 0;
}