第1章 程序設(shè)計(jì)入門(mén)

1.1 算數(shù)表達(dá)式


1.2 變量及其輸入


1.3 順序結(jié)構(gòu)設(shè)計(jì)程序

  • 例題 1-3 交換變量
    輸入兩個(gè)整數(shù) a, b,交換二者的值,然后輸出
    1. 解法一:
      a = a+b;
      b = a-b;
      a = a-b;
      只有定義了減法的數(shù)據(jù)結(jié)構(gòu)才能使用
    2. 解法二:
      a^=b^=a^=b;
      好強(qiáng)。好蠢。
    3. 解法三:
      int a, b;
      printf("%d %d", b, a);

1.4 分支結(jié)構(gòu)程序設(shè)計(jì)


1.5 注解與習(xí)題(python 實(shí)現(xiàn))

  1. 輸入三個(gè)整數(shù)求平均數(shù),保留三位小數(shù)
a = int(input())
b = int(input())
c = int(input())
print("%.3f" % ((a + b + c)/3))
# ((a+b+c)/3) 要加括號(hào) 有優(yōu)先級(jí)問(wèn)題
  1. 華氏度轉(zhuǎn)攝氏度,保留三位小數(shù)(c = 5(f-32)/9)
f = float(input())
print("%.3f" % (5 * (f-32) /9))
  1. 連續(xù)和 sum
x = int(input())
sum = 0
for i in range(x):
       sum += i
print(sum)
  1. 正弦和余弦:輸入正整數(shù) n(n<360),輸出 n 度的正余弦函數(shù)值
import mathn = int(input())
print("正弦值: %.2f" % math.sin(math.radians(n)))
print("余弦值: %.2f" % math.cos(math.radians(n)))
# 如果不取兩位小數(shù)會(huì)有精度問(wèn)題
  1. 打折:一件衣服95元,消費(fèi)滿(mǎn)300元打85折,輸入購(gòu)買(mǎi)件數(shù)輸出金額(兩位小數(shù))
price = 95x = int(input())
if 95 * x > 300:    
       print("%.2f" % (95 * x * 0.85))
else:    
       print("%.2f" % (95 * x))
  1. 三角形:輸入三角形的三邊長(zhǎng),如果能構(gòu)成直角三角形輸出 yes 否則輸出 no,如果不能構(gòu)成三角形輸出 not a triangle
a = int(input())
b = int(input())
c = int(input())
triangleEdge = [a, b, c]
triangleEdge.sort(reverse=True)
a, b, c = triangleEdge[:3]
if b + c <= a:
    print('not a triangle')
elif a * a == b * b + c * c:
    print('yes')
else:
    print('no')

7.判斷閏年

year = int(input())
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print('yes')
else:
    print('no')

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容