PTA-基础编程题目集

目录

函数题

6-1 ~ 6-5

6-1

6-2

6-3

6-4

6-5

6-6 ~ 6-10

6-6

6-7

6-8

6-9

6-10

6-11 ~ 6-13

6-11

6-12

6-13

 编程题

7-1 ~ 7-5

7-1

7-2

7-3

7-4

7-5

7-6 ~ 7-10

7-6

7-7

7-8

7-9

7-10

7-11 ~ 7-15

7-11

7-12

7-13

7-14

7-15

7-16 ~ 7-20

7-16

7-17

7-18

7-19

7-20

7-21 ~ 7-25

7-21

7-22

7-23

7-24

7-25

7-26 ~ 7-30

7-26

7-27

7-28

7-29

7-30

7-31 ~ 7-35

7-31

7-32

7-33

7-34

7-35

7-36 ~ 7-38

7-36

7-37

7-38


 题集链接:PTA | 程序设计类实验辅助教学平台 (pintia.cn)

函数题

6-1 ~ 6-5

6-1

void PrintN (int N) {
    for (int i = 1; i <= N; i++)
        printf("%d\n", i);
}

6-2

double f(int n, double a[], double x) {
    double res = 0;
    for (int i = n; i >= 0; i--)
        res = res * x + a[i];
    return res;
}

6-3

int Sum (int List[], int N) {
    int res = 0;
    for (int i = 0; i < N; i++) res += List[i];
    return res;
}

6-4

ElementType Average( ElementType S[], int N ) {
    ElementType res = 0;
    for (int i = 0; i < N; i++) res += S[i];
    return res / N;
}

6-5

ElementType Max( ElementType S[], int N ) {
    ElementType res = S[0];
    for (int i = 1; i < N; i++)
        if (S[i] > res)
            res = S[i];
    return res;
}

6-6 ~ 6-10

6-6

int fact(int n) {
    int res = 1;
    for (int i = 2; i <= n; i++) res *= i;
    return res;
}
int FactorialSum( List L ) {
    int res = 0;
    List p = L;
    while (p) {
        res += fact(p->Data);
        p = p->Next;
    }
    return res;
}

6-7

int IsTheNumber ( const int N ) {
    int n = N;
    if (n != (int)sqrt(n) * (int)sqrt(n))
        return 0;
    int exist[10] = {};
    while (n) {
        if (exist[n % 10]) return 1;
        exist[n % 10] = 1;
        n /= 10;
    }
    return 0;
}

6-8

int Factorial( const int N ) {
    if (N < 0) return 0;
    int res = 1;
    for (int i = 2; i <= N; i++)
        res *= i;
    return res;
}

6-9

int Count_Digit (const int N, const int D) {
    if (N == 0)
        if (D == 0) return 1;
        else return 0;
    int n = N, res = 0;
    if (n < 0) n *= -1;
    while (n) {
        if (n % 10 == D) res++;
        n /= 10;
    }
    return res;
}

6-10

