C語言鏡頭焦距程式


給定兩個浮點值;影像距離和物體距離透鏡;任務是列印透鏡的焦距。

什麼是焦距?

光學系統的焦距是指透鏡或曲面鏡的中心與其焦點之間的距離。

讓我們藉助下圖來理解:

在上圖中,i 是物體,F 是形成的物體的像,f 是像的焦距。

因此,要根據透鏡找到像的焦距,公式為:

1F= 1O+1I

其中,F 是焦距。

O 是透鏡和物體之間的總距離。

I 是透鏡和透鏡形成的像之間的總距離。

示例

Input: image_distance=5, object_distance=10
Output: Focal length of a lens is: 3.333333
Explanation: 1/5 + 1/10 = 3/10🡺 F = 10/3 = 3.33333333

Input: image_distance = 7, object_distance = 10
Output: Focal length of a lens is: 4.1176470

我們用來解決上述問題的方案

  • 獲取image_disance和object_distance的輸入。
  • 找到1/image_distance和1/object_distance的和,並返回結果除以1的結果。
  • 列印結果。

演算法

Start
Step 1-> In function float focal_length(float image_distance, float object_distance)
   Return 1 / ((1 / image_distance) + (1 / object_distance))

Step 2-> In function int main()
   Declare and initialize the first input image_distance = 5
   Declare and initialize the second input object_distance = 10
   Print the results obtained from calling the function focal_length(image_distance, object_distance)
Stop

示例

 現場演示

#include <stdio.h>
// Function to find the focal length of a lens
float focal_length(float image_distance, float object_distance) {
   return 1 / ((1 / image_distance) + (1 / object_distance));
}
// main function
int main() {
   // distance between the lens and the image
   float image_distance = 5;
   // distance between the lens and the object
   float object_distance = 10;
   printf("Focal length of a lens is: %f
", focal_length(image_distance, object_distance));    return 0; }

輸出

Focal length of a lens is: 3.333333

更新於: 2019年12月20日

326 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.