
algorithm
文章平均质量分 54
wens07
天下风云出我辈
一入江湖岁月催
皇图霸业谈笑中
不胜人生一场醉
展开
-
Find the missing integer----programming pearls(second chapter)
Given a tape that contains at most 1,000,000 twenty-bit integers in random order, find a twenty-bit integer that isn't on the tape (and there must be at least one missing - Why?). a. How would you solve this problem with ample quantities转载 2010-10-26 23:18:00 · 2279 阅读 · 0 评论 -
新浪微博笔试题:删除字符串中多余的空格
给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个。比如 “ I like football ” 会变成 "I like football"函数接口为:void RemoveExtraSpace(char* str); 代码如下: void RemoveExtraSpace(char* str){ if(str == NULL原创 2011-10-21 16:09:11 · 933 阅读 · 0 评论 -
编程之美——2.21 只考加法的面试题
我们知道:1+2 = 3;4+5 = 9;2+3+4 = 9。等式的左边都是两个或两个以上连续的自然数相加,是不是所有的整数都可以写成这样的形式呢?问题1: 对于一个64位正整数,输出它所有可能的连续自然数(两个以上)之和的算式。问题2: 大家在测试上原创 2011-08-26 20:45:24 · 685 阅读 · 0 评论 -
输出和为n的所有的连续自然数序列
输出和为n的所有的连续自然数序列 如 n = 9: 9 4 5 2 3 4 《编程之美》的题目(2.21只考加法的面试题),去年曾经写过本题的代码,后来不知道把代码放哪里了。按以前的思路,重写了下代码,写完后翻了下书,原创 2011-08-26 20:51:29 · 771 阅读 · 0 评论 -
输出自然数n的所有因子
假设 n = i * j (i = 0 思路一: j = n / i >= i (注意:i – int(n / i) 随着i的增大而增大,int(n/i)指n/i的整数部份)从i = 1 开始判断 i是否能被n整除,当int(n / i)原创 2011-08-26 20:49:01 · 1103 阅读 · 0 评论 -
求一个数的立方的新方法
JOJ的2042题目也是一个程序理解题目,这个题目非常有意思,给出了下面一段C++源代码,要求计算出最后的输出结果,源代码如下: #include int main(void) { int x = 987654原创 2011-08-26 11:30:41 · 1356 阅读 · 2 评论 -
编程之美——2.20 程序理解和时间分析
最近在看《编程之美》,为找工作面试做准备。该书中2.20程序理解和时间分析一题没有给出解答,所以简单写一下我自己的答案。题目如下:阅读以下C#代码,回答问题: using System;using System.Collections.Generic原创 2011-08-26 11:20:18 · 2381 阅读 · 0 评论 -
编程之美——2.8 找符合条件的整数
题目:任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0.解决这个问题首先考虑对于任意的N,是否这样的M一定存在。可以证明,M是一定存在的,而且不唯一。简单证明:因为 这是一个无穷数列,但是数列中的每一项取值范围都在原创 2011-08-21 14:44:13 · 925 阅读 · 0 评论 -
最大公约数算法
算法一:欧几里得算法原理: gcd (a,b) = gcd(b, a mod b)c++ 算法表述如下:int gcd(int a, int b){ if (b == 0) return a; else r原创 2011-08-20 22:05:10 · 451 阅读 · 0 评论 -
ZOJ 1013 Great Equipment (DP)
很巧妙很经典的一道DP, 其中dp[n][x][y] 表示前n辆车装了x个装备1和y个装备2之后能装的最多的装备3的个数,而把第n辆车上装的ix,iy装备去掉后就是前n-1辆车在拿了x - ix,y - iy个1,2装备后所能装的最大的装备3的个数所以 dp[n][x原创 2011-08-20 16:17:46 · 869 阅读 · 0 评论 -
Perfect Cubes(zoj_1331-————the beginner problem)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=331解答如下(注意裁剪即可):#include #include using namespace std;const int MAX = 200; /原创 2011-07-27 21:53:32 · 394 阅读 · 0 评论 -
约瑟夫环问题(1)----Wikipedia上的原文解答
Josephus Problems: There are people standing in acircle waiting to be executed. After the first person is executed, a certain number of people are skipped and one person is executed. Then,原创 2011-11-13 22:50:46 · 550 阅读 · 0 评论