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

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、输出的时候不要漏掉最后的句号。

  最后,做这类麻烦题,思路一定要清晰,切忌浮躁和代码杂乱。

  代码


  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 }

Freecode : www.cnblogs.com/yym2013

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

时间: 2024-10-05 06:42:36

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

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

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst

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

Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game named "Fruit Ninja"?Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for Nvidia Tegra 2 based Android devices) is a video game de

SDUT 2411 Pixel density(模拟,字符串)

题目链接:SDUT 2411 Pixel density 这一题,可以说题目略坑.为什么呢,第一就是他题意描述过于模糊,第二题输出格式有点坑(说到底还是题意不要表述清晰).现在我们来看看这一题吧,这一题是 <2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛> 的题.本题可以说是一道着重于细节的一道题.对于细节没有把握清楚的话,就是WA.据说当时某些队伍就是因为细节没有把握好,结果硬是WA了26次(深表同情啊).我们仔细观察题目的话,可以发现 他给我们的格式是 &qu

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

Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess set out immedia

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

Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each quer

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

本文出处:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411 题意:给你一个串,让你依据那个串来输出ppi.坑特别多.ppi的计算方法是dp / inches; dp = sqrt(wp*wp + hp * hp); 现在我来说说这个题目有多坑: 给你的串的格式是这样: name + inches+ "inches"

Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

Time Limit: 5000MS Memory limit: 65536K 题目描写叙述 Haveyou ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for NvidiaTegra 2 based Android devices) is a video game developed by Hal

2012山东省ACM省赛-Pixel density

Pixel density 题目描述 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 qual

Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)

题目地址:sdut 2608 Alice and Bob Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*....