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. LWP 轻量级线程的意义与实现

    转子 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 二.Linux 2.4内核中的轻量进程实现 最初的进程定义都包含程序.资源 ...

  2. centos 搭建gitlab

    #修改yum源 yum -y install wget cd /etc/yum.repos.d wget -O CentOS-Base.repo http://mirrors.aliyun.com/r ...

  3. Oracle 11g r2 x64 中文乱码解决方案

    1.检查服务器编码: 执行SQL语法: select * from v$nls_parameters; 2.设置本地客户端编码: 进入 我的电脑,属性,高级,环境变量,添加2项:LANG=zh_CN. ...

  4. 用开源Look&Feel (Substance)写 漂亮的Swing应用程序

    今天用Swing 做了一个模仿QQ2009的登录界面,用到了开源的Look&Feel (Substance),在使用的过程中遇到了一些问题,也学到了一些技巧.Substance (https: ...

  5. NoClassDefFoundError:aspectj/weaver/reflect/ReflectionWorld$Reflection

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: ...

  6. 【iOS】app的生命周期

    对于iOS应用程序,关键的是要知道你的应用程序是否正在前台或后台运行.由于系统资源在iOS设备上较为有限,一个应用程序必须在后台与前台有不同的行为.操作系统也会限制你的应用程序在后台的运行,以提高电池 ...

  7. tcpdump 时报ServFail 0/0/1 (97)

    ServFail           结合业务应该是dns   server fail 0/0/1               1/0/0表示机器号/槽位号/子接口号                 ...

  8. linux下ping的C语言实现(转)

    #include <stdio.h> #include <signal.h> #include <arpa/inet.h> #include <sys/typ ...

  9. HDOJ 1536 S-Nim

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  10. bc.34.B.Building Blocks(贪心)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...