iT邦幫忙

5

[自學Python紀錄] HackerRank 新手30天挑戰-Day01

Hi there! 我是嘟嘟~受到前輩啟發,想說可以紀錄一下自己練習的過程,小女子為程式超超菜鳥,此系列非教學文,僅為個人解題筆記,可能有錯誤或未補充詳盡之處,歡迎前輩們不吝指教!也歡迎正在自學的夥伴一起討論學習~


Day 1: Data Types

題目已宣告3個變量i,d,s,要求宣告3個變量符合下列規則。

輸入格式

第一行包含一個必須與i相加的整數。
第二行包含一個必須與d加總的double(浮點數) 。
第三行包含一個必須與s連接的字符串。

輸出格式

在第一行上打印兩個整數的總和,在第二行上打印兩個雙精度數的總和(到小數點第1位) (scaled to 1 decimal place) ,然後在第三行上打印兩個串聯的字符串。

樣本輸入

12
4.0
is the best place to learn and practice coding!

樣本輸出

16
8.0
HackerRank is the best place to learn and practice coding!

  
  
 

初始格式

i = 4
d = 4.0
s = 'HackerRank '
# Declare second integer, double, and String variables.

# Read and save an integer, double, and String to your variables.

# Print the sum of both integer variables on a new line.

# Print the sum of the double variables on a new line.

# Concatenate and print the String variables on a new line
# The 's' variable above should be printed first.

  
 
 

我的解答

i = 4
d = 4.0
s = 'HackerRank '

a = int(i) + int(input())
b = float(d) + float(input())
c = s + input()

print(a)
print(round(b,1))
print(c)

  
 

補充:

  • 此處double指的是C++語言中的雙倍精度浮點數,類似於Python的float浮點數的意思。

  • 題目有寫印出的第二個浮點數要取到小數點第一位,打文章才發現,故加上round(),可以參考文章【Python如何控制小數點後面的小數位數


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言