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.

提示

Dp= sqrt(Wp*Wp+Hp*Hp )
Wp is width resolution in pixels, Hp is height resolution in pixels.

来源

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

 
  字符串处理
  思路是先将这一行字符串都读入进来,然后从字符串中依次提取出名字name,型号kind,和ppi,最后按格式输出。写一个函数,专门提取。在函数中,我的做法是倒着向前依次处理每一个字符分别提取出型号kind,屏幕高a*屏幕宽b,尺寸c,名字name,然后判断c是否等于0,如果等于,令ppi=0;否则计算ppi = sqrt(a*a+b*b)/c。最后返回,输出结果。
  这道题需要注意细节的处理,很容易WA。
  下面是我写代码过程中遇到的几个需要注意的地方:
  1、inches尺寸前面的数可以是整数,也可以是浮点数,也就是说可能有小数点,也可能没有。所以不能用小数点'.'作为标记。
  2、空格问题。字符串前面可能有空格,每个单词中间可能有多个空格,最后也可能有多个空格。
  3、注意尺寸和高*宽两个数字部分,可能写错。也就是说有这几种情况:0.0,0,00000,0009.2,9.20000,0640*480000。
  4、注意类型kind提取出来后要全部转换成小写字母。
  5、输出的时候不要漏掉最后的句号。
  最后,做这类麻烦题,思路一定要清晰,切忌浮躁和代码杂乱。
  代码
 #include <iostream>
#include <string.h>
#include <cmath>
#include <stdio.h>
using namespace std;
char str[];
double Abs(double n)
{
return n<?-n:n;
}
void GetVal(char name[],char kind[],double &ppi)
{
//iPhone 4S 3.5 inches 960*640 PHONE
int len = strlen(str),indexl,indexr,i,t,x;
double a,b,c;
//类型后界
for(indexr=len-;indexr>=;indexr--)
if(str[indexr]!=' ')
break;
//类型前界
for(indexl=indexr;indexl>=;indexl--)
if(str[indexl]==' ')
break;
//提取类型
t = ;
for(i=indexl+;i<=indexr;i++){
if('A'<=str[i] && str[i]<='Z') //类型的大写全部转换成小写
kind[t++] = str[i] + ;
else
kind[t++] = str[i];
}
kind[t] = '\0';
//a*b的b的后界
for(indexr = indexl;indexr>=;indexr--)
if(str[indexr]!=' ')
break;
//提取b
t = ;x = ;
for(i=indexr;str[i]!='*';i--){
int tt = str[i]-'';
t = tt*x + t;
x*=;
}
b = t;
//提取a
indexr = i-;
t = ;x = ;
for(i=indexr;str[i]!=' ';i--){
int tt = str[i]-'';
t = tt*x + t;
x*=;
}
a = t;
for(indexr = i;str[indexr]==' ';indexr--);
for(;str[indexr]!=' ';indexr--);
//找c的下界
for(;str[indexr]==' ';indexr--);
//找c的上界
for(indexl = indexr;str[indexl]!=' ';indexl--);
for(i = indexl+;str[i]!='.' && i<=indexr;i++); //找到小数点或者到下界
if(str[i]=='.'){ //有小数点,证明是小数
//提取c的小数部分
c = ;
double xx = 0.1;
int j;
for(j=i+;str[j]!=' ';j++){
int tt = str[j]-'';
c = c+tt*xx;
xx/=;
}
//提取c的整数部分
x = ;
for(j=i-;str[j]!=' ';j--){
int tt = str[j]-'';
c = c+tt*x;
x*=;
}
indexr = j;
}
else { //不是小数点,证明是整数
//提取整数
c = ;
x = ;
int j;
for(j=indexr;str[j]!=' ';j--){
int tt = str[j]-'';
c = c+tt*x;
x*=;
}
indexr = j;
}
//可以计算ppi了
if( Abs(c)<1e- ) //是0
ppi = ;
else
ppi = sqrt(a*a+b*b)/c;
//名字后界
for(;indexr>=;indexr--)
if(str[indexr]!=' ')
break;
//类型前界
for(indexl=;str[indexl]==' ';indexl++);
//提取名字
t = ;
for(i=indexl;i<=indexr;i++)
name[t++] = str[i];
name[t] = '\0';
}
int main()
{
int T;
cin>>T;
getchar();
for(int cnt = ;cnt<=T;cnt++){
cin.getline(str,,'\n');
char name[],kind[];
double ppi;
GetVal(name,kind,ppi); //提取数据:名字,类型,ppi
printf("Case %d: The %s of %s's PPI is %.2lf.\n",cnt,kind,name,ppi);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)的更多相关文章

  1. sdut 2416:Fruit Ninja II(第三届山东省省赛原题,数学题)

    Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game name ...

  2. sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

    n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you w ...

  3. sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

    Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you a ...

  4. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  5. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  6. sdut 2163:Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...

  7. sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

  8. sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)

    Ivan comes again! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...

  9. sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)

    Clockwise Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...

随机推荐

  1. html5+ 获取当前设备的加速度信息

    getCurrentAcceleration 获取当前设备的加速度信息 void plus.accelerometer.getCurrentAcceleration( successCB, error ...

  2. VS2010+OpenCV2.4.6永久性配置方法

    1. 配置OpenCV环境变量 计算机->(右键)属性,出现如图1所示界面 单击“高级系统设置”,选中高级(标签)出现如图2所示界面 单击右下方的“环境变量”,弹出如图3所示界面,注意这里最好用 ...

  3. OGNL和Struts2标签

    OGNL和Struts2标签 你使用过的OGNL 页面获取并输出Action属性<s:property value="userName"/> 页面中获取request保 ...

  4. JavaScriptSerializer中日期序列化问题解决方案

    JavaScriptSerializer中日期序列化问题解决方案 直接进入主题: class Student { public int age { get; set; } public DateTim ...

  5. Asp.net面试题

    Asp.net核心技术思想 1.概述反射和序列化 反射:程序集包含模块,而模块包含类型,类型又包含成员.反射则提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对 ...

  6. C#集合接口的继承关系图

  7. Java中的构造函数和重载

    一.Java中的构造函数 构造函数是对象被创建时初始化对象的成员方法,它具有和它所在的类完全一样的名字.构造函数只能有入口参数,没有返回类型,因为一个类的构造方法的返回类就是类本身.构造函数定义后,创 ...

  8. 彻底理解数字图像处理中的卷积-以Sobel算子为例

    彻底理解数字图像处理中的卷积-以Sobel算子为例 作者:FreeBlues 修订记录 2016.08.04 初稿完成 概述 卷积在信号处理领域有极其广泛的应用, 也有严格的物理和数学定义. 本文只讨 ...

  9. Telegram

    官网:https://www.telegram.org/ GitHub:https://github.com/telegramdesktop/tdesktop

  10. 特殊表达式的意义[c++ special expressions]

    [本文链接] http://www.cnblogs.com/hellogiser/p/special-expressions.html x&(x-1)表达式的意义: 统计二进制中1的个数.   ...