一:求GCG(greatest common divisor)
analysis: tow nonnegative integer p and q, if q is 0 then gcd is q, then the gcd is the greatest common divisor of q and p%q. q和p%q的最大公约数,当然也是p和q的最大公约数。
int gcd(int p, int q){
if(q == 0) return q;
return gcd(q, p%q);
}
二:merge two sorted integer array into a third array, and make sure the third array also be sorted.
analysis: image two integer array are a and b, create array c and its length is
a.lenght plus b.length, define two varible indexA, indexB pointing the index of a and b, then loop infinite comparing a[indexA] and b[indexB], if a[indexA] > b[indexB], then assign b[indexB] to c[indexA+indexB] and indexB increase by 1, so vice verse. if indexA exceeds a.length or indexB exceeds b.length, then copy the remaining of a or b to c and terminates the loop.
pseudo code:
int[] a = {};
int[] b = {};
int[] c = new int[a.length+b.length];
int indexA = indexB = 0;
while(true){
while(true){
if(indexA == a.length){// copy remaining b to c
System.arraycopy(b, indexB, c, indexA+indexB, b.length - indexB);
break;
}
if(indexB == b.length){//copy remaining a to c
System.arraycopy(a, indexA, c, indexA+indexB, a.length-indexA);
break;
}
if(a[indexA] > b[indexB]){
c[indexA+indexB] = b[indexB];
indexB++;
}else{
c[indexA+indexB] = a[indexA];
indexA++;
}
}
}
三:reverse an array or testify if a number is a palindrome number
pseudo code:
int[] a = {};
int tmp;
for(int i=0;i<a.length/2;i++){
tmp = a[i];
a[i] = a[a.length-1-i];
a[a.length-1-i] = tmp;
}
四:matrix-matrix multiplicatoin
在计算机中,一个矩阵说穿了就是一个二维数组。一个n行m列的矩阵可以乘以一个m行p列的矩阵,得到的结果是一个n行p列的矩阵,其中的第i行第j列位置上的数等于前一个矩阵第i行上的m个数与后一个矩阵第j列上的m个数对应相乘后所有m个乘积的和
pseudo code:
int[][] a = {{1, 1},{2, 0}};
int[][] b = {{0, 2, 3}, {1, 1, 2}};
if(a[0].length != b.length){
throw new IllegalArgumentException("first matrix's column length must equals with second matrix's row length");
}
int[][] c = new int[a.length][b[0].length];
for(int i=0;i<a.length;i++){//matrix a's row loop
for(int j=0;j<b[0].length;j++){//matrix b's column loop
for(int k=0;k<a[0].length;k++){
c[i][j] += a[i][k] * b[k][j];
}
}
}
五:HanoiTower
思想:首先要把前N-1个盘子转移到辅助柱,然后把第N个盘转到目标柱,最后再把前N-1个盘从辅助柱转移到目标柱,在转移过程中,把第三根柱子视为辅助柱。
pseudo code:
private static void hanoTower(String start, String dest, String auxiliary, int number){
if(number == 0){
return ;
}
hanoTower(start, auxiliary, dest, number-1);
count++;
System.out.println("move " + number + " from "+ start +" to "+ dest);
hanoTower(auxiliary, dest, start, number-1);
}
结论:总的移动次数是2^n -1, 如果N是偶数,则开始把1号盘移到辅助柱,如果N是奇数,把1号盘移到目标柱上。
分享到:
相关推荐
java算法编程题目及答案50道
python算法趣味题目.doc
基础算法题目精简集合 题目相对来说简要了一些,算是有代表性了,各方面都有题目 偶不希望像别的帖子那样像为了凑数般弄够100题,相反这里不过二三十。 前六章均为算法基础入门必会解答的题目,也就是若当中有任何一...
华为2017年算法比赛题目全套,包括中文版和英文版,编译器有gcc和java
从入门到进阶版的算法题目,有多种解法详细说明;
Java程序设计算法竞赛题目Java程序设计算法竞赛题目Java程序设计算法竞赛题目Java程序设计算法竞赛题目Java程序设计算法竞赛题目
JAVA经典算法题目,非常好的算法,实用,学习、面试起很大的作用。
这是数据结构算法课程中算复杂度的作业及答案。
计算机算法基础题目归并分类上机答案要的速度看绝对正确啊
PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 ...
几道动态规划的经典算法 非常经典 值得分享
acm简单的算法设计题目100道 入门必做的题2 1.一种计算机病毒叫黑色星期五,如果当天是13号,又恰好是星期五,就会发作起来毁球计算机的存储系统,试编程找出九十年代中这种病毒可能发作的日期,假设1990年1月1日...
java经典题目,九十道好题目,经典算法,系统锻炼你的思维能力和编码技术,
ZTE2017中兴捧月算法大赛傅里叶题目及作品。资源内包含ZTE2017中兴捧月算法大赛傅里叶题目和作品。
毕设题目:关于HEVC帧间预测测试AMP模式的快速算法 毕设题目:关于HEVC帧间预测测试AMP模式的快速算法 毕设题目:关于HEVC帧间预测测试AMP模式的快速算法 毕设题目:关于HEVC帧间预测测试AMP模式的快速算法 毕设题目...
数据结构与算法\课程设计题目\各种各样有关数据结构与算法的题目
腾讯算法大赛2018年题目腾讯算法大赛2018年题目腾讯算法大赛2018年题目腾讯算法大赛2018年题目腾讯算法大赛2018年题目腾讯算法大赛2018年题目腾讯算法大赛2018年题目
算法高频题目精讲百度云链接
名企算法笔试题目