Pixel density 模拟

Description

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.

Input

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

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).

Sample Input

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

Sample Output

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.

Hint
Dp= sqrt(Wp*Wp+Hp*Hp )

Wp is width resolution in pixels, Hp is height resolution in pixels.

借鉴博客:https://www.cnblogs.com/yym2013/p/3694768.html

一道字符串的模拟题,先把一行字符串读进去,用cin.getline,然后从字符串中依次提取名字name,型号kind,和ppi,然后按格式输出。写一个get函数,专门提取这些东西,从字符串的末尾开始往前处理每一个字符分别取出型号kind,屏幕高a*屏幕宽b,尺寸c,名字name,然后判断c是否为0,如果等于0,那么ppi为0,否则计算ppi=sqrt(a*a+b*b)/c。

需要注意的地方

1、inches尺寸前面的数可以是整数,也可以是浮点数,也就是说可能有小数点,也可能没有。所以不能用小数点'.'作为标记。
  2、空格问题。字符串前面可能有空格,每个单词中间可能有多个空格,最后也可能有多个空格。
  3、注意尺寸和高*宽两个数字部分,可能写错。也就是说有这几种情况:0.0,0,00000,0009.2,9.20000,0640*480000。
  4、注意类型kind提取出来后要全部转换成小写字母。
  5、输出的时候不要漏掉最后的句号。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
char str[1000000];
double change(double a)
{
    return a<0?-a:a;
}
void get(char name[],char kind[],double &ppi)
{
    int len=strlen(str),il,ir,t,i,x;
    double a,b,c;
    //找到类型后界
    for(ir=len-1;ir>=0;ir--)
    {
        if(str[ir]!=' ')
            break;
    }
    //找到类型前界
    for(il=ir;il>=0;il--)
    {
        if(str[il]==' ')
            break;
    }
    t=0;
    //提取类型
    for(i=il+1;i<=ir;i++)
    {
        if('A'<=str[i]&& str[i]<='Z')//类型大写要全部转小写
            kind[t++]=str[i]+32;
        else
            kind[t++]=str[i];
    }
    kind[t]='\0';
    //找到a*b的b的后界
    for(ir=il;ir>=0;ir--)
    {
        if(str[ir]!=' ')
            break;
    }
    //提取b
    t=0;
    x=1;
    for(i=ir;str[i]!='*';i--)
    {
        int s=str[i]-'0';
        t=s*x+t;
        x*=10;
    }
    b=t;
    //提取a
    ir=i-1;
    t=0;
    x=1;

    for(i=ir;str[i]!=' ';i--)
    {
        int s=str[i]-'0';
        t=s*x+t;
        x*=10;
    }
    a=t;
    for(ir=i;str[ir]==' ';ir--);
    for(;str[ir]!=' ';ir--);
    //找到c的后界
    for(;str[ir]==' ';ir--);
    //找到c的前界
    for(il=ir;str[il]!=' ';il--);
    for(i=il+1;str[i]!='.'&&i<=ir;i++);//找到小数点或者后界

        if(str[i]=='.')//有小数点,说明这个数是小数
        {
            c=0;//提取小数部分
            double xx=0.1;
            int j;
            for(j=i+1;str[j]!=' ';j++)
            {
                int tt=str[j]-'0';
                c=tt*xx+c;
                xx/=10;
            }
            x=1;
            for(j=i-1;str[j]!=' ';j--)//提取整数部分
            {
                int tt=str[j]-'0';
                c=c+tt*x;
                x*=10;
            }
            ir=j;
        }
        else//不是小数点,说明是整数
        {
            c=0;//提取整数
            x=1;
            int j;
            for(j=ir;str[j]!=' ';j--)
            {
                int tt=str[j]-'0';
                c=c+tt*x;
                x*=10;
            }
            ir=j;
        }

    if(c==0)//提取ppi
        ppi=0;
    else
        ppi=sqrt(a*a+b*b)/c;
    //找到名字后界
    for(;ir>=0;ir--)
    {
        if(str[ir]!=' ')
            break;
    }
    //找到名字前界
    for(il=0;str[il]==' ';il++);
    //提取名字
    t=0;
    for(i=il;i<=ir;i++)
    {
        name[t++]=str[i];
    }
    name[t]='\0';
}
int main()
{
    int t;
    cin>>t;
    int cnt=1;
    getchar();
    while(t--)
    {
        cin.getline(str,100000000,'\n');
        char name[10000],kind[10000];
        double ppi;
        get(name,kind,ppi);
        printf("Case %d: The %s of %s's PPI is %.2lf.\n",cnt++,kind,name,ppi);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值