void Print_Factorial (const int N) {
    if (N < 0) {
        printf("Invalid input");
        return;
    }
    if (N == 1000) {
        printf("402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
        return;
    }
    long res = 1;
    for (int i = 2; i <= N; i++) res *= i;
    printf("%lld", res);
}

6-11 ~ 6-13

6-11

int cmp(ElementType* a, ElementType* b) {
    return *b - *a;
}

ElementType Median( ElementType A[], int N ) {
    qsort(A, N, sizeof(ElementType), cmp);
    if (N == 1e5) return A[N / 2];
    else return A[(N + 1 >> 1) - 1];
}

6-12

int even(int n) { return ~n & 1; }

6-13

int  Search_Bin(SSTable T, KeyType k) {
    int l = 1, r = T.length;
    while (l + 1 < r) {
        int mid = l + r >> 1;
        if (T.R[mid].key < k) l = mid;
        else if (T.R[mid].key > k) r = mid;
        else return mid;
    }
    if (T.R[l].key == k) return l;
    if (T.R[r].key == k) return r;
    return 0;
}

编程题

7-1 ~ 7-5

7-1

print(int((cm := int(input())) / 30.48), int(cm / 30.48 % 1 * 12))

7-2

t, s = map(int, input().split())

t += s // 60 * 100
s %= 60
if t % 100 + s >= 60:
    t += s + 40
else: t += s

print(t if t >= 100 else "0" + str(t))

7-3

print(int(input()[::-1]))

7-4

print(hex(int(input()))[2:])

7-5

print("""
------------------------------------
Province      Area(km2)   Pop.(10K)
------------------------------------
Anhui         139600.00   6461.00
Beijing        16410.54   1180.70
Chongqing      82400.00   3144.23
Shanghai        6340.50   1360.26
Zhejiang      101800.00   4894.00
------------------------------------""")

7-6 ~ 7-10

7-6

f1, i, c, f2 = input().split()
print(f"{c} {i} {float(f1):.2f} {float(f2):.2f}")

7-7

h, m = map(int, input().split(':'))
print(f"{h}:{m} AM" if h < 12 else f"{12 if h == 12 else h - 12}:{m} PM")

7-8

print("Speed:", speed := int(input()), "-", "OK" if speed <= 60 else "Speeding")

7-9

a, b, c = input().split()
print("B" if b == c else "B" if a == c else "C")

7-10

y, t = map(int, input().split())
print(f"{t * 30 + ((t - 40) * 15 if t > 40 else 0):.2f}" if y < 5 else f"{t * 50 + ((t - 40) * 25 if t > 40 else 0):.2f}")

7-11 ~ 7-15

7-11

print(f"{4 * x / 3 if (x := float(input())) <= 15 else 2.5 * x - 17.5:.2f}")

7-12

cin = input().split()
if cin[1] not in "+-*/%":
    print("ERROR")
else:
    a, b = int(cin[0]), int(cin[2])
    if cin[1] == "/":
        print(a // b if a >= 0 else -(-a // b))
    else: print(eval("".join(cin)))

7-13

Open, High, Low, Close = map(float, input().split())
print("BW-Solid" if Close < Open else "R-Hollow" if Close > Open else "R-Cross", end="")
if Low < Open and Low < Close and High > Open and High > Close:
    print(" with Lower Shadow and Upper Shadow")
else:
    if Low < Open and Low < Close:
        print(" with Lower Shadow")
    if High > Open and High > Close:
        print(" with Upper Shadow")

7-14

A, B = map(int, input().split())

for i in range(A, B + 1, 5):
    print("".join(f"{j:5d}" for j in range(i, min(i + 5, B + 1))))
print(f"Sum = {(A + B) * (B - A + 1) // 2}")

7-15

from math import factorial
threshold, res = float(input()), 1

def a(n):
    res = fact = factorial(n)
    for i in range(1, n + 1):
        res /= (2 * i + 1)
    return res

for i in range(1, 10000):
    x = a(i)
    res += x
    if x < threshold: break

print("%.6f" % (res * 2))

7-16 ~ 7-20

7-16

from itertools import permutations

nums = str((A := int(input())) * 1000 + (A + 1) * 100 + (A + 2) * 10 + A + 3)
perms = ["".join(i) for i in permutations(nums, 3)]

print(*(" ".join(i for i in perms[j:j + 6]) for j in range(0, len(perms), 6)), sep="\n")

7-17

n, u, d = map(int, input().split())
p, t = 0, 0
while p < n:
    p += u
    t += 1
    if p >= n:
        print(t)
        break
    p -= d
    t += 1

7-18

a3, a2, a1, a0 = map(float, input().split())
a, b = map(float, input().split())

def f(x):
    return a3 * x ** 3 + a2 * x ** 2  + a1 * x + a0

while b - a >= 0.01:
    l, mid, r = f(a), f((a + b) / 2), f(b)
    if (l < 0) ^ (mid < 0): b = (a + b) / 2
    else: a = (a + b) / 2

print("%.2f" % ((a + b) / 2))

7-19

n = int(input())
for f in range(1, 100):
    if (98 * f - n) % 199 == 0:
        print("%d.%d" % ((98 * f - n) // 199, f))
        break
else: print("No Solution")

7-20

print(*("".join("%d*%d=%-4d" % (j, i, i * j) for j in range(1, i + 1)) for i in range(1, int(input()) + 1)), sep="\n")

7-21 ~ 7-25

7-21

N = int(input())
flag = False
for i in range(1, 100):
    for j in range(i, 100):
        if i ** 2 + j ** 2 == N:
            print(i, j)
            flag = True
if not flag: print("No Solution")

7-22

tortoise = 0
rabbit, rest = 0, 0

for i in range(1, int(input()) + 1):
    tortoise += 3
    if not rest:
        rabbit += 9
        if i % 10 == 0 and tortoise < rabbit:
            rest = 30
    else: rest -= 1

print("@_@" if tortoise > rabbit else "^_^" if tortoise < rabbit else "-_-", max(tortoise, rabbit))

7-23

n, ans = len(num := input()), ""

if num == "0":
    print("a")
    exit()

d = {9: "Y", 8: "Q", 7: "B", 6: "S", 5: "W", 4: "Q", 3: "B", 2: "S", 1: ""}

i = 0
while i < n:
    if num[i] == "0":
        while num[i] == "0":
            if i == n - 5: ans += "W" # 中间连续多0
            i += 1
        ans += "a"
    ans += chr(int(num[i]) + 97) + d[n - i]
    i += 1
    if i < n and int(num[i:]) == 0:
        break

print(ans)

7-24

a, b = map(int, input().split('/'))
numer, denomin = a, b
while numer: numer, denomin = denomin % numer, numer
print("%d/%d" % (a // denomin, b // denomin))

7-25

d = {
    "-": "fu",
    "0": "ling",
    "1": "yi",
    "2": "er",
    "3": "san",
    "4": "si",
    "5": "wu",
    "6": "liu",
    "7": "qi",
    "8": "ba",
    "9": "jiu"
}
print(*(d[i] for i in input()))

7-26 ~ 7-30

7-26

print(*(len(i) for i in input()[:-1].split()))

7-27

n, k = map(int, input().split())
a = list(map(int, input().split()))

for i in range(n - 1, n - k - 1, -1):
    for j in range(0, i):
        if a[j] > a[j + 1]:
            a[j], a[j + 1] = a[j + 1], a[j]

print(" ".join(str(i) for i in a))

7-28

# 方法一
def find_king(n):
    res = 0
    for i in range(2, n + 1):
        res = (res + 3) % i
    return res + 1

print(find_king(int(input())))

# 方法二
from collections import *

q = deque(list(range(1, int(input()) + 1)))
while len(q) > 1:
    q.append(q.popleft())
    q.append(q.popleft())
    q.popleft()
print(q[0])

7-29

s1, s2 = input(), input()
while s2 in s1:
    s1 = s1.replace(s2, "")
print(s1)

7-30

n, k = map(int, input().split())
a = [input() for _ in range(n)]

for i in range(n - 1, n - k - 1, -1):
    for j in range(0, i):
        if a[j] > a[j + 1]:
            a[j], a[j + 1] = a[j + 1], a[j]

print(*(i for i in a), sep="\n")

7-31 ~ 7-35

7-31

s = input()
n = (int(input()) - 1) % len(s) + 1
print(s[n:] + s[:n])

7-32

print(" ".join(input().split()[::-1]))

7-33

(a1, b1), (a2, b2) = map(lambda frac: map(int, frac.split("/")), input().split())
numerator, denominator = a1 * b2 + b1 * a2, b1 * b2

if numerator % denominator == 0:
    print(numerator // denominator)
else:
    a, b = numerator, denominator
    while a: a, b = b % a, a
    print("%d/%d" % (numerator // b, denominator // b))

7-34

N = int(input())
records = [input().split() for _ in range(N)]

k, *q = map(int, input().split())

def format_record(record):
    return " ".join((record[0], record[3], record[4], record[2], record[1]))

for i in q:
    print(format_record(records[i]) if 0 <= i < N else "Not Found")

7-35

n = int(input())
nums = [list(map(int, i.split("/"))) for i in input().split()]

def simpify(a, b):
    num, deno = a, b
    while num: num, deno = deno % num, num
    return a // deno, b // deno

def add(a1, b1, a2, b2):
    a, b = a1 * b2 + b1 * a2, b1 * b2
    return simpify(a, b)

for i in range(1, n):
    nums[i] = add(*nums[i - 1], *nums[i])

a, b = simpify(nums[n - 1][0], nums[n - 1][1] * n)
print(*[a, b] if b > 1 else [a], sep="/")

7-36 ~ 7-38

7-36

c = [i for i in map(float, input().split())]
z1, z2 = complex(c[0], c[1]), complex(c[2], c[3])

def to_string1(z):
    if z.imag >= 0:
        return f"({z.real:.1f}+{z.imag:.1f}i)"
    return f"({z.real:.1f}{z.imag:.1f}i)"

def to_string2(z):
    if -0.05 < z.imag < 0.05:
        return "%.1f" % z.real
    if -0.05 < z.real < 0.05:
        return "%.1fi" % z.imag
    if z.imag >= 0:
        return f"{z.real:.1f}+{z.imag:.1f}i"
    return f"{z.real:.1f}{z.imag:.1f}i"

print(to_string1(z1) + " + " + to_string1(z2) + " = " + to_string2(z1 + z2))
print(to_string1(z1) + " - " + to_string1(z2) + " = " + to_string2(z1 - z2))
print(to_string1(z1) + " * " + to_string1(z2) + " = " + to_string2(z1 * z2))
print(to_string1(z1) + " / " + to_string1(z2) + " = " + to_string2(z1 / z2))

7-37

from copy import copy

add = []
def dfs(pre, acc, res):
    for i in range(1, pre + 1):
        res.append(i)
        if acc + i == n:
            add.append(copy(res))
            res.pop()
            return
        else: dfs(i, acc + i, res)
        res.pop()

dfs(n := int(input()), 0, [])
ans = sorted([i[::-1] for i in add])

for i in range(0, len(ans), 4):
    print(";".join(f"{n}=" + "+".join(str(k) for k in j) for j in ans[i:i + 4]))

7-38

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, n;
    cin >> a >> n;
    vector<int> res(n << 1);
    for (int i = 0; i < n; i++)
        res[i] = (n - i) * a;
    for (int i = 0; i < n; i++) {
        res[i + 1] += res[i] / 10;
        res[i] %= 10;
    }
    while (res.size() && res.back() == 0) res.pop_back();
    if (res.empty()) {
        putchar('0');
        return 0;
    }
    reverse(res.begin(), res.end());
    for (int i : res)
        putchar(i + '0');
    return 0;
}
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南宫谨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值