您的位置:首页 > 编程语言

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; }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: