http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411
首先要找到输入与输出的规律。。。
我们仔细观察题目的话,可以发现 他给我们的格式是 “名称 + 屏幕大小(以英寸为单位)+ inches + 屏幕规格(Wp*Hp) + 类型”。其中加号的位置可以有若干个空格,当然在名称和类型中也可以有若干个空格。最后输出的时候,字符串之间只能有一个空格,类型必须转化为小写,最后一行不能有换行符。还有一个坑,就是当Inches为0的情况,这是你就要另外处理。
输出的格式是“The + 类型 + of + 名称's + PPI is + 像素.”。
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1010;
char s[101][101]; //存储原字符
char name[maxn],type[maxn];
void change(char *p) //将类型转换为小写字母
{
int len = strlen(p);
for(int i=0;i<len;i++)
{
if(p[i]>='A' && p[i]<='Z')
p[i] += 32;
}
}
double DP(double inches,double wp,double hp)
{
return sqrt(wp*wp+hp*hp)/inches;
}
int main()
{
int n;
double a,wp,hp,ans;
int cnt;
int k;
scanf("%d",&n);
getchar();
for(int t=1;t<=n;t++)
{
cnt = 0;
memset(name,0,sizeof(name));
memset(type,0,sizeof(type));
while(scanf("%s",s[cnt]))
{
if(strstr(s[cnt],"inches")!=NULL) //找到最后一个inches
k = cnt;
cnt ++;
if(getchar()=='\n')
break;
}
sscanf(s[k-1],"%lf",&a);
sscanf(s[k+1],"%lf*%lf",&wp,&hp);
for(int i=0;i<k-1;i++)
{
if(i)
strcat(name," ");
strcat(name,s[i]);
}
for(int i=k+2;i<cnt;i++)
{
strcat(type,s[i]);
if(i!=cnt-1)
strcat(type," ");
}
change(type);
if(a==0.0)
ans = 0;
else
ans = DP(a,wp,hp);
printf("Case %d: The %s of %s's PPI is %.2lf.\n",t,type,name,ans);
}
return 0;
}
参考博客:https://blog.csdn.net/luomingjun12315/article/details/45419083