Try   HackMD

4-4 3個浮點數

CHAWTeamFeb 16, 2022


目錄:DICE C語言程式破解
上一篇:4-3 浮點數
下一篇:4-5 長方形


題目

寫一個程式,輸入3個浮點數,然後印出該浮點數到指定的小數位數。

第一個數印到小數點後第一位,第二個數印到小數點後第二位,第三個數印到小數點後第三位,採用四捨五入(如輸出範例)。

輸入範例:
2.333
3.666
6.333

輸出範例:
2.3
3.67
6.333

提醒使用C++的學習者,以小數點後6位為範例,寫出以下程式碼,請參考或複製使用。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float num;
cin>>num;
cout<<fixed<<setprecision(6)<<num;
return 0;
}

提示

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

程式碼

範例1

#include <stdio.h> int main() { float a,b,c; scanf("%f\n%f\n%f",&a,&b,&c); printf("%.1f\n%.2f\n%.3f", a, b, c); return 0; }

範例2

#include <stdio.h> int main() { float a, b, c; scanf("%f %f %f", &a, &b, &c); printf("%.1f\n%.2f\n%.3f", a, b, c); return 0; }

輸出

2.333
3.666
6.333
2.3
3.67
6.333

查看我們在HackMD上的所有筆記
目錄:DICE C語言程式破解
上一篇:4-3 浮點數
下一篇:4-5 長方形


👉【幫我們一個忙!】👈

👋如果您喜歡這篇文章,請在下方按5個Like!
❤您的支持是我們最大的動力!

您只要登入帳號(Facebook、Google),在下方按5個Like,我們就會收到來自LikeCoin基金會的贊助。
您只需要支持我們,完全不會花到錢!