A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: E…
题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 不能用条件语句,基本上只有考虑递归. 常规解法: 利用构造函数的每次初始化来实现递增 class Sum { public: Sum() {num++; sum+=num;} ; sum = ;} static int GetSum() { return sum; } private: static int num; static int sum; };…
题目:判断101-200之间有多少个素数,并输出所有素数. 思路:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数. 具体代码: public Vector exp(int first, int end) { Vector v = new Vector(); boolean b; for (int i = first; i <= end; i++) { b = true;// 假设是质数 for (int j = 2; j < i; j++)…
1.二叉树的建立 首先,定义数组存储树的data,然后使用list集合将所有的二叉树结点都包含进去,最后给每个父亲结点赋予左右孩子. 需要注意的是:最后一个父亲结点需要单独处理 public static TreeNode root; //建立二叉树内部类 class TreeNode{ public Object data; //携带变量 public TreeNode lchild,rchild; //左右孩子 public TreeNode() { data = null; lchild…
JAVA经典算法题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... public class exp2{ public static void main(String args[]){ int i=0; for(i=1;i<=20;i++) System.out.println(f(i)); } publ…
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 5442 Description A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", &qu…