校招全国统一模拟笔试(三月场)编程题参考代码

开锁

思路分析

对每个数字,比较是加过去步数少,还是减过去步数少即可。

参考代码

#include <bits/stdc++.h>
using namespace std;

int n, s, i;
string a, b;
int main() {
	cin >> n >> a >> b;
	for (int i = 0; i < n; i++) s += min((a[i] - b[i] + 10) % 10, (b[i] - a[i] + 10) % 10);
	cout << s << endl;
}

加减二叉树

思路分析

所有n都不大于2^k。考虑到n等于2^k时,一直往左走最后一步往右走。n等于2^k-1时一直往左走。n等于2^k-d时,分d是奇数偶数讨论最后一步的走法(奇左偶右),前面k-1步一直往左走,令减去的值刚好等于d/2向下取整。

参考代码

#include <bits/stdc++.h.h>

using namespace std;

int main() {
	long long n, k, a, d, b, i;
	scanf("%lld%lld", &n, &k);
	a = 1LL<<k;
	d = a - n;
	b = d / 2;
	for(i = 0; i < k; i++) {
		if((1LL<<i) & b)printf("%lld -\n",(1LL<<i));
		else {
			if(i == k - 1) {
				if(d & 1) printf("%lld +\n",(1LL<<i));
				else printf("%lld +\n",(1LL<<i)+1LL);
			}
			else printf("%lld +\n",(1LL<<i));
		}
	}
	return 0;
}

走斜线

思路分析

要到达目的地花费的最小步数是x和y的最大值,如果k小于这个值就一定到不了。如果k溢出了,那么在整个k步里,最多只会走两条直线。分别是如下三种情况(令x<=y):0条直线(y-x)是偶数且(k-x)是偶数;1条直线(y-x)是奇数;2条直线(y-x)是偶数且(k-x)是奇数。

参考代码

#include <bits/stdc++.h>

using namespace std;

int main() {
	int T;
	long long x, y, k, t, ans;
	scanf("%d", &T);
	while(T--) {
		scanf("%lld%lld%lld",&x,&y,&k);
		if(x > y) {
			t = x;
			x = y; 
			y = t;
		}
		if(y > k) {
			puts("-1");
			continue;
		}
		ans = k;
		if((y - x) % 2) ans--;
		else if((k - x) % 2) ans -= 2;
		printf("%lld\n", ans);
	}
	return 0;
}

得分最大

思路分析

双方都要使自己的得分尽可能比对方多,就有两种策略:使自己得分越多越好;使对方得分越少越好。所以贪心比较当前自己盒子里分值最大的彩球和对方盒子里分值最大的彩球,如果自己的分比较多,就从自己盒子里拿(自己得分越多越好),否则从对方盒子里拿(对方得分越少越好)。

参考代码

#include <bits/stdc++.h>
using namespace std;

int a[100015],b[100015];

bool _cmp(int i, int j){return i > j;}

