暴风影音2014校园招聘笔试题目-技术D卷
/*暴风影音2014校园招聘笔试题目-技术D卷.6. m*n的网格从左上角A点走到右下角B点,每次可走一格,只能往右或下走。输出有多少种走法和所有路线数。
*/
#include <cstring>
#include <cstdio>
#include <algorithm>using namespace std;int m = 2, n = 3;
int* path;void printPath()
{int i;for( i=0; i<m+n-1; i++) {printf("%s->", path[i] ? "right" : "down"); }printf("%s\n", path[m+n-1] ? "right" : "down");
}// 方法一
// 数学排列
int A(int a, int b)
{int sum = 1;while(b--) {sum *= a--;}return sum;
}void solve1()
{printf("%d*%d的网格A到B的路径数:%d\n", m, n, A(m+n, n) / A(n, n));int i;for( i=0; i<m; i++) {path[i] = 0; //0表示down}for( i=m; i<m+n; i++) {path[i] = 1; //1表示right}do {printPath();} while(next_permutation(path, path+m+n)); //生成全排列
}// 方法二
int dfs(int cur, int x, int y)
{if(cur == m+n) {printPath();return 1;}int cnt = 0;if(x < m) { //可以向下走path[cur] = 0;cnt += dfs(cur+1, x+1, y);}if(y < n) {path[cur] = 1;cnt += dfs(cur+1, x, y+1);}return cnt;
}void solve2()
{//0表示down, 1表示rightint cnt = dfs(0, 0, 0);printf("%d\n", cnt);}// 方法三: 动态规划来计算路径个数
void dp()
{const int MAXN = 100, MAXM = 100; // 假设 m,n<100 int d[MAXM][MAXN];memset(d, 0, sizeof(d));d[0][0] = 1;for(int i=0; i<m+1; i++) {for(int j=0; j<n+1; j++) {if(i!=0) d[i][j] += d[i-1][j];if(j!=0) d[i][j] += d[i][j-1];}}printf("A-B的路径数:%d \n", d[m][n]);
}int main()
{path = new int[m+n];/*数学方法:A到B需要m+n步,其中m步向下,n步向右。所有路径数为m个down和n个right的排列个数。所以 A(m+n, m+n) / ( A(m, m) * A(n, n) ) = A(m+n, n) / A(n, n);*///solve1(); /*由于需要打印路径,可以考虑使用回溯法*///solve2();/*动态规划*/dp();return 0;
}/*暴风第5题, 从一个表达式字符串中找到最深层圆括号内的表达式.如从x+(y*z)-(m-(3+4)) 中找到3+4*/#include <cstring>
#include <cstdio>
#include <algorithm>using namespace std;int main()
{char str[] = "x+(y*z)+(m-(3-4)+(1+(2-0))+(m-(3+4))";char *pa =0, *pe = 0;int m = 0, lvl = 0;char *p = str;while(*p) {if(*p == '(') {lvl++;if(lvl > m) { pa = p+1;m = lvl;pe = 0;}} else if(*p == ')') {if(pe == 0) pe = p;lvl--;}p++;}while(pa < pe) {putchar(*pa++);}return 0;
}/*暴风第4题.二叉树节点定义:struct TBinaryTree {TBinaryTree *m_pLeft;TBinaryTree *m_pRight;char m_chElement;};将两个二叉树合并:a) 不能改变两个二叉树原有的内部结构b) 只可以将其中一个二叉树的根节点,挂到另一个二叉树的非满节点下c) 使得合并后树高度最小d) 最优合并方式有很多种,给出一种就可以了*/
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
const int MAXN = 20;using namespace std;struct TBinaryTree {TBinaryTree *m_pLeft;TBinaryTree *m_pRight;char m_chElement;
};TBinaryTree tbt1[MAXN];
TBinaryTree tbt2[MAXN];TBinaryTree * init_tree(TBinaryTree *tbt1) {int n, i;scanf("%d", &n);int idx, left, right;char key;for(i=1; i<=n; i++) {scanf("%d %c%d%d", &idx, &key, &left, &right);//printf("%d%c%d%d\n", idx, key, left, right);tbt1[i].m_chElement = key;if(left) tbt1[i].m_pLeft = &tbt1[left];else tbt1[i].m_pLeft = 0;if(right) tbt1[i].m_pRight = &tbt1[right];else tbt2[i].m_pRight = 0;}return &tbt1[1];
}//获得树的深度
int getHight(TBinaryTree *root)
{if(root == NULL) return 0;return MAX(getHight(root->m_pLeft), getHight(root->m_pRight)) + 1;
}//找到深度最小、非度为2的节点
//采用广度遍历树,(深度遍历也可以,但是效率差)
void getRightNode(int &l, TBinaryTree *&pNode, TBinaryTree *root)
{queue<TBinaryTree *> q;q.push(root);l = 1; //根深度为1int cnt = 1; //当深度节点数while(!q.empty()) {int temp = 0; // temp记录下个深度中的节点数while(cnt--) {root = q.front(); q.pop();if(root->m_pLeft==0 || root->m_pRight==0) {pNode = root;return;}q.push(root->m_pLeft);q.push(root->m_pRight);temp += 2;}cnt = temp;l++;}
}// 层序遍历树
void print(TBinaryTree *root)
{printf("\n");queue<TBinaryTree *> q;q.push(root);int cnt = 1; //当深度节点数while(!q.empty()) {int temp = 0; // temp记录下个深度中的节点数while(cnt--) {root = q.front(); q.pop();printf("%c ", root->m_chElement);if(root->m_pLeft) {q.push(root->m_pLeft);++temp;}if(root->m_pRight) {q.push(root->m_pRight);++temp;}}cnt = temp;printf("\n");}
}/*
输入
4
1 A 2 3
2 B 0 4
3 C 0 0
4 D 0 0
6
1 a 2 3
2 b 4 5
3 c 6 0
4 d 0 0
5 e 0 0
6 f 0 0
*/
int main()
{//freopen("in.txt", "r", stdin);TBinaryTree *pTree1, *pTree2;pTree1 = init_tree(tbt1);pTree2 = init_tree(tbt2);print(pTree1);print(pTree2);int h1, h2;h1 = getHight(pTree1);h2 = getHight(pTree2);printf("tree1 height:%d, tree2 height: %d\n", h1, h2);TBinaryTree *pNode1, *pNode2;int l1, l2;getRightNode(l1, pNode1, pTree1);getRightNode(l2, pNode2, pTree2);printf("%d %c\n", l1, pNode1->m_chElement);printf("%d %c\n", l2, pNode2->m_chElement);int L1 = MAX(h1, l1+h2);int L2 = MAX(h2, l2+h1);if(L2 < L1) {swap(pNode1, pNode2);swap(pTree1, pTree2);}if(pNode1->m_pLeft == NULL) {pNode1->m_pLeft = pTree2;} else {pNode1->m_pRight = pTree2;}print(pTree1);return 0;
}
暴风影音2014校园招聘笔试题目-技术D卷相关推荐
- 百度2014校园招聘笔试面试汇总
目 录 1. 百度笔试 2 1.1百度2014校园招聘笔试题(成都站,软件研发岗) 2 1.2 2013百度校园招聘-机器学习和数据挖掘工程师-笔试题 7 1.3 百度2014校园招聘 技术研发题 ...
- 深圳深信服科技07年校园招聘笔试题目
发信人: happyhippy (CS03.Silent Void), 信区: jobservice 标 题: 深圳深信服科技07年校园招聘笔试题目 发信站: 郁金香BBS站 (2006年11月 ...
- 九度OJ 1525 子串逆序打印 -- 2012年Google校园招聘笔试题目
题目地址:http://ac.jobdu.com/problem.php?pid=1525 题目描述: 小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在印刷的过程中将字符串 ...
- 阿里巴巴 c语言 笔试题,阿里巴巴校园招聘笔试题目
笔试时间为2014年8月29日,均为网上答题.第一部分为单选题,共20题,要在40分钟内完成.每个人的选择题都不一样,应该是后台有题库,每个人的试卷都是随机生成的.第二部分为附加题,一般为1道问答题, ...
- 微软2014校园招聘笔试试题
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/12190807 1.Which statement(s) is(are) ...
- 2015_12_27微软校园招聘笔试题目
首先发三个题目,其链接如下: http://hihocoder.com/contest/mstest2015dec3/problems 解析: 第一个题目是暴力,简单的枚举就可以解决,代码如下所示: ...
- 2011年恒生电子校园招聘笔试题目
倒数第二道题: C语言字符串拷贝函数的实现 char *strcp(char *dest, const char *source) char *strcp(char *dest, ...
- 网易校园招聘笔试题目
1 两个数组x[].y[],长度为n,而且都经过排序从小到大排列,请编写C/C++程序求出两个数组合并后(仍然从小到大排列)的中位数,要求比较次数少于n. 分析:要求出中间的两个数,只要用一个数组来存 ...
- 网新恒天2014校园招聘笔试编程题
已知memcpy的函数为: void* memcpy(void *dest , const void* src , size_t count)其中dest是目的指针,src是源指针.不调用c++/c的 ...
最新文章
- 2022-2028年中国LCD光刻胶行业市场深度分析及发展规模预测报告
- MapReduce_wordcount
- BAT数据披露:缺人!110万AI人才缺口,两者矛盾,凉凉了!
- sql server 数据库模型 备份 恢复 总结 备份脚本
- 2021年人工智能数据采集标注行业四大趋势预测
- (Java集合框架)Map集合
- boost::process::on_exit相关的测试程序
- FlyCms 是一个类似知乎以问答为基础的完全开源的JAVA语言开发的社交网络建站程序
- 【一起学爬虫】PyQuery详解
- PAM for Kmedoids algorithm, PAM算法的实现, kmeans 算法实现. 利用scikit-learn toolbox.
- 【前端安全】JavaScript防http劫持与XSS (转)
- 【3dmax千千问】初学3dmax插件神器第20课:3dmax渲染教程|效果图大师和疯狂模渲大师怎么使用3dmax软件自带的渲染器去设计并渲染三维效果图场景的3dmax模型?
- linux系统Nginx下limit_req模块burst参数超详细解析
- centos oracle libaio哪下载,在CentOS4.5上安装Oracle10g出现的libaio-0.3.96; found Not found问题求救...
- 历经一个月研究,发布两款机器人,小白就会python自己制作机器人了
- 聚类kmeans和DBSCAN算法的简单实现
- suse linux乱码,轻松解决Suse中文乱码问题
- IK分词器-自定义分词
- Windows 下搭建Scratch环境
- Java之JDK环境的安装与配置