【kickstart 2018 round A】 Even Digits Python
我寫的複雜一點,思路找到一個比它小 中最大的符合條件的數/ 比它大最小的符合條件的數,看那個跟原值接近
# -*- coding:utf-8 -*-
t=input("")
t=int(t)
a=[]
def bignum(num):
length=len(num)
flag=0
for i in range(length):
if(num[i]!=0 and num[i]!=2 and num[i]!=4 and num[i]!=6 and num[i]!=8):
if(flag):
num[i]=8
else:
flag=1
num[i]=num[i]-1
else:
if(flag):
num[i]=8
return num
def smallnum(num):
length=len(num)
flag=0
jw=0
for i in range(length):
if(num[i]!=0 and num[i]!=2 and num[i]!=4 and num[i]!=6 and num[i]!=8):
if(flag):
num[i]=0
else:
flag=1
if(num[i]==9):
num[i]=0
j=i
j=j-1
if(j==-1):
jw=2
else:
f=1
while(f and j>=0):
num[j]=num[j]+1
if(num[j]==9):
f=1
num[j]=0
else:
f=0
num[j]=num[j]+1
j=j-1
if(j<0 and f):
jw=2
else:
num[i]=num[i]+1
else:
if(flag):
num[i]=0
if(jw==2):
num.insert(0,2)
return num
for ca in range(1,t+1):
a=[]
t=t-1;
num=input()
num=int(num)
temp=num
while(num):
a.insert(0,num%10)
num=num/10
t2=a[:]
bn=bignum(t2)
t2=a[:]
sn=smallnum(t2)
m1=0
for i in bn:
m1=m1*10+i
m2=0
for i in sn:
m2=m2*10+i
ans=min(temp-m1,m2-temp)
print("Case #%d: %d"%(ca,ans))
# print("Case #:%d"%bn)
看了別人寫的,比我寫的簡單 https://blog.csdn.net/sinat_32682001/article/details/106305486
思路就是遇到9時,答案只會在比它小的那一側,因為遇到9 比它大的話,會進位,會破壞掉前一個偶數,那麼差值就會變得更大
相關文章
- Google kickstart 2022 Round A題解Go
- 配置yilia/even
- Add Digits 各位相加Git
- [LeetCode] 258. Add DigitsLeetCodeGit
- Codeforces 915C Permute DigitsGit
- [LeetCode] 402. Remove K DigitsLeetCodeREMGit
- Atcoder ARC090F Number of DigitsGit
- ACL Beginner Contest E.Replace DigitsGit
- [ARC146C] Even XOR
- It's not even the whole quest. In this article
- 328. Odd Even Linked List
- Python教程:ceil、floor、round、int取整Python
- 002.Kickstart部署之NFS架構NFS架構
- CF2031F Penchick and Even Medians
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Solution - Codeforces 2031F Penchick and Even Medians
- [ABC126D] Even Relation 題解
- DevOps is Hard、DevSecOps is Even Harder. --- Enterprise Holdidev
- LeetCode之Sum of Even Numbers After Queries(Kotlin)LeetCodeKotlin
- HOME: Count Digits —— 計算字串中數字個數Git字串
- kickstart無人值守方式安裝Linux 7.6Linux
- I do not even think it's a case of people loving PSO
- Therefore, even though this Post Hook strategy may not become a normHookORM
- Keep processes running in the background and even after SSH session disconnectsSession
- Linux中的無人職守安裝指令碼kickstartLinux指令碼
- GJ Round
- PXE高效網路裝機與Kickstart無人值守安裝
- PXE+Kickstart無人值守安裝安裝Centos7.9CentOS
- Codeforces Round 955
- Codeforces Round 977 (Div. 2, based on COMPFEST 16 - Final Round)
- 簡述grub啟動載入程式和製作kickstart檔案
- Codeforces Global Round 27
- Edu Round 170 ReviewView
- CF Pinely Round 4
- Codeforces Global Round 26
- Educational Codeforces Round 163
- Codeforces Global Round 13