Skip to content

Commit

Permalink
add basic 1121-1125
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny656 committed Dec 21, 2023
1 parent 70586a1 commit 490fabf
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions PAT (Basic Level) Practice (中文)/1121_祖传好运 (15).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def is_lucky(num_str):
result, val = True, 0
for i in range(0, len(num_str)):
val = val * 10 + int(num_str[i])
result &= val % (i + 1) == 0
return result


input()
num_str_arr = input().split()
for num_str in num_str_arr:
print("Yes" if is_lucky(num_str) else "No")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
input()
arr = map(int, input().split())
# filter out even numbers
arr = list(filter(lambda v: v % 2 == 1, arr))
result = arr[0]
# xor remove odd number pair
for i in range(1, len(arr)):
result ^= arr[i]
print(result)
26 changes: 26 additions & 0 deletions PAT (Basic Level) Practice (中文)/1123_舍入 (20).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from decimal import *


def round_number(method, number, precision):
context = getcontext()
context.prec = 300 # 精度位数一定要大
if method == "1":
context.rounding = ROUND_HALF_UP
elif method == "2":
context.rounding = ROUND_DOWN
elif method == "3":
context.rounding = ROUND_HALF_EVEN

d = Decimal(number).quantize(Decimal("0." + "0" * precision))
if d == 0:
d = abs(d)
ans = str(d)
if ans[0] == "+":
ans = ans[1:]
return ans


n, d = map(int, input().split())
for i in range(n):
op, num = input().split()
print(round_number(op, num, d))
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fib = [0, 1]

count = 0
while fib[-1] <= 10**8:
new = fib[-1] + fib[-2]
fib.append(new)

v = int(input())
ans, diff = -1, 10**8
for i in range(len(fib)):
if fib[i] >= v:
if abs(fib[i] - v) < diff:
ans = fib[i]
break
else:
ans = fib[i]
diff = abs(fib[i] - v)
print(ans)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
using namespace std;

int main() {
ios::sync_with_stdio(false);
string s, p;
cin >> s >> p;

pair<int, string> ans{10e5+10, ""};
// brute force match p in s and find the shortest substring
for (int i = 0; i < s.size(); i++) {
if (s[i] != p[0]) continue;
int l = i, r = l+1;
int k = 1;
for (; r < s.size() && k != p.size(); r++) {
if (s[r] == p[k]) k++;
if (k == p.size()) break;
}

int len = r-l+1;
if (k == p.size() && len < ans.first) {
ans.first = len;
ans.second = s.substr(l, len);
}
}

cout << ans.second << endl;
return 0;
}

0 comments on commit 490fabf

Please sign in to comment.