sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)

时间:2014-04-28 07:48:18   收藏:0   阅读:534

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、输出的时候不要漏掉最后的句号。
  最后,做这类麻烦题,思路一定要清晰,切忌浮躁和代码杂乱。
  代码
bubuko.com,布布扣
  1 #include <iostream>
  2 #include <string.h>
  3 #include <cmath>
  4 #include <stdio.h>
  5 using namespace std;
  6 char str[1000000];
  7 double Abs(double n)
  8 {
  9     return n<0?-n:n;
 10 }
 11 void GetVal(char name[],char kind[],double &ppi)
 12 {
 13     //iPhone 4S  3.5 inches 960*640 PHONE
 14     int len = strlen(str),indexl,indexr,i,t,x;
 15     double a,b,c;
 16     //类型后界
 17     for(indexr=len-1;indexr>=0;indexr--)
 18         if(str[indexr]!= )
 19             break;
 20     //类型前界
 21     for(indexl=indexr;indexl>=0;indexl--)
 22         if(str[indexl]== )
 23             break;
 24     //提取类型
 25     t = 0;
 26     for(i=indexl+1;i<=indexr;i++){
 27         if(A<=str[i] && str[i]<=Z)  //类型的大写全部转换成小写
 28             kind[t++] = str[i] + 32;
 29         else
 30             kind[t++] = str[i];
 31     }
 32     kind[t] = \0;
 33     //a*b的b的后界
 34     for(indexr = indexl;indexr>=0;indexr--)
 35         if(str[indexr]!= )
 36             break;
 37     //提取b
 38     t = 0;x = 1;
 39     for(i=indexr;str[i]!=*;i--){
 40         int tt = str[i]-0;
 41         t = tt*x + t;
 42         x*=10;
 43     }
 44     b = t;
 45     //提取a
 46     indexr = i-1;
 47     t = 0;x = 1;
 48     for(i=indexr;str[i]!= ;i--){
 49         int tt = str[i]-0;
 50         t = tt*x + t;
 51         x*=10;
 52     }
 53     a = t;
 54     for(indexr = i;str[indexr]== ;indexr--);
 55     for(;str[indexr]!= ;indexr--);
 56     //找c的下界
 57     for(;str[indexr]== ;indexr--);
 58     //找c的上界
 59     for(indexl = indexr;str[indexl]!= ;indexl--);
 60     for(i = indexl+1;str[i]!=. && i<=indexr;i++);    //找到小数点或者到下界
 61     if(str[i]==.){    //有小数点,证明是小数
 62         //提取c的小数部分
 63         c = 0;
 64         double xx = 0.1;
 65         int j;
 66         for(j=i+1;str[j]!= ;j++){
 67             int tt = str[j]-0;
 68             c = c+tt*xx;
 69             xx/=10;
 70         }
 71         //提取c的整数部分
 72         x = 1;
 73         for(j=i-1;str[j]!= ;j--){
 74             int tt = str[j]-0;
 75             c = c+tt*x;
 76             x*=10;
 77         }
 78         indexr = j;
 79     }
 80     else {    //不是小数点,证明是整数
 81         //提取整数
 82         c = 0;
 83         x = 1;
 84         int j;
 85         for(j=indexr;str[j]!= ;j--){
 86             int tt = str[j]-0;
 87             c = c+tt*x;
 88             x*=10;
 89         }
 90         indexr = j;
 91     }
 92     //可以计算ppi了
 93     if( Abs(c)<1e-9 )   //是0
 94         ppi = 0;
 95     else
 96         ppi = sqrt(a*a+b*b)/c;
 97     //名字后界
 98     for(;indexr>=0;indexr--)
 99         if(str[indexr]!= )
100             break;
101     //类型前界
102     for(indexl=0;str[indexl]== ;indexl++);
103     //提取名字
104     t = 0;
105     for(i=indexl;i<=indexr;i++)
106         name[t++] = str[i];
107     name[t] = \0;
108 }
109 int main()
110 {
111     int T;
112     cin>>T;
113     getchar();
114     for(int cnt = 1;cnt<=T;cnt++){
115         cin.getline(str,100000000,\n);
116         char name[10000],kind[10000];
117         double ppi;
118         GetVal(name,kind,ppi); //提取数据:名字,类型,ppi
119         printf("Case %d: The %s of %s‘s PPI is %.2lf.\n",cnt,kind,name,ppi);
120     }
121     return 0;
122 }
bubuko.com,布布扣

 

Freecode : www.cnblogs.com/yym2013

sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!