快轉到主要內容

ITSA 基礎題庫 —— 題目02. 英哩轉公里

·80 字·1 分鐘·
ChengHao Yang
作者
ChengHao Yang
SRE / CNCF Ambassador
目錄

題目 Problem
#

題目連結:https://etutor2.itsa.org.tw/mod/topics/view.php?id=15903

敘述 Description
#

試撰寫一程式,可由鍵盤輸入英哩,程式的輸出為公里,其轉換公式如下: 1 英哩= 1.6 公里

輸入 Input
#

輸入欲轉換之英哩數(int)。

輸出 Output
#

輸出公里(double),取到小數點以下第一位。

範例輸入 Sample Input
#

90
95

範例輸出 Sample Output
#

144.0
152.0

提示 Hint
#

題解 Solution
#

題目需要以整數 (int) 讀取,以雙倍精度浮點數 (double) 輸出,因此會需要強制轉換型態後乘上 1.6

printf 就會需要以 lf 輸出,但這樣還不夠,要取到小數點後一位,則需要 .1 標示取得位數長度。

程式碼 Accepted Code
#

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int km;
    scanf("%d", &km);
    printf("%.1lf\n", (double)km * 1.6);
	  return 0;
}

後記 Afterword
#

ChatGPT 當道的時代,還會有人需要看 blog 嗎?我其實很意外的是,ITSA 基礎題庫的點閱比我其他文章還高?

相關文章