
ACM-数学题
文章平均质量分 71
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
POJ 1316 self numbers 整数各位相加处理
这题很简单,直接贴AC代码了#include #include #define MAXN 10000using namespace std;int num[MAXN];int dnum(int i){ int sum = i; int temp; while (i > 0) { temp = i%10; sum += temp; i = (i-temp)/10; } return sum;}int main(){ int i,j;原创 2010-11-27 16:24:00 · 1608 阅读 · 0 评论 -
LeetCode Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible ord原创 2015-01-12 14:14:33 · 1616 阅读 · 0 评论 -
LeetCode Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?思路分析:这题比较容易想到的解法是先检查是否有环,然后检查每个node是否在环内,但是是O(N^2)的解原创 2015-01-19 15:00:32 · 1797 阅读 · 0 评论 -
POJ 1828 选猴王 排序qsort应用
<br /> 今天总算上完一门课了,以后做OJ的时间可以渐渐多一些。好几天疲于奔命,OJ也没写几题,囧。今天补起来,补一篇处女博!<br /> 这题很简单,但是第一次做的时候没有排序采用暴力判断超时,后来想到先排序再判断更快。<br />#include <iostream>using namespace std;struct mon{ int x; int y;};//按照x y升序排列,如果不排序直接判断容易超时int cmp(const void *a,const原创 2010-12-04 21:40:00 · 1610 阅读 · 0 评论 -
面试题 从很长的数据流等概率随机采样 蓄水池抽样 Reservoir Sampling
题目:有一个网页抓取器每秒钟抓取一个网页,定义一个API,每次调用的时候要等概率的从目前已经抓取的网页中随机选取一个,应该怎么实现?分析:这题题目定义有一定迷惑性,最直接的思路貌似应该是先保存当前采集到的所有网页,然后随机采样,这显然不是这题的考点。这题想只用O(1)的空间。其实就等价于有一个很长的数据流,数据量大到无法载入内存,怎么做随机等概率采样?容易想到的思路是产生一个0到1之间的随机数,然转载 2015-02-24 11:47:58 · 12861 阅读 · 0 评论 -
LeetCode Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before coding. Bonus poi原创 2015-03-18 14:15:36 · 1254 阅读 · 0 评论 -
LeetCode Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of原创 2015-05-31 11:36:55 · 2225 阅读 · 0 评论 -
LeetCode Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.思路分析:这题是一道数学题,求小于n的所有质数。容易想到的思路是定义个isPrime的判定函数,对小于n的数一个一个判定,但是时间复杂度O(N^2)。有没有更快的算法呢?有,这是一个经典的找质数的算法,Sieve of Eratosthe原创 2015-05-25 14:57:09 · 2686 阅读 · 0 评论 -
LeetCode Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For原创 2015-07-19 14:47:06 · 4011 阅读 · 0 评论 -
LeetCode Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always原创 2015-07-20 14:58:41 · 3112 阅读 · 0 评论 -
POJ 2017 计算公路里程 模拟题
水题一道,以后还是不做水题,没什么收获Source CodeProblem: 2017 User: yangliuACMerMemory: 244K Time: 0MSLanguage: C++ Result: Accepted#include using namespace std;int原创 2012-02-10 16:54:49 · 3201 阅读 · 2 评论 -
POJ 2388 求中位数 qsort用法
这题就是求中位数,直接用qsort排序取中,算法复杂度为O(NlogN)当然求中位数的最优算法是O(N)的Select算法Source CodeProblem: 2388 User: yangliuACMerMemory: 284K Time: 63MSLanguage: C++ Result: Accepted原创 2012-02-11 13:23:29 · 2050 阅读 · 0 评论 -
POJ 1547 Clay Bully 结构体数组使用题意理解
这题很简单,可以设置结构体保存三维及姓名,求出体积最大的人和最小的人输出即可。 由于没有注意输出"."而贡献了一次WA,囧#include #include #include using namespace std;struct stu{ stu(){} string name; int d;}s[1000];int comp(const void * a,const void * b){ return ((stu*)a)->d - ((stu*)b)原创 2010-11-27 20:47:00 · 1939 阅读 · 0 评论 -
POJ 1326 飞机里程 四舍五入处理
今天主要是学习了一下DP,三个使用前提:最有子结构、子问题重叠性、无后效性,今后重点练dp题,至少做30题加深理解 这题纯粹为了练习编码速度。#include #include using namespace std;int main(){ int c,miles = 0; string a; char b; while(cin>>a && a[0] != '#'){ if (a[0] != '0') { cin>>a>>c>>b;原创 2010-11-28 21:33:00 · 1624 阅读 · 0 评论 -
POJ 3744 数学题概率题 矩阵乘幂
转的网上的解题报告http://hi.baidu.com/rpsproblem/blog/item/d2cbe67aa1d8b5fd0bd1875f.html,用于备战校赛按照题目的意思,我们很容易推出公式:f[i] = p*f[i-1] + (1-p)*f[i-2];f[i]表示到达位置i的概率(不是安全到达那些很复杂之类的概率,就只是简单的到达的概率),p为题目给的概率由于有炸弹的原因,就不能直接算了。这里我用 x[i]表示第i颗炸弹的位置但如果把整个过程按照炸弹来分阶段,起点为x[i-1]+1转载 2010-12-11 12:51:00 · 1927 阅读 · 0 评论 -
POJ 1218 囚犯问题 数学题找规律
<br /> <br />这是北信夏令营原题B题,难怪ACMER都可以飞速AC掉,他们做的都是已经做过的题,能不快吗?直接贴考试时的代码了<br />#include <stdio.h>int n,a[102]={0},i,j,changdu,count=0,jiange;void main(){ scanf("%d",&n); while(n--){ scanf("%d",&changdu); for(i=1;i<=changdu;i++){ a[i] = 0;原创 2010-12-09 21:44:00 · 1686 阅读 · 0 评论 -
POJ 3070 Fibonacci数列 矩阵乘法及乘幂求法
可以先推算一下,会发现m0,m1,m2,m3等为方阵的乘幂,其第1行第2列就是Fibonacci数列的项,关键是如何求矩阵的乘幂呢?分奇数幂和偶数幂两种情况A(2)^n因为方阵的乘法有结合律,所以A(2)^n=A(2)^(n/2)*A(2)^(n/2),不妨设n是偶数所以求A(n)就可以化成求A(n/2)并作一次乘法,所以递归方程是:A(n)=A(n/2)^2但是如果n为奇数,可以A(n)=A((n-1)/2) * A((n+1)/2或者A(n)=A((n-1)/2)^2 * A(1)#include原创 2010-12-11 17:16:00 · 2023 阅读 · 0 评论 -
POJ 1061 青蛙的约会 数学题
<br /> <br />开始用了暴力枚举,后来一看数据这么大,估计肯定超时,无奈上网搜索了一下,考察的是扩展欧几里德算法,还有比较大的整数_int64的处理。<br />转自http://apps.hi.baidu.com/share/detail/16229280<br /> 设过s步后两青蛙相遇,则必满足以下等式:<br /> (x+m*s)-(y+n*s)=k*l(k=0,1,2....)<br /> 稍微变一下形得:<br /> (n-m)*s+k*l=x-y<br /> 令转载 2010-12-17 23:08:00 · 1707 阅读 · 0 评论 -
POJ 2085 Inversion 数学题
对于任意一个序列i, i + 1, ..., j其最大的inversion number是全部逆序的情况,即j, j - 1, ..., i + 1, i,值记为in(i, j) = (j - i + 1) * (j - i) / 2所以这个问题的解决的步骤如下:(1)对于输入n, seq, 从后往前考虑找到可以涵盖seq值的i, 即in(i, n) >= seq(2)由(1)可知,i -> n足够用来表示值为seq的insersion number,所以1-> i - 1只要按照升序打印即可(3)剩下的i转载 2010-12-20 23:37:00 · 2289 阅读 · 0 评论 -
POJ 1006 解同余方程
POJ 1006原创 2010-11-08 17:29:00 · 1534 阅读 · 0 评论 -
POJ 1207 求最大数链长度 暴力枚举数学题
这个题目直接用的暴力枚举,但是还是WA了几次原因是这句话You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j. 注意i可能大于j,此时需要交换顺序,但是输出的原创 2012-02-10 21:57:32 · 1799 阅读 · 0 评论 -
LeetCode Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Hint:How many majority elements could it possibly hav原创 2015-07-20 15:10:04 · 3796 阅读 · 1 评论