给定一个整数像素,以像素为单位表示框的宽度,任务是将像素转换为以rem和em单位度量的等效值。
例子:
Input: pixel = 45
Output: em = 2.812500, rem = 2.812500
Input: pixel = 160
Output: em = 10, rem = 10
方法:可以根据以下数学关系来解决问题:
For modern browsers,
- 1 rem = 16 px
Therefore, 1px = 0.0625 rem - 1 em = 16 px
Therefore, 1 px = 0.0625 em
请按照以下步骤解决问题:
- 通过将像素乘以0.0625来计算像素的rem和em中的等效值。
- 以rem和em为单位打印等效的像素值。
下面是上述方法的实现:
C++
// C++ program to convert
// pixels to rem and em
#include
using namespace std;
// Function to convert
// pixels to rem and em
void Conversion(double pixels)
{
double rem = 0.0625 * pixels;
double em = 0.0625 * pixels;
cout << fixed << setprecision(6)
<< "The value in em is " << em << endl;
cout << fixed << setprecision(6)
<< "The value in rem is " << rem << endl;
}
// Driver Code
int main()
{
// Input
double PX = 45;
Conversion(PX);
return 0;
}
Java
// Java program to convert pixel to rem and em
import java.io.*;
class GFG {
// Function to convert pixel to rem and em
static double Conversion(double pixel)
{
double rem = 0.0625 * pixel;
double em = 0.0625 * pixel;
System.out.println("The value in rem is " + rem);
System.out.println("The value in em is " + em);
return 0;
}
// Driver Code
public static void main(String args[])
{
double pixels = 45;
Conversion(pixels);
}
}
Python
# Python program to convert pixel to rem and em
# Function to convert pixel to rem and em
def Conversion(pixel):
rem = 0.0625 * pixel
em = 0.0625 * pixel
print "The value in em is", round(em, 2)
print "The value in rem is", round(rem, 2)
# Driver Code
pixel = 45
Conversion(pixel)
C#
// C# program to convert pixel to rem and em
using System;
class GFG {
// Function to convert pixel to rem and em
static double Conversion(double pixel)
{
double rem = 0.0625 * pixel;
double em = 0.0625 * pixel;
Console.WriteLine("The value in em is " + em);
Console.WriteLine("The value in rem is " + rem);
return 0;
}
// Driver Code
public static void Main()
{
double pixel = 45;
Conversion(pixel);
}
}
PHP
输出:
The value in em is 2.812500
The value in rem is 2.812500
时间复杂度: O(1)
辅助空间: O(1)