山东省第三届ACM大学生程序设计竞赛-Pixel density(模拟)

Pixel density

Time Limit: 1000MS Memory limit: 65536K

题目描述

 

Pixels per inch (PPI) or pixel density is a measurement of the resolution of devices in various contexts; typically computer displays, image scanners, and digital camera image sensors. Note, the unit is not square inches. Good quality photographs usually require 300 pixels per inch when printed. When the PPI is more than 300(phone), we call it retina screen. Sunnypiggy like the retina screen very much.

 


But you know it is expensive for Sunnypiggy and Sunnypiggy’s own smart phone isn’t like that.
I tell you how to calculate the PPI. First we must know how big the mobile phone’s screen is. Then we get the resolution (Hp*Wp) about it. After that we calculate the diagonal resolution in pixels (Dp) and divided by diagonal size in inches. Now you get the answer.
Maybe you knew it, but Sunnypiggy’s math is very bad and he wants you to help him to calculate the pixel density of all the electronic products he dreamed.
 

输入

First you will get an integer T which means the number of test cases, and then Sunnypiggy will tell you the name and type of the electronic products. And you know, Sunnypiggy is a careless boy and some data aren’t standard, just like 04.00 inches or 0800*0480.

输出

Output the answers to Sunnypiggy just like the sample output. Maybe it is not a phone. Sunnypiggy like such a form, although it seems no use. The result should be rounded to 2 decimal places. When it has no screen (0.0 inches) that we define the answer is 0.00(PPI).

示例输入

2
iPhone 4S  3.5 inches 960*640 PHONE
The new iPad  0009.7 inches 2048*1536 PAD

示例输出

Case 1: The phone of iPhone 4S's PPI is 329.65.Case 2: The pad of The new iPad's PPI is 263.92.

提示

来源

2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛

示例程序


题目意思:

每一行给出机器名称、屏幕尺寸、像素、机器类型。

其中机器名称和机器类型不一定只有一个字符串;

屏幕尺寸以形如 04.00  or 0800*0480 的两种方式给出;

像素是HP*WP的形式给出。

求DP/inches的值,其中DP=sqrt(HP*HP+WP*WP)。

解题思路:

这是一道看起来很简单的模拟题,我就说说各大坑点吧。

①注意特判屏幕大小为0;

②机器类型可能不止一个字符串,当然机器名称也是;

③注意前导空格,不然可能会PE;

④保留两位小数且最后有个句号。


#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
using namespace std;
template <class Type>
Type stringToNum(const string& str)//string转数字的模板
{
    istringstream iss(str);
    Type num;
    iss>>num;
    return num;
}
int main()
{
    int t,cnt=0;
    cin>>t;
    while(t--)
    {
        string s;
        string in="inches";//配对用
        string str[50];//保存整行字符串(到像素位置)
        int i=0,flag,temp=-1;
        double inch;
        char na[50];
        while(cin>>s)//按空格分隔输入各个串
        {
            str[i++]=s;//存入整行
            if(s==in)//如果该串是"inches",flag记录下位置
            {
                flag=i;
                cin>>s;//输入像素
                str[i++]=s;
                gets(na);//输入机器类型
                break;//输入完毕
            }
        }
        flag-=2;
        //特判inches是不是形如0800*0480
        string inc=str[flag];
        for(int j=0; inc[j]!='\0'; ++j)
            if(inc[j]=='*')
                temp=j;
        if(temp==-1)
        {//如果不是就直接转换成double类型
            inch= stringToNum<double>(str[flag]);
        }
        else
        {//如果是就要以*为界限分隔
            string ins1="";
            for(int j=0; inc[j]!='*'; ++j)
                ins1=ins1+inc[j];//inches前半段
            string ins2="";
            for(int j=temp+1; inc[j]!='\0'; ++j)
                ins2= ins2+inc[j];//inches后半段
            double in1= stringToNum<double>(ins1);
            double in2= stringToNum<double>( ins2);
            inch=in1*in2;
        }
        if(inch==0)//特判inch=0时,PPI=0.00
        {
            flag+=2;
            cout<<"Case "<<++cnt<<": "<<"The ";
            int te=-1;
            for(int j=0; na[j]!='\0'; ++j)
            {//去除机器类型的前导空格
                if(na[j]!=' ')
                {
                    te=j;
                    break;
                }
            }
            if(te==-1)//如果没有前导空格
            {
                for(int j=0; na[j]!='\0'; ++j)
                    na[j] =tolower( na[j]);
            }
            else//如果有前导空格
            {
                for(int j=te; na[j]!='\0'; ++j)
                {
                    na[j] =tolower( na[j]);
                    cout<< na[j];
                }
            }
            cout<<" of ";
            flag-=3;
            int j;
            for(j=0; j<flag; ++j)
                cout<<str[j]<<" ";
            cout<<str[flag];
            cout<<"'s PPI is 0.00."<<endl;
            continue;
        }
        //如果inch!=0时
        flag+=2;
        string p=str[flag];
        for(int j=0; p[j]!='\0'; ++j)
            if(p[j]=='*')
                temp=j;//找到像素中的分隔点
        flag--;
        string h="";
        for(int j=0; p[j]!='*'; ++j)
            h=h+p[j];//HP
        string w="";
        for(int j=temp+1; p[j]!='\0'; ++j)
            w=w+p[j];//WP
        double hp= stringToNum<double>(h);
        double wp= stringToNum<double>(w);
        double dp=sqrt(hp*hp+wp*wp);//DP
        double ans=dp/ inch;//求得的答案
        cout<<"Case "<<++cnt<<": "<<"The ";
        int te=-1;
        for(int j=0; na[j]!='\0'; ++j)
        {//去除机器类型的前导空格
            if(na[j]!=' ')
            {
                te=j;
                break;
            }
        }
        if(te==-1)//如果没有前导空格
        {
            for(int j=0; na[j]!='\0'; ++j)
                na[j] =tolower( na[j]);
        }
        else//如果有前导空格
        {
            for(int j=te; na[j]!='\0'; ++j)
            {
                na[j] =tolower( na[j]);
                cout<< na[j];
            }
        }
        cout<<" of ";
        flag-=2;
        for(int j=0; j<flag; ++j)//输出机器名称
            cout<<str[j]<<" ";
        cout<<str[flag];
        cout<<"'s PPI is ";
        printf("%.2f.\n",ans);//保留两位小数
    }
    return 0;
}
/*
自己写的几个测试用例:
5
iPhone 4S  2.6*4.1 inches 960*640   PHONE PAD
The new iPad  0.2*0.3 inches 2048*1536 PAD
The new iPad  0.0 inches 2048*1536   sDg rfgHTb PAD
iPhone 4S  2.6*4.1 inches 960*640 PHONE
The new iPad  0.2*0.3 inches 2048*1536 PAD
*/

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值