给定一个整数厘米(代表以厘米为单位的长度),任务是将其转换为以像素为单位的等效值。
例子:
Input: centimeter = 10
Output: pixels = 377.95
Input : centimeter = 5.5
Output : pixels = 207.87
方法:可以根据以下数学关系来解决问题:
1 inch = 96 px
1 inch = 2.54 cm
Therefore, 2.54 cm = 96 px
=> 1 cm = ( 96 / 2.54 ) px
=> N cm = ( ( N * 96) / 2.54 ) px
下面是上述方法的实现:
C++
// C++ program to convert centimeter to pixels
#include
using namespace std;
// Function to convert
// centimeters to pixels
void Conversion(double centi)
{
double pixels = (96 * centi) / 2.54;
cout << fixed << setprecision(2) << pixels;
}
// Driver Code
int main()
{
double centi = 15;
Conversion(centi);
return 0;
}
Java
// Java program to convert
// centimeter to pixels
import java.io.*;
class GFG {
// Function to convert
// centimeters to pixels
static double Conversion(double centi)
{
double pixels = (96 * centi) / 2.54;
System.out.println(pixels);
return 0;
}
// Driver Code
public static void main(String args[])
{
int centi = 15;
Conversion(centi);
}
}
Python3
# Python program to convert
# centimeters to pixels
# Function to convert
# centimeters to pixels
def Conversion(centi):
pixels = ( 96 * centi)/2.54
print ( round(pixels, 2))
# Driver Code
centi = 15
Conversion(centi)
C#
// C# program to convert
// centimeter to pixels
using System;
class GFG {
// Function to convert
// centimeter to pixels
static double Conversion(double centi)
{
double pixels = (96 * centi) / 2.54;
Console.WriteLine(pixels);
return 0;
}
// Driver Code
public static void Main()
{
double centi = 15;
Conversion(centi);
}
}
PHP
输出:
566.93
时间复杂度: O(1)
辅助空间: O(1)