CAU12.
Kiểm tra CSC
with open("CAU12.inp","r") as fin:
data=fin.readlines()
l=int(data[0])
s=list(map(int,data[1].split()))
d=s[2]-s[1]
for i in range(1,l-1):
if s[i+1]-s[i]!=d:
result="FALSE"
else:
result="TRUE"
with open("CAU12.out","w") as fout:
fout.writelines(result)
CAU2. Tìm max, chỉ số phần tử đầu tiên max
with open("CAU2.INP", "r") as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().strip().split()))
max_value = max(a)
first_max_index = a.index(max_value)
with open("CAU2.OUT", "w") as f:
f.write(f"{max_value}\n")
f.write(f"{first_max_index + 1}\n")
CAU3. Tìm max, chỉ số tất cả phần tử max
with open("CAU3.INP", "r") as f:
data = f.readlines()
klinh = list(map(int, data[0].split()))
gtln = max(klinh)
chiso = [hello + 1 for hello, num in enumerate(klinh) if num == gtln]
with open("CAU3.OUT", "w") as f:
f.write(str(gtln) + "\n")
f.write(" ".join(map(str, chiso)))
CAU15. Số phần tử vị trí lẻ, giá trị lẻ
with open ("CAU15.INP","r") as f:
data=f.readlines()
N=list(map(int,data[1].split()))
dem=0
for i in range(len(N)):
if i%2==0 and N[i]%2==1:
dem+=1
with open("CAU15.OUT", "w") as f:
f.writelines(str(dem))
CAU17. Sắp xếp 2 dãy tăng dần
with open("CAU17.INP", "r") as f:
data=f.readlines()
a=list(map(int,data[0].split()))
b=list(map(int,data[1].split()))
c=a+b
a1=list(sorted(a))
a2=' '.join(str(x) for x in a1)
b1=list(sorted(b))
b2=' '.join(str(y) for y in b1)
c1=list(sorted(c))
c2=' '.join(str(z) for z in c1)
S=a2+"\n"+b2+"\n"+c2
with open("CAU17.OUT", "w") as f:
f.writelines(str(S))
024HH. Số bằng tổng ước không kể nó
with open("024HH.inp","r")as fin:
data=fin.readlines()
N=int(data[0])
a=0
for i in range(2,N+1):
u=[b for b in range(1,i)if i%b==0]
hh=sum(u)==i
if hh:
a+=1
with open("024HH.out","w")as fout:
fout.writelines(str(a))
015TD. Tổng và TB phần tử
with open("015TD.INP", "r") as f:
data=f.readlines()
n=list(map(int,data[0].split()))
s=0
for x in range(len(n)):
N=int(n[x])
s=s+N
tb=s/len(n)
with open("015TD.OUT", "w") as f:
f.writelines(str(s)+" "+format(tb,".3f"))
015TD02. Số phần tử lớn hơn TB các phần tử chẵn
with open("015TD02.INP", "r") as f:
data=f.readlines()
N=list(map(int,data[0].split()))
ssh=0
tong=0
for i in N:
if i%2==0:
ssh+=1
tong+=i
soluong=0
for i in range (0,len(N)):
if i>(tong/ssh):
soluong+=1
with open("015TD02.OUT", "w") as f:
f.writelines(str(soluong)+" "+str("{:.3f}".format(tong))
015TD05. Phần tử bằng 2k. Có: vị trí max. Không: -1 số phần tử >=k
with open("015TD05.INP", "r") as f:
data = f.readlines()
arr = list(map(int, data[0].split()))
k = int(data[1].strip())
position = -1
count_gte_k = 0
for idx, value in enumerate(arr):
if value >= k:
count_gte_k += 1
if value == 2 * k:
position = idx + 1
with open("015TD05.OUT", "w") as f:
f.write(f"{position} {count_gte_k}\n")