招商银行信用卡中心笔测
一.板凳题
思路:先对数组进行排序,最大的k就是最大值加上m值,而最小的k,就是把其他的先填补到里面最大值,然后再用m/n进行求余和整除来获得我们可以得到几次填补的1,再加1即可:
python版
#n板凳m个人,本身有a[i]个人
import sys
if __name__ == "__main__":
n=sys.stdin.readline().strip()
m=sys.stdin.readline().strip()
arr=[]
for i in range(int(n)):
b=int(sys.stdin.readline().strip())
arr.append(int(b))
arr.sort()
maxm=arr[-1]+int(m)
#最小化,就是不断的把最小的人数加一,直到加的人数等于m,则再求k
for i in range(int(m)):
arr[0]+=1
arr.sort()#这里的时间复杂度有点呈现线性最好不要这样
maxn=arr[-1]
#print(arr)
print(maxn,maxm)
C++版:
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<char,ll> M;
int n,m;
int a[101010]={0};
bool cmp(int p,int q)
{
return p>q;
}
int main()
{
int i,p,c,maxx=0,ans2=0,ans1=0;
cin>>n>>m;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,cmp);
maxx=a[0];
ans2=a[0]+m;
for(i=1;i<n;i++)
{
while(a[i]<a[0])
{
a[i]++;
m--;
}
}
if(m>0)
{
if(m%n==0)
ans1=m/n+a[0];
else
ans1=m/n+a[0]+1;
}
else
ans1=a[0];
cout<<ans1<<" "<<ans2;
return 0;
}
二.巧克力题
思路:其实就是求解2的n-6次方,但是要对666666666进行取模:
#n是人数
import sys
def chocolate(n,mod_num=666666666):
if n<6:
return 0
new_n=n-6
res_mode=exp_mode(2,new_n,mod_num)
return res_mode
def exp_mode(a,n,b):
if n==0:
return 1%b
if n==1:
return a%b
t=exp_mode(a,n>>1,b)
t=(t*t)%b
if(n&0x01==1):#如果为奇数
t=(t*a)%b
return t
#其实就是排列组合的问题比如说
if __name__ == "__main__":
n=sys.stdin.readline().strip()
print(chocolate(int(n)))
三.试剂题
思想:关于这道题,基本上就是统计的思想,这道题需要两个数组,一个数num可以通过乘以2或者除以2来变换
一个数组统计将可以变换到每一个数b的步数记录下来
再用一个数组记录可以变成这个数b的个数只有所有数能到这个数才是正确的可取的
#n个数,m是数据
import sys
MAX=100000
cnt=[0]*MAX#到这个数的步数
vis=[0]*MAX#到这个数查看个数
#在这里其实在穷举
def handle(t):#处理一个数进行统计
stp=0
tmp=t
while tmp<=MAX:
vis[tmp]+=1
cnt[tmp]+=stp
stp+=1#用来统计次数,这里的先后顺序还是有关的
tmp<<=1#tmp乘2
stp=0
while t:#这里针对除以2的部分
if t&0x01 and t!=1:#如果是奇数,则还可以进行反乘回去,这是我们之前无法得到的
t>>=1
stp+=1
cnt[t]+=stp
vis[t]+=1
tt=t
tmstp=stp
while tt<=MAX:
tt<<=1
tmstp=stp
vis[tt]+=1
cnt[tt]+=tmstp
else:
t>>=1
stp+=1
cnt[t]+=stp
vis[t]+=1
if __name__ == "__main__":
n=sys.stdin.readline().strip()
m=sys.stdin.readline().strip()
values=map(int,m.split())#输入多行的数据
num1=list(values)#获取到数组
print(num1)
n=int(n)
for i in range(n):
handle(num1[i])
ans=100000
for i in range(MAX):
if vis[i]==n:
if ans>cnt[i]:
ans=cnt[i]
else:
ans=ans
print(ans)
以上程序都是经过自我的调试的,均没问题。