Ping An Technology
1. Find the square root
dichotomy:
a,b= input().strip().split()
a=float(a)
b=int(b)
low=0
high=a
ans=(low+high)/2
for i in range(1000):
if pow(ans,b)==a:
break
if pow(ans,b)>a:
high=ans
else:
low=ans
ans=(low+high)/2
ans=("%.6f"%ans)
print(ans)
3. Compare version numbers
Note that each part of the version number may have a leading 0, which should be ignored and the comparison result is returned
v1,v2=map(str, input().strip().split())
def compare_version(version1,version2):
print(version1,version2)
if version1 == version2:
return 0
try:
current_section_version1 = version1[:version1.index('.')]
except:
current_section_version1 = version1
try:
current_section_version2 = version2[:version2.index('.')]
except:
current_section_version2 = version2
if int(current_section_version1) > int(current_section_version2):
return 1
elif int(current_section_version1) < int(current_section_version2):
return -1
try:
other_section_version1 = version1[version1.index('.')+1:]
except:
other_section_version1 = "0"
try:
other_section_version2 = version2[version2.index('.')+1:]
except:
other_section_version2 = "0"
return compare_version(other_section_version1,other_section_version2)
print(compare_version(v1,v2))
Meituan
1.
According to the division method, all strings are sorted and divided according to the first letter. After division, each part continues to be sorted to the next
arr = list(map(str, input().strip().split(',')))
""" arr.sort(reverse=True)
print(','.join(arr)) """
#waimai,dache,lvyou,liren,meishi,jiehun,lvyoujingdian,jiaopei,menpiao,jiudian
#waimai,menpiao,meishi,lvyou,lvyoujingdian,liren,jiudian,jiehun,jiaopei,dache
def charat(s,i):
n=len(s)
if i<n:
return ord('z')-ord(s[i])
else:
return -1
def sortnew(arr):
n=len(arr)
aux=['' for i in range(n)]
sortit(arr,aux,0,n-1,0)
def sortit(arr,aux,lo,hi,d):
if lo>=hi:
return
R=26
count=[0 for i in range(R+2)]
for i in range(lo,hi+1):
count[charat(arr[i],d)+2]+=1
for i in range(0,R+1):
count[i+1]+=count[i]
#print(len(count))
#print(count)
for i in range(lo,hi+1):
x=charat(arr[i],d)+1
#print(count)
aux[count[x]]=arr[i]
count[x]+=1
for i in range(lo,hi+1):
arr[i]=aux[i-lo]
for i in range(0,R):
sortit(arr,aux,lo+count[i],lo+count[i+1]-1,d+1)
sortnew(arr)
print(','.join(arr))
2.
Without full A, the algorithm needs to be improved. . .
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
#include<cmath>
#include<climits>
#include<queue>
#include<cstring>
#include<set>
#include <string>
using namespace std;
typedef struct box {
int cur;
int limit;
}box;
bool cmp(const box& a,const box& c){
if(a.limit==c.limit)
return a.cur>c.cur;
return a.limit > c.limit;
}
int main(){
int n;
cin >> n;
vector<box> b(n);
for (int i = 0; i < n; ++i)
cin >> b[i].cur;
for (int i = 0; i < n; ++i)
cin >> b[i].limit;
sort(b.begin(), b.end(), cmp);
int i, res = 0, last = n - 1;
for (i = 0; i < last; ++i) {
int le = b[i].limit - b[i].cur;
if (le < b[last].cur) {
b[last].cur -= le;
b[i].cur = b[i].limit;
res += le;
} else {
b[i].cur += b[last].cur;
res += b[last].cur;
b[last--].cur = 0;
}
if (b[i].cur < b[i].limit)
--i;
}
cout <<i<<' '<<res<<endl;
system("PAUSE");
return 0;
}
/*4
3 3 4 3
4 7 6 5
2 6*/