
algorithms
lwj1396
这个作者很懒,什么都没留下…
展开
-
Horner's rule
Horners Rule for Polynomials A general polynomial of degree can be written as <!-- MATH /begin{equation}P(x) = a_0 + a_1 x + a_2 x^2 + ... + a_n x^n = /sum_{i=0}^n a_i x^i/end{equation}转载 2008-07-18 10:43:00 · 8560 阅读 · 0 评论 -
数组实现大数相乘
还很粗糙,效率也不太高,有时间再封装到c++类。//数组自乘测试void multiply(int a[],int len,int n,int dd){ int LEN=len; int N=n; //初始化result int yy=LEN-1,xx=MAX-1; while(yy>=0) { aa[xx--]=a[yy--]; }原创 2008-10-23 11:56:00 · 4436 阅读 · 0 评论 -
8皇后问题的c++与python实现对比
c++经典书多,但貌似不太新,而python则新书很多,能体现一些新思路新概念。看python的书,写python代码,再用c++实现一遍,这样互补的学习方式貌似也蛮适合自己的。 在《Beginning Python From Novice to Professional》一书的八皇后问题上,python果然精妙而优雅。 在对待for each possibility at le原创 2008-12-31 09:47:00 · 2042 阅读 · 0 评论 -
plan
原创 2009-01-02 14:11:00 · 1158 阅读 · 0 评论 -
背包问题
此处背包问题描述为:N种大小和价值不等东西,容量为M的背包,求能带走的最大价值。 可以采用直接递归做法,但是时间复杂度为指数级别,代码如下 //递归版本,指数复杂度,cap为容量int knap(int cap){ int i,t,max,space; for(i=0,max=0;i<N;i++) if((space=cap-ite原创 2009-01-16 16:58:00 · 931 阅读 · 0 评论 -
约瑟夫环
数组实现链表:#include#include#define N 8#define M 5int item[N];int next[N];void init(){ int i; for(i=0;i<N;i++) { item[i]=i; next[i]=i+1; }原创 2009-01-14 13:05:00 · 809 阅读 · 0 评论 -
Stooge-sort
Stooge-sort(A, i, j)if A[i] > A[j] then exchange A[i], A[]if i + 1 >= j then returnk = (j - i + 1) / 3Stooge-sort(A, i, j - k)Stooge-sort(A, i + k , j)Stooge-sort(A, i, j - k)即先排序前2/3部分,然后是后2/3原创 2008-12-08 10:38:00 · 5974 阅读 · 0 评论 -
SNS广播的问题
广播的问题 * 快速有效跟踪友邻动态 * 最近的动态,而非所有动态 * 合并重复信息 * 减少刷屏的影响通常lifestream解决方案 twitter的方式 Twitter is, fundamentally, a messaging system.整个系统的后台是一个巨大的异步消息队列,每条广播被放大N倍发送出去,N为你的follower. 谁在转载 2009-03-10 21:34:00 · 783 阅读 · 0 评论 -
USACO:Section 1.1 Friday the Thirteenth
/*ID:lwj13961 TASK:fridayLANG: C++*/#include#include#includeusing namespace std;bool isleapyear(int year){ if(year%100!=0&&year%4==0) return true; if(year%100==0&&year%400=原创 2009-06-29 17:23:00 · 906 阅读 · 0 评论 -
radix sort &&bucket sort
//桶式排序bucket sort//radix sort较小的数作为基数,采取多趟桶式排序的方法。具体做法是最低有效位优先,将各数放入痛中;然后调整,依次类推到最高有效位。#include#include#include#include#define BUKETSIZE 10typedef struct listitem{ int _value; struct listitem原创 2008-08-02 14:28:00 · 4466 阅读 · 1 评论 -
counting sort(stable)
#include#includevoid counting_sort(int a[], int sorted[],int k,int size){ int *c=(int *)malloc((k+1)*sizeof(int)); int i=0; for(;i c[i]=0; for(i=0;i c[a[i]]=c[a[i]]+1; //n原创 2008-08-01 09:20:00 · 3926 阅读 · 0 评论 -
尾递归 - Tail Recursion
尾递归是针对传统的递归算法而言的, 传统的递归算法在很多时候被视为洪水猛兽. 它的名声狼籍, 好像永远和低效联系在一起.尾递归就是从最后开始计算, 每递归一次就算出相应的结果, 也就是说, 函数调用出现在调用者函数的尾部, 因为是尾部, 所以根本没有必要去保存任何局部变量. 直接让被调用的函数返回时越过调用者, 返回到调用者的调用者去.以下是具体实例:线性递归:lo转载 2008-07-30 10:15:00 · 4246 阅读 · 0 评论 -
归并排序
#include#include#include"common.h"void Merge(int a[],int p,int q,int r){ int l_len=q-p+1; int r_len=r-q; int *L= (int *)malloc((l_len+1)*sizeof(int)); int *R= (int *)malloc((r_len+1)*size原创 2008-07-17 11:32:00 · 3849 阅读 · 1 评论 -
插入排序
#include#include"common.h"int iarray[SIZE];//非递归版本void insert_sort(int a[],int size){ int i,j; int key; for(j=1;j { key=a[j]; i=j-1; while(i>0 && key {原创 2008-07-17 11:31:00 · 3630 阅读 · 0 评论 -
binary search
int binarysearch(int a[],int value,int l,int h){ int middle; if((l>=h)&&a[l]!=value) return -1; middle=l+(h-l)/2; if(value==a[middle]) return middle; if(value retu原创 2008-07-18 20:56:00 · 3798 阅读 · 0 评论 -
bubblesort
void bubblesort(int a[],int size){ int i,j,temp; for(i=0;i for(j=size-1;j>i;j--) if(a[j] { temp = a[j]; a[j]=a[j-1]; a[原创 2008-07-19 08:53:00 · 3726 阅读 · 0 评论 -
寻找逆序对
/* * Let A[1...n] be an array of n distinct numbers,If ia[j], * then pair(i,j) is called an inversion of A .This program determies * the number of inversions of n elements in o(nlgn) worst-case time原创 2008-07-19 10:12:00 · 3844 阅读 · 0 评论 -
大数阶乘问题
http://blog.youkuaiyun.com/liangbch/category/292924.aspx原创 2008-07-20 09:50:00 · 3731 阅读 · 0 评论 -
tableau problem
Problem 1: Young tableauAn m × n Young tableau is an m × n matrix such that the entries of each row are in sorted orderfrom left to right and the entries of each column are in sorted order from top to原创 2008-07-27 15:45:00 · 3933 阅读 · 0 评论 -
快速排序
#includevoid swap(int *a, int *b){ int temp=*a; *a=*b; *b=temp;}int partition(int a[],int low,int high){ int i=low-1; int j=low; for(;j { if(a[j]>=a[high]) {原创 2008-07-29 10:04:00 · 4017 阅读 · 1 评论 -
很好的技术文件集合
C Introduction to C Programming C Optimization Tutorial Compiling C and C++ Programs on UNIX Systems - gcc/g++ Building and Using Static and Shared C Libraries Programming in C: UNIX System Calls and原创 2009-11-20 14:12:00 · 5173 阅读 · 10 评论