杭州四中基础题题解(Python+Haskell)
前言
这些代码都是校本教材上面的习题,有的代码我可能偷懒就直接用轮子了,不保证和老师要求的一模一样。
本文将持续更新
第一节
T1
依次打出结果即可
a = int(input())
print(a + 8, int((a + 8) / 5), int((a + 8) / 5 - 10), int(((a + 8) / 5 - 10) * 10))main = do
a :: Int <- fmap read getLine
putStrLn (show (a + 8) ++ " " ++ show ((a + 8) `div` 5) ++ " " ++ show ((a + 8) `div` 5 - 10) ++ " " ++ show (((a + 8) `div` 5 - 10) * 10))T2
依次打出结果即可,Python 当中记得将分隔符 sep 改为逗号。
a = int(input())
b = int(input())
print(a + b, a - b, a * b, a / b, sep=",")main = do
a <- fmap read getLine
b <- fmap read getLine
putStrLn (show (a + b) ++ "," ++ show (a - b) ++ "," ++ show (a * b) ++ "," ++ show (a / b))T3
按要求格式打出即可。
a = int(input())
b = int(input())
print(str(a) + "+" + str(b) + "=" + str(a + b))main = do
a <- fmap read getLine
b <- fmap read getLine
putStrLn (show a ++ "+" ++ show b ++ "=" ++ show (a + b))T4
按要求分行打出。
a = int(input())
b = int(input())
c = int(input())
print(a * b, a * c, b * c)
print(a * b * c)main = do
a <- fmap read getLine
b <- fmap read getLine
c <- fmap read getLine
putStrLn (show (a * b) ++ " " ++ show (a * c) ++ " " ++ show (b * c))
print (a * b * c)第二节
T1
根据公式计算即可。
r = float(input())
print(2 * 3.14 * r)
print(3.14 * r * r)main = do
r <- fmap read getLine
print (3.14 * r * 2)
print (3.14 * r * r)T2
根据公式计算即可。
r = float(input())
print(4 * 3.14 * r)
print(4 * r * r * r * 3.14 / 3)main = do
r <- fmap read getLine
print (4 * 3.14 * r)
print (4 * r * r * r * 3.14 / 3)T3
首先如果我们令原数为 a。a 的个位是(a%10),将其乘上 100;a 的十位是(a//10)%10,将其乘上 10;a 的百位是(a//100),将其乘上 1:这三个值相加得到的结果,就是 a 百位和个位调换的结果(可以自己模拟一下),然后加上 a 输出就得到答案了。
a = int(input())
print(a + (a % 10) * 100 + (a // 10) % 10 * 10 + a // 100)main = do
a <- fmap read getLine
print (a + (a `mod` 10) * 100 + (a `div` 10) `mod` 10 * 10 + a `div` 100)T4
以一个正常身份证号码为例,应该是 xxxxxxyyyymmddxxxx 的格式(x 代表其他字符,yyyy 代表年份,mm 代表月份,dd 代表日期),如果我们将这个东西除以 10000 取整,则得到 xxxxxxyyyymmdd,如果再将这个东西模 100000000,即可得到 yyyymmdd,即为所求。
id = int(input())
print(id // 10000 % 100000000)main = do
id <- fmap read getLine
print ((id `div` 10000) `mod` 100000000)第三节
T1
根据题意判断输出即可,此处我用了三目表达式,格式是:(statement-true) if (condition) else (statement-false)。
a = int(input())
b = int(input())
print(1 if a > b else 2 if a < b else 3)main = do
a :: Int <- fmap read getLine
b :: Int <- fmap read getLine
print
( if a > b
then 1
else
if a < b
then 2
else 3
)T2
正常的做法应该是打擂法,但是不够快,直接调max()函数就得了。
a = int(input())
b = int(input())
c = int(input())
print(max(a, b, c))main = do
[a :: Int, b :: Int, c :: Int] <- traverse (fmap read) [getLine, getLine, getLine]
print (maximum [a, b, c])T3
分层输出。
score = int(input())
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("E")-- along with this program. If not, see <https://www.gnu.org/licenses/>.
getGrade :: Int -> Char
getGrade score
| score >= 90 = 'A'
| score >= 80 = 'B'
| score >= 70 = 'C'
| score >= 60 = 'D'
| otherwise = 'E'
main = do
score <- fmap read getLine
putChar $ getGrade score
putChar '\n'T4
如果是 Haskell 就直接 sort 后打印就得了,Python 的做法和教材上写的一样。
a = int(input())
b = int(input())
c = int(input())
print(min(a, b, c), a + b + c - min(a, b, c) - max(a, b, c), max(a, b, c))import Data.List
main = do
[a :: Int, b :: Int, c :: Int] <- traverse (fmap read) [getLine, getLine, getLine]
putStrLn (foldl (\cur x -> cur ++ show x ++ " ") [] (sort [a, b, c]))第四节
T1
和前面一样,我这里用了三目,分开判断输出即可。
a = int(input())
print("YES" if a <= 999 and a >= 100 and a % 2 == 1 else "NO")main = do
a <- fmap read getLine
if a <= 999 && a >= 100 && a `mod` 2 == 1
then putStrLn "YES"
else putStrLn "NO"T2
根据题意打印即可。
lucky = int(input())
good = int(input())
if lucky >= 10 or good >= 20:
print(1)
else:
print(2)main = do
lucky <- fmap read getLine
good <- fmap read getLine
if lucky >= 10 || good >= 20
then putStrLn "1"
else putStrLn "0"T3
我使用了max和min先分别搞出最大最小值,然后综合减去最大最小就是中间值,判断输出即可。
a = int(input())
b = int(input())
c = int(input())
mi = min(a, b, c)
ma = max(a, b, c)
mid = a + b + c - mi - ma
print("YES" if mi + mid > ma else "NO")import Data.List
main = do
[a, b, c] <- traverse (fmap read) [getLine, getLine, getLine]
let chk = sum $ take 2 (sort [a, b, c])
if c < chk
then putStrLn "YES"
else putStrLn "NO"T4
按照闰年立法判断即可。
year = int(input())
print("YES" if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0) else "NO")main = do
year <- fmap read getLine
if (year `mod` 400 == 0) || (year `mod` 100 /= 0 && year `mod` 4 == 0)
then putStrLn "YES"
else putStrLn "NO"T5
算出最大最小,减去然后除以二输出即可。
a = int(input())
b = int(input())
c = int(input())
d = int(input())
print((a + b + c + d - min(a, b, c, d) - max(a, b, c, d)) / 2)main = do
[a, b, c, d] <- traverse (fmap read) [getLine, getLine, getLine, getLine]
print ((a + b + c + d - maximum [a, b, c, d] - minimum [a, b, c, d]) / 2)第五课
T1
先利用三目判断一下奇偶性,如果是奇数设为 n-1,如果是偶数则保留,然后以-2 为步进向前循环。
n = int(input())
for x in range(n - 1 if n % 2 == 1 else n, 1, -2):
print(x)main = do
n <- fmap read getLine
let realN = n `div` 2
putStrLn $ foldl (\cur x -> cur ++ show x ++ " ") "" [x * 2 | x <- [realN, realN - 1 .. 1]]T2
for 输出[a,b)之间所有数,以逗号隔开,然后对 b 进行特殊处理(防止末尾多一个逗号)
a = int(input())
b = int(input())
for x in range(a, b):
print(x, end=",")
print(b)main = do
[a, b] <- traverse (fmap read) [getLine, getLine]
putStr $ foldl (\cur x -> cur ++ show x ++ ",") "" [a, a + 1 .. b - 1]
print bT3
可以依次以对应的值为步进进行 for,但是我这里不太一样,我是枚举 size 的系数,然后对应相乘后相加。
begin = int(input())
size = int(input())
for x in range(1, 10):
print(begin + size * x, end=",")
print(begin + size * 10)main = do
[begin, dep] <- traverse (fmap read) [getLine, getLine]
putStr $ foldl (\cur x -> cur ++ show x ++ ",") "" [begin, begin + dep .. begin + 9 * dep]
print (begin + 10 * dep)T4
暴力办法,对 x 的因子进行一个个判断。
当然,这不是最好的方法,只是在学考(甚至选考)当中最简洁明了的办法。理论上由于因子成对出现,所以我只要循环到
n = int(input())
for x in range(2, n):
if n % x == 0:
print(x)main = do
n <- fmap read getLine
putStrLn $ foldl (\cur x-> cur++show x++" ") "" [x|x<-[2..n-1],n`mod`x==0]T5
判定素数首先也只要判断到
n = int(input())
for x in range(2, n):
if n % x == 0:
print("NO")
exit()
print("YES")main = do
n <- fmap read getLine
if null [x|x<-[2..n-1],n`mod`x==0]
then putStrLn "YES"
else putStrLn "NO"第六课
T1
while 死除就行
n = int(input())
cnt = 0
while n > 0:
n = n // 10
cnt = cnt + 1
print(cnt)log10 :: Int -> Int
log10 0 = 0
log10 a = log10 (a `div` 10) + 1
main = do
a <- fmap read getLine
print (log10 a)T2
n = int(input())
x = 0
while n % (2 ** (x + 1)) == 0:
x = x + 1
print(x)func :: Int -> Int -> Int
func n a =
if n `mod` (2 ^ (a + 1)) == 0
then func n (a + 1)
else a
main = do
a <- fmap read getLine
print (func a 1)T3
n = int(input())
sum = 0
while n > 0:
sum = sum + n % 10
n = n // 10
print(sum)numSum :: Int -> Int
numSum 0 = 0
numSum n = numSum (n `div` 10) + (n `mod` 10)
main = do
a <- fmap read getLine
print $ numSum aT4
n = int(input())
rev = 0
while n > 0:
rev = rev * 10
rev = rev + n % 10
n = n // 10
print(rev)main = do
a <- getLine
putStrLn $ reverse a暑假作业
T1
n = int(input())
for x in range(2, n):
if n % x == 0:
print(x)T2
# 由于题设要求,我这段代码用了搜索法,但是这不是个好办法,正确的方法应该用辗转相除
a = int(input())
b = int(input())
m = min(a, b)
ans = 0
for x in range(1, m):
if (a % x == 0) & (b % x == 0):
ans = max(ans, x)
print(ans, end="\n")T3
# Note: This is a O(n) program to judge a prime(Obviously correct), but
# Miller-Rabin Algorithm(May not correct) will be a better choice
n = int(input())
for x in range(2, n):
if n % x == 0:
print("NO")
exit()
print("YES")T4
n = int(input())
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
if sum == n:
print("YES")
else:
print("NO")T5
n = int(input())
for x in range(100, n + 1):
if (
(x // 100) * (x // 100) * (x // 100)
+ (x // 10 % 10) * (x // 10 % 10) * (x // 10 % 10)
+ (x % 10) * (x % 10) * (x % 10)
) == x:
print(x)T6
a = int(input())
b = int(input())
cnt = 0
for x in range(a, b + 1):
if x // 100 == 1:
cnt += 1
if x // 10 % 10 == 1:
cnt += 1
if x % 10 == 1:
cnt += 1
print(cnt)T7
n = int(input())
flag = True
ans = 0
for x in range(1, n + 1):
if flag:
ans += x
flag = False
else:
ans -= x
flag = True
print(ans)T8
# This algorithm is naive. Use Euler's Sieve or Eratosenes' Sieve will be a better choice
n = int(input())
for x in range(2, n):
if n % x == 0:
flag = True
for y in range(2, x):
if x % y == 0:
flag = False
break
if flag:
print(x, end=" ")
print()T9
l1 = 1
l2 = 1
n = int(input())
for x in range(1, n):
tmp = l1 + l2
l1 = l2
l2 = tmp
print(l1)T10
def sol(lis, res: int, cnt: int):
if cnt > 5:
return
sum = 0
for x in lis:
sum += x
if sum > res:
return
if cnt == 5 and sum == res:
print(lis)
return
sol(lis + [10], res, cnt + 1)
sol(lis + [5], res, cnt + 1)
sol(lis + [2], res, cnt + 1)
sol(lis + [1], res, cnt + 1)
sol([], 7, 0)T11
def sol(res: int, lis):
if res == 0:
print(lis)
return
if res < 5:
return
sol(res - 5, lis + [5])
sol(res - 12, lis + [12])
sol(120, [])T12
n: int = int(input())
k: int = int(input())
ans: int = 0
while n > k:
ans += n
n = n % k + n // k
print(ans)T13
a = int(input())
Nega = False
if a < 0:
Nega = True
a = -a
while a % 10 == 0:
a //= 10
lis = []
while a > 0:
lis.append(a % 10)
a //= 10
if Nega:
print("-", end="")
for x in lis:
print(x, end="")
print()T14
def gcd(a: int, b: int) -> int:
if a == 0:
return b
elif b == 0:
return a
else:
return gcd(b, a % b)
a = int(input())
b = int(input())
print(gcd(a, b))T15
n = int(input())
for x in range(1, n + 1):
for y in range(n - x):
print(end=" ")
for y in range(1, 2 * x):
print(y, end="")
print()T16
n = int(input())
for x in range(1, n + 1):
for y in range(n - x):
print(end=" ")
for y in range(2 * x - 1):
print("*", end="")
print()
for x in range(n - 1, 0, -1):
for y in range(n - x):
print(end=" ")
for y in range(2 * x - 1):
print("*", end="")
print()LICENSE
本文出现的所有代码均以 GPLv3 开源。
Copyright (C) 2023 Andy Shen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.