ITeye博客

EmmaZhao的博客

EmmaZhao · 共 34 篇文章

判断一个数是质数的几种方法

质数也叫素数,是只能被1和它本身整除的正整数,最小的质数是2,目前发现的最大的质数是p=2^57885161-1【注1】。 判断一个数是质数的最简单的方法如下: def isPrime1(n): for i in range(2, n): if n % i == 0: return False return True 但是在上面的方法中有一些冗余的计算,所以做以下改进 def isPrime2(n): import math if n == 1: return False elif n < 4: return True else: r = int(math.floor(math.sqrt(n…

2013-05-17 16:28 · math · 评论(0)

POJ1276

#include<iostream> using namespace std; int main() { int cash,n,max; while(cin>>cash>>n) { max = 0; int m[1001],v[1001]; for(int i = 0;i<n;i++) cin>>m[i]>>v[i]; bool dp[100001]; memset(dp,0,sizeof(dp)); dp[0] = true; for(int i = 0;i<n;i++) { for(int j = max;j>=0;j--) { if(dp[j]) { for(int k = 1;k <…

2013-05-16 12:10 · 算法 · 评论(0)

Project Euler 23:Find the sum of all the positive integers which cannot be writt

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. A number n is called deficient if the sum of its proper divisors is less…

2013-05-16 12:08 · math · 评论(0)

ZOJ1046

#include<iostream> #include<math.h> //248K 16MS using namespace std; typedef struct RGB { short R; short G; short B; }; int D(RGB a,RGB b) { int t1 = (a.R - b.R) * (a.R - b.R); int t2 = (a.G - b.G) * (a.G - b.G); int t3 = (a.B - b.B) * (a.B - b.B); return sqrt(1.0 * (t1 + t3 +t2)); } int main() { R…

2012-12-14 10:02 · 评论(0)

POJ2533

#include<iostream> using namespace std; int main() { int dp[10001]={0},n,a[10001]={0}; while(cin>>n) { for(int i = 1;i<=n;i++) cin>>a[i]; dp[1] = 1; int max,tmp; for(int i = 2;i<=n;i++) { dp[i] = 1; for(int j = i-1;j>=1;j--) { if(a[i] >a[j]) dp[i] = dp[i]>dp[j]+1?dp[i]:dp[j]+1; } } max = dp[1]; for…

2012-12-13 00:01 · 评论(0)

POJ3176

#include<iostream> #define N 350 using namespace std; int main() { int n,max; int t[n+1][n+1]={0},dp[n+2][n+2]={0}; cin>>n; for(int i = 1;i<=n;i++) for(int j = 1;j<=i;j++) cin>>t[i][j]; max = t[1][1]; for(int i = 1;i<=n+1;i++) { for(int j = 1;j<=n+1;j++){ int tmp1,tmp2; tmp1 = dp[i-1][j-1] + t[i-1]…

2012-12-12 12:06 · 评论(0)

POJ1080

#include<iostream> #include<string> using namespace std; int value['t'+1]['t'+1]; int dp[101][101]; int max(int a,int b,int c) { int k = (a>b)?a:b; return (c>k)?c:k; } int main(){ value['a']['a'] = value['c']['c']=value['t']['t']=value['g']['g'] =5; value['a']['c'] = value['c']['a'] = -1; value['a'…

2012-12-12 11:11 · 评论(0)

POJ1028

#include<iostream> #include<string> using namespace std; int main() { string websites[102]; websites[1] = "http://www.acm.org/"; string str,tmp; int i = 1,max; while(cin>>str && (str[0] != 'Q')){ if(str[0] == 'V') { cin>>tmp; websites[++i] = tmp; max = i; cout<<tmp<<endl; } else if(str[0] == 'B'){ …

2012-12-12 09:30 · 评论(0)

POJ1006

#include<iostream> using namespace std; int main() { int p,e,i,d,num = 1; cin>>p>>e>>i>>d; while((p!=-1) || (e!=-1) || (i!=-1) || (d!=-1)){ while((p!=e) || (p!=i)){ if(p<e){ if(p<i){ p+=23; }else i+=33; } else{ if(e<i) e+=28; else i+=33; } } int days = (21252+p-d)%21252; if(days == 0) days = 21252;…

2012-12-11 12:00 · 评论(0)

在vMware7.0中安装apache2

使用apt-get install apache2后,修改配置文件: 默认的配置在/etc/apache2/sites-available/default文件中 2.默认的http.conf是空的,如果要修改ServiceName,在hrttp.conf中添加一句即可 3.端口信息在ports.conf中,如果要修改端口,则要修改其中的内容 4.主机名设置成127.0.0.1:端口 4.配置虚拟机的网络连接为桥接模式 5.关闭虚拟机防火墙ubuntu的命令是sudo ufw disable 6.在虚拟机中查看ip地址,在主机浏览器中输入即可。

2012-12-01 20:55 · 评论(0)

【转】2011阿里云计算研发中心笔试题(45minutes)

2011阿里云计算研发中心笔试题(45minutes) [url]http://oneweaklight.itpub.net/post/42951/519364[/url] 应聘职位:软件开发工程师-数据平台 1.状态转换图,有限自动机,正则表达式 2.最小堆,删除堆根节点,画出任意结果 3.Heap与stack在进程中的区别 4.硬盘概率问题,对立事件,独立事件,平均分布,一季度,一年 5.工厂分布距离最小问题 6.多线程输出变量的所以可能值 7.补充题,堆排序补充 8.整数数组中求相加和最大的子数组,时间复杂度为O(n),编程语言不限(C/C /JAVA) 附加知识: 一个由C/C++编…

2012-07-24 09:54 · 求职面试 · 评论(0)

微软面试100题_9

题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。 如果是返回true,否则返回false。 例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: 8 / \ 6 10 / \ / \ 5 7 9 11 因此返回true。 如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。 package cn.emma.interview_9; public class Ex9 { public static boolean isBackOrder(int[] a,int left,int right){ int root =…

2012-05-21 17:24 · 求职面试 · 求职面试 · 评论(0)

名企面试100题_16

输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。 public static void hierarchyTraverse(BinaryTree tree){ Queue queue = new Queue(); if(tree.getRoot() != null){ queue.enQueue(tree.getRoot()); while(!queue.emptyStack()){ TreeNode p = queue.deQueue(); System.out.print(p.getVlaue() + " "); if(p.left != null){ qu…

2012-05-21 11:42 · 求职面试 · 评论(0)

名企面试100题_15

第15题(树): 题目:输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 用递归和循环两种方法完成树的镜像转换。 package cn.emma.interview_15; public class Mirror { public static void getMirror(BinaryTree.TreeNode tree){ if(tree != null){ BinaryTree.TreeNode temp = tree.left; tree.left = tree.right; tree.right = temp; getMirro…

2012-05-17 16:46 · 算法 · 评论(0)

名企面试100题_14

第14题(数组): 题目:输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。 思路: package cn.emma.interview_14; public class GetDivision { public static void getDivision(int[] a, int n){ int i,j; for(i = 0,j=a.length-1;i<j;){ whi…

2012-05-17 16:42 · 求职面试 · 评论(0)