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. Fast-cgi cgi nginx php-fpm 的关系 (转

    Fast-cgi  cgi  nginx  PHP-fpm 的关系 Fast-cgi是由cgi发展而来,是http服务器(http,nginx等)和动态脚本语言(php,perl等)之间的的通信接口, ...

  2. DEEP LEARNING WITH STRUCTURE

    DEEP LEARNING WITH STRUCTURE Charlie Tang is a PhD student in the Machine Learning group at the Univ ...

  3. iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前

    - (NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate ...

  4. xss概念剖析

    XSS又叫CSS (Cross-Site Scripting) ,跨站脚本攻击.恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意 ...

  5. php排序 sort、rsort、asort、arsort、ksort、krsort

    sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...

  6. [Winform]DataGridView列自适应宽度

    引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...

  7. PDP 有多种定义,具体哪一种还需研究!!!!

    PDP (用户面进行隧道转发的信息的保存协议) 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 即PDP上下文,保存用户面进行隧道转发的所有信息,包括RNC/GGSN的 ...

  8. JVM是如何分配和回收内存?有实例!

    上一篇博客我简单介绍了下如何手动计算一个Java对象到底占用多少内存?今天就想聊下这个内存JVM到底是是如何分配和回收的. Java整体来说还是一个GC比较友好的语言,无论是分代的垃圾收集,还是基于G ...

  9. Sixth scrum meeting - 2015/10/31

    概述 今天是周末,我们小组由于之前拖延的比较久,所以今天仍然在努力的开发…… 目前开发已经到了中期阶段,今天遇到了一个问题就是,由于小组的某些同学对git的使用不太熟悉,导致在git push的时候遇 ...

  10. 【Hadoop】史上最全 Hadoop 生态 全景图