PAT基础编程题目-7-1 厘米换算英尺英寸
PAT基础编程题目-7-1 厘米换算英尺英寸
题目详情
解答
C语言版
#include<stdio.h>
void Conversion(int cm);
int main() {
int cm;
scanf("%d", &cm);
Conversion(cm);
return 0;
}
void Conversion(int cm) {
int foot, inch;
float m;
m = (cm / 100.0) / 0.3048; //换算成米再计算
foot = m;
inch = (m - foot) * 100;
if (inch % 10 >= 5) //四舍五入
inch = (m - foot) * 10 + 1;
else
inch = (m - foot) * 10;
printf("%d %d", foot, inch);
}
C++版
#include<iostream>
using namespace std;
void Conversion(int cm);
int main() {
int cm;
cin >> cm;
Conversion(cm);
return 0;
}
void Conversion(int cm) {
int foot, inch;
float m;
m = (cm / 100.0) / 0.3048; //换算成米再计算
foot = m;
inch = (m - foot) * 100;
if (inch % 10 >= 5) //四舍五入
inch = (m - foot) * 10 + 1;
else
inch = (m - foot) * 10;
cout << foot << " "<< inch;
}
Java版
import java.util.Scanner;
public class Main{
private static void conversion(int cm) {
int foot, inch;
double m;
m = ((cm / 100.0) / 0.3048);
foot = (int)m;
inch = (int)((m-foot)*100);
if( inch % 10 >= 5)
inch = (int) ((m - foot) * 10 + 1);
else
inch = (int) (m - foot) * 10;
System.out.println(foot+" "+inch);
}
public static void main(String[] args) {
int cm=0;
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext())
cm = scanner.nextInt();
scanner.close();
conversion(cm);
}
}
通过上面的耗时对比来看,C语言确实是最快的,其次是C++,Java相比C来说,确实要慢太多了。
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!