2014年编程之美初赛解题报告题目一
2014-04-19 22:36
357 查看
题目1 : 焦距
时间限制:2000ms单点时限:1000ms
内存限制:256MB
描述
一般来说,我们采用针孔相机模型,也就是认为它用到的是小孔成像原理。在相机坐标系下,一般来说,我们用到的单位长度,不是“米”这样的国际单位,而是相邻像素的长度。而焦距在相机坐标系中的大小,是在图像处理领域的一个非常重要的物理量。
假设我们已经根据相机参数,得到镜头的物理焦距大小(focal length),和相机胶片的宽度(CCD width),以及照片的横向分辨率(image width),则具体计算公式为:
Focal length in pixels = (image width in pixels) * (focal length on earth) / (CCD width on earth)
比如说对于Canon PowerShot S100, 带入公式得
Focal length in pixels = 1600 pixels * 5.4mm / 5.27mm = 1639.49 pixels
现在,请您写一段通用的程序,来求解焦距在相机坐标系中的大小。
输入
多组测试数据。首先是一个正整数T,表示测试数据的组数。每组测试数据占一行,分别为
镜头的物理焦距大小(focal length on earth)
相机胶片的宽度(CCD width on earth)
照片的横向分辨率大小(image width in pixels),单位为px。
之间用一个空格分隔。
输出
每组数据输出一行,格式为“Case X: Ypx”。 X为测试数据的编号,从1开始;Y为焦距在相机坐标系中的大小(focallength in pixels),保留小数点后2位有效数字,四舍五入取整。
数据范围
对于小数据:focal length on earth和CCD width on earth单位都是毫米(mm)对于大数据:长度单位还可能为米(m), 分米(dm), 厘米(cm), 毫米(mm), 微米(um),纳米(nm)
样例输入
2 5.4mm 5.27mm 1600px 5400um 0.00527m 1600px
样例输出
Case 1: 1639.47px Case 2: 1639.47px
#include <iostream> #include <stdio.h> #include <sstream> #include <string> using namespace std; double convertFromString(const string &s) { istringstream i(s); double x; if (i>>x) { return x; } } double getNumber(string &focalLengthEarthStr) { double focalLengthEarth; int index1; char temp[2]; for (int i=0;i<focalLengthEarthStr.length();i++) { if (focalLengthEarthStr[i]=='u'||focalLengthEarthStr[i]=='m'||focalLengthEarthStr[i]=='d'||focalLengthEarthStr[i]=='c'||focalLengthEarthStr[i]=='n'||focalLengthEarthStr[i]=='p') { temp[0]=focalLengthEarthStr[i]; if (focalLengthEarthStr[i+1]!='\0') { temp[1]='m'; } index1=i; break; } } focalLengthEarth=convertFromString(focalLengthEarthStr.substr(0,index1)); switch (temp[0]) { case 'u': focalLengthEarth/=1000; break; case 'm': if (temp[1]=='m') { focalLengthEarth=focalLengthEarth; } else { focalLengthEarth*=1000; } break; case 'c': focalLengthEarth*=10; break; case 'n': focalLengthEarth/=1000000; break; } return focalLengthEarth; } int main() { int cases; cin>>cases; int current=0; while (current<cases) { double focalLengthEarth,ccdWidth,imageWidth; long double focalLengthPixel; string focalLengthEarthStr,ccdWidthStr,imageWidthStr; getline(cin,focalLengthEarthStr,' '); getline(cin,ccdWidthStr,' '); getline(cin,imageWidthStr); //cin>>focalLengthEarth>>ccdWidth>>imageWidth; focalLengthEarth=getNumber(focalLengthEarthStr); ccdWidth=getNumber(ccdWidthStr); imageWidth=getNumber(imageWidthStr); focalLengthPixel=(focalLengthEarth*imageWidth)/ccdWidth; current++; printf("Case %d:",current); printf("%.2f",focalLengthPixel); printf("px\n"); } return 0; }
相关文章推荐
- Java - 实例化(instantiation) 详解
- 帝国cms列表分页代码的修改
- PHP开发中的小技巧(需注意)
- Java之CloneNotSupportedException
- PHP邮件发送主要代码
- 自己动手写代码,整合Spring和Hibernate(一)和FactoryBean的使用
- PHP验证码工具类
- java JNDI配置与自定义
- 编程之美 第一场 第一题
- 一个最基本的PHP解析XML的实例
- 【小白笔记】PHP学习之路 (30) --绘制文字及相关库函数
- phpnow几个重要配置文件目录
- PHP操作数据库工具类
- C# 创建缩略图
- C++类模版学习笔记
- TortoiseGit和msysGit安装及使用笔记(windows下使用上传数据到GitHub)
- C 风格字符串 (c++primer)
- Windows 7 bug: nonexistent Java Runtime Enviroment
- 用C#创建PDF表格:iTextSharp的应用
- Windows 7 bug: nonexistent Java Runtime Enviroment