百度2017春招笔试真题编程题集合
2021-04-29 15:44完成
试题地址
https://www.nowcoder.com/test/4998655/summary
1
度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少?
如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1
set去重后排序即可
n=int(input())
a=list(map(int,input().split()))
a=list(set(a))
a.sort()
n=len(a)
if n>=3:
print(a[2])
else:
print("-1")
//scala ACM模式读入读出
import scala.io.StdIn
import scala.math.{min,max,abs}
object Main extends App{
var n=StdIn.readInt;
var b=StdIn.readLine().split(" ").map(_.toInt).toSet.toList.sorted;
if (b.length>=3) println(b(2));
else println(-1);
}
//C# ACM模式读入读出
using System;
using System.Collections.Generic;
namespace baidu_code {
class Program {
static void Main() {
int n = int.Parse(Console.ReadLine());
List < string > a = new List < string > (Console.ReadLine().Split(' '));
List < int > b = new List < int > ();
foreach(string ele in a) {
int ele2int;
int.TryParse(ele, out ele2int);
b.Add(ele2int);
}
HashSet < int > hs = new HashSet < int > (b);
b = new List < int > (hs);
b.Sort(); //这一句不能丢,我吐了
//foreach(var ele in b) Console.WriteLine(ele);
if (b.Count >= 3) {
Console.WriteLine(b[2]);
} else {
Console.WriteLine( - 1);
}
}
}
}
2
一个数轴上共有N个点,第一个点的坐标是度度熊现在位置,第N-1个点是度度熊的家。现在他需要依次的从0号坐标走到N-1号坐标。
但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个点,并直接将这个点忽略掉,问度度熊回家至少走多少距离?
把一个点i去掉可以节省\(abs(a[i]-a[i-1])+abs(a[i]-a[i+1])-abs(a[i-1]-a[i+1])\)
找到可以节省的最大值即可
n=int(input())
a=list(map(int,input().split()))
n=len(a)
maxValue=-100000
Sum=0
for i in range(1,n-1):
maxValue=max(maxValue,abs(a[i]-a[i-1])+abs(a[i]-a[i+1])-abs(a[i-1]-a[i+1]))
Sum=Sum+abs(a[i]-a[i-1])
Sum=Sum+abs(a[n-1]-a[n-2])
print(Sum-maxValue)
//scala ACM模式读入读出
import scala.io.StdIn
import scala.math.{min,max,abs}
object Main extends App{
var n=StdIn.readInt;
var a=StdIn.readLine().split(" ").map(_.toInt);
var maxValue=(-100000);
var Sum=0;
for(i<-Range(1,n-1,1)){
maxValue=max(maxValue,abs(a(i)-a(i-1))+
abs(a(i)-a(i+1))
-abs(a(i-1)-a(i+1))
);
Sum=Sum+abs(a(i)-a(i-1));
}
Sum=Sum+abs(a(n-1)-a(n-2));
println(Sum-maxValue);
}
//C# ACM模式读入读出
using System;
using System.Collections.Generic;
using static System.Math;
namespace baidu_code
{
class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
List < string > a = new List < string > (Console.ReadLine().Split(' '));
List < int > b = new List < int > ();
foreach(string ele in a)
{
int ele2int;
int.TryParse(ele, out ele2int);
b.Add(ele2int);
}
int maxValue=-100000;
int Sum=0;
for (int i=1;i<n-1;i++)
{
maxValue=Max(maxValue,Abs(b[i]-b[i-1])
+Abs(b[i]-b[i+1])
-Abs(b[i-1]-b[i+1]));
Sum=Sum+Abs(b[i]-b[i-1]);
}
Sum=Sum+Abs(b[n-1]-b[n-2]);
Console.WriteLine(Sum-maxValue);
}
}
}
3
三维空间中有N个点,每个点可能是三种颜色的其中之一,三种颜色分别是红绿蓝,分别用'R', 'G', 'B'表示。
现在要找出三个点,并组成一个三角形,使得这个三角形的面积最大。
但是三角形必须满足:三个点的颜色要么全部相同,要么全部不同。
(N <= 50) 友好的范围
考虑到这题友好的变量范围,实际这题是考察面积公式
当练习编码规范了
笔试时可不能写这么长
#include<bits/stdc++.h>
#define rep(i,a,x) for(int i=a;i<=x;i++)
using namespace std;
typedef long long ll;
struct point{
char color;
double x;
double y;
double z;
}p[100];
class Vector{
public:
double x;
double y;
double z;
Vector(double xx,double yy,double zz){
this->x=xx;
this->y=yy;
this->z=zz;
}
};
double Norm2(Vector v){
return sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
}
Vector CrossProduct(Vector v1,Vector v2){
Vector v=Vector(0.0,0.0,0.0);
v.x=v1.y*v2.z-v1.z*v2.y;
v.y=v1.z*v2.x-v1.x*v2.z;
v.z=v1.x*v2.y-v1.y*v2.x;
return v;
}
double TriangleArea(point a,point b,point c){
Vector v1=Vector(b.x-a.x,b.y-a.y,b.z-a.z);
Vector v2=Vector(c.x-a.x,c.y-a.y,c.z-a.z);
return 0.5*abs(Norm2(CrossProduct(v1,v2)));
}
int n;
int main(){
cin>>n;
double maxArea=-1.0;
for(int i=0;i<=n-1;i++){
cin>>p[i].color>>p[i].x>>p[i].y>>p[i].z;
}
rep(i,0,n-1){
rep(j,i+1,n-1){
rep(k,j+1,n-1){
if(p[i].color==p[j].color&&p[j].color==p[k].color){
maxArea=max(maxArea,TriangleArea(p[i],p[j],p[k]));
}
if(p[i].color!=p[j].color&&p[j].color!=p[k].color&&p[k].color!=p[i].color){
maxArea=max(maxArea,TriangleArea(p[i],p[j],p[k]));
}
}
}
}
printf("%.5lf\n",maxArea);
return 0;
}
4
度度熊有一个N个数的数组,他想将数组从小到大 排好序,但是萌萌的度度熊只会下面这个操作:
任取数组中的一个数然后将它放置在数组的最后一个位置。
问最少操作多少次可以使得数组从小到大有序?
(N <= 50, 每个数的绝对值小于等于1000) 友好的范围
输入例子1:
4
19 7 8 25
输出例子1:
2
直接暴力,
枚举ans从0到n-1。对于每一个枚举的ans值,把前ans大的数移到最后去
判断是否可行。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct element{
int num;
int idx;
}a[100],b[100];
bool cmp(element a,element b){
if(a.num<=b.num) return 1;
else return 0;
}
int n;
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i].num;
a[i].idx=i;
b[i].idx=i;
b[i].num=a[i].num;
}
sort(a,a+n,cmp);
int ans=0;
while(ans<=n-1){
int pivot=10000;
if(ans>0) pivot=a[n-1-ans].num;
bool flag=false;
int last=0;
for(int i=0;i<=n-1;i++){
if(b[i].num<=pivot){
if(!flag) {
flag=true;
last=b[i].num;
}
else{
if(b[i].num>=last) {
last=b[i].num;
continue;
}
else {
flag=false;
break;
}
}
}
}
if(flag) break;
else ans++;
}
cout<<ans<<endl;
return 0;
}
5
问你降位数为k的n-排列个数。结果对2017取模。
欧拉数板子题。
状态转移方程是说,考虑n-1排列到【m个降位的n-排列】的转移。
如果n插在【本来的m个降位的2个数中间】或者【本来的n-1排列的最后一位】,降位数保持不变,有m+1个插法。这解释了右边的第二项。
如果n插在其他位置,有\((n+1)-(m+1)=n-m\)种插法,降位数会加一。这解释了右边的第一项。
//先写个cpp打印欧拉数
//dp[n][j]是欧拉数,表示降位数为j的n-排列的方案数目
//有的时候把j+1或者j-1对应的情况叫做欧拉数,囧
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 2017
#define rep(i,a,x) for(int i=a;i<=x;i++)
ll number,cnt;
ll dp[1005][1005];
int main(){
cin>>number>>cnt;
memset(dp,0,sizeof(dp));
for(int n=0;n<=number;n++){
for(int m=0;m<=number;m++){
if(m>=n||n==0) dp[n][m]=0;
else if(m==0) dp[n][m]=1;
else dp[n][m]=((n-m)*(dp[n-1][m-1])%mod+(m+1)*dp[n-1][m]%mod)%mod;
}
}
// rep(i,0,10){
// rep(j,0,10){
// cout<<dp[i][j]<<" ";
// }
// puts("");
// }
cout<<(dp[number][cnt]+mod)%mod<<endl;
return 0;
}
/*
0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0
1 4 1 0 0 0 0 0 0 0 0
1 11 11 1 0 0 0 0 0 0 0
1 26 66 26 1 0 0 0 0 0 0
1 57 302 302 57 1 0 0 0 0 0
1 120 1191 399 1191 120 1 0 0 0 0
1 247 259 1500 1500 259 247 1 0 0 0
1 502 489 1503 881 1503 489 502 1 0 0
1 1013 1449 1367 1321 1321 1367 1449 1013 1 0
*/
dp=[[0 for i in range(1005)] for j in range(1005)]
mod=2017
number,cnt=map(int,input().split())
for n in range(0,number+1,1):
for m in range(0,number+1,1):
if(m>=n or n==0):
dp[n][m]=0
elif m==0:
dp[n][m]=1
else:
dp[n][m]=((n-m)*(dp[n-1][m-1])%mod+(m+1)*dp[n-1][m]%mod)%mod
print((dp[number][cnt]+mod)%mod)