int main() {	
	int n;
	long long ans;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
	sort(a + 1, a + 1 + n, _cmp);
	sort(b + 1, b + 1 + n, _cmp);
	ans = 0;
	int i = 1;
	int j = 1;
	while(i <= n || j <= n) {
		if(a[i] > b[j]) ans += a[i++];
		else j++;
		if(b[j] > a[i]) ans -= b[j++];
		else i++;
	}
	printf("%lld", ans);
	return 0;
}
#校招##笔试题目#
全部评论
只有最后一道有思路,还没完全写出来,前面的二叉树和斜着走真是哭辽。。。
点赞 回复 分享
发布于 2019-03-14 23:38
楼主 最后一题会不会数组越界
点赞 回复 分享
发布于 2019-03-14 23:13
都是考一样的吗?我报的前端也是这些题,用js写简直崩溃了。
点赞 回复 分享
发布于 2019-03-14 21:57
而且我觉得加减二叉树那一题有问题答案。。。。。
点赞 回复 分享
发布于 2019-03-15 14:19
走到第k个节点时所得到的数字刚好等于n。路径长度为k - 1,为什么还要求最短路径???
点赞 回复 分享
发布于 2019-04-03 09:27
有注释最好啦,哈哈
点赞 回复 分享
发布于 2019-03-19 11:06
前面两题没有思路,最后一题还错了。
点赞 回复 分享
发布于 2019-03-18 15:25
加减二叉树用递归写只过了50%,完全不知哪里有问题,直接心态崩了
点赞 回复 分享
发布于 2019-03-16 18:33
#include <iostream> #include <math.h> #include <vector> using namespace std; int main() { int total_line; int vector_x; int vector_y; int step_number; cin >> total_line; vector<int>step; for (int i = 0; i < total_line; ++i) { int temp; cin >> vector_x >> vector_y >> step_number; ///全部丢到第一象限 vector_x = abs(vector_x); vector_y = abs(vector_y); ///横纵坐标一样的时候,步数小于横纵坐标是不能走到的,步数大于等于横纵坐标时候,可以反复走重复路径//还得考虑奇偶数情况 if(vector_x == vector_y) { if(step_number < vector_x) { temp = -1; step.push_back(temp); } if(step_number >= vector_x) { int temp_diffrence; temp_diffrence = step_number - vector_x; if(temp_diffrence % 2 == 0) { step.push_back(step_number); } else { step.push_back(step_number-2); } } } ////////vector_x和vector_y不一样的时候 if(vector_x != vector_y) { int temp_min; int temp_max; int temp_diff; int temp_sum; temp_min = min(vector_x,vector_y); temp_max = max(vector_x,vector_y); temp_diff = temp_max - temp_min; temp_sum = temp_diff + temp_min; //走到一条边之后直接走直线到终点所需要的步数 int temp_steps = temp_min; //走斜线,最小的那个正方形,便可以走到正方形的一条边上 ///到达不了终点的情况 if(step_number < temp_sum) { temp = -1; step.push_back(temp); } ///步数先走最小正方形后,再走直线刚好到终点的情况,讨论直线可以变为斜线的情况 if(step_number == temp_sum) { if(temp_diff % 2 == 0) { step.push_back(step_number); } else { step.push_back(step_number-1); } } ///步数先走完最小正方形后,再走直线到达终点步数小于给出步数的情况,考虑反复走 if(step_number > temp_sum) { if(temp_max % 2 == 0) { if(temp_min % 2 == 0) { if((step_number-temp_max) % 2 == 0) { step.push_back(step_number); } else { step.push_back(step_number-2); } } else { step.push_back(step_number-1); } } else { if(temp_min % 2 == 0) { step.push_back(step_number-1); } else { if((step_number-temp_max) % 2 == 0) { step.push_back(step_number); } else { step.push_back(step_number-2); } } } } } } for (int j = 0; j < step.size(); ++j) { cout << step[j] << endl; } return 0; }
点赞 回复 分享
发布于 2019-03-15 16:53
超时真是硬伤啊,看来是时候掌握一些优化算法了
点赞 回复 分享
发布于 2019-03-15 16:44
点赞 回复 分享
发布于 2019-03-15 14:44
这是算法题?为什么我觉得这是智力题?!!😂😂😂
点赞 回复 分享
发布于 2019-03-15 14:27
#include <cstdio> #include <iostream> #include <algorithm> bool _cmp(int t1,int t2) {     return t1>t2; } using namespace std; int main() {     int n;     cin>>n;     int a[n],b[n];     int temp;     for(int i=0;i<n;i++)     {         cin>>temp;         a[i]=temp;     }     for(int i=0;i<n;i++)     {         cin>>temp;         b[i]=temp;     }     long long ans=0;     int indexa=0;     int indexb=0;     sort(a,a+n,_cmp);     sort(b,b+n,_cmp);     while(indexa<n||indexb<n)     {         if(a[indexa]>b[indexb])   //a先做决定             ans=ans+a[indexa++];         else             indexb++;         //b在做决定         if(b[indexb]>a[indexa])             ans=ans-b[indexb++];         else             indexa++;     }     printf("%lld\n",ans);     return 0; }
点赞 回复 分享
发布于 2019-03-15 14:10
点赞 回复 分享
发布于 2019-03-15 14:09
第二个题目,本地测试输出的都是啥玩意啊,很大的整数或很大的负数。。。。。。。。
点赞 回复 分享
发布于 2019-03-15 14:07
一看就会一交不对。。
点赞 回复 分享
发布于 2019-03-15 13:30
点赞 回复 分享
发布于 2019-03-15 12:13
本地测试两个测试用例都是一次通过,然后提交 0%,两道题都这样,有点小郁闷,思路都差不多的,最气的是,有一道题,根本没有用数组,然后报错数组越界了😂😂😂
点赞 回复 分享
发布于 2019-03-15 11:28
  为什么走斜线用longlong啊?题目不是说不超过一百万吗?
点赞 回复 分享
发布于 2019-03-15 10:59
看头文件就知道,,楼主打acm的吧
点赞 回复 分享
发布于 2019-03-15 09:59

相关推荐

04-14 20:10
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
03-31 18:02
门头沟学院 Java
白日梦想家_等打包版:不要的哦佛给我
点赞 评论 收藏
分享
评论
点赞
138
分享

创作者周榜

更多
牛客网
牛客企业服务