1.1 算數(shù)表達(dá)式
1.2 變量及其輸入
1.3 順序結(jié)構(gòu)設(shè)計(jì)程序
- 例題 1-3 交換變量
輸入兩個(gè)整數(shù) a, b,交換二者的值,然后輸出- 解法一:
a = a+b;
b = a-b;
a = a-b;
只有定義了減法的數(shù)據(jù)結(jié)構(gòu)才能使用 - 解法二:
a^=b^=a^=b;
好強(qiáng)。好蠢。 - 解法三:
int a, b;
printf("%d %d", b, a);
- 解法一:
1.4 分支結(jié)構(gòu)程序設(shè)計(jì)
1.5 注解與習(xí)題(python 實(shí)現(xiàn))
- 輸入三個(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)題
- 華氏度轉(zhuǎn)攝氏度,保留三位小數(shù)(c = 5(f-32)/9)
f = float(input())
print("%.3f" % (5 * (f-32) /9))
- 連續(xù)和 sum
x = int(input())
sum = 0
for i in range(x):
sum += i
print(sum)
- 正弦和余弦:輸入正整數(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)題
- 打折:一件衣服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))
- 三角形:輸入三角形的三邊長(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')