随笔分类 -  算法

摘要:参考:http://www.cnblogs.com/blair/p/3229580.html(参考该文章写成)double tail(double x){ return x - floor(x);}// 判断y年m月(1,2,..,12,下同)d日是Gregorian历还是Julian历//(opt=1,2,3分别表示标准日历,Gregorge历和Julian历),是则返回1,是Julian历则返回0,// 若是Gregorge历所删去的那10天则返回-1int ifGregorian(int y, int m, int d, int opt){ if (opt == 1) ... 阅读全文
posted @ 2014-03-11 12:47 翛尧 阅读(1331) 评论(0) 推荐(0) 编辑
摘要:农历数据解析:1901 - 2100 农历数据:const int HHLunarCalendarInfo[] = { 0x04AE53,0x0A5748,0x5526BD,0x0D2650,0x0D9544,0x46AAB9,0x056A4D,0x09AD42,0x24AEB6,0x04AE4A,/*1901-1910*/ 0x6A4DBE,0x0A4D52,0x0D2546,0x5D52BA,0x0B544E,0x0D6A43,0x296D37,0x095B4B,0x749BC1,0x049754,/*1911-1920*/ 0x0A4B48,0x5B25BC,0x06A550... 阅读全文
posted @ 2014-03-11 12:32 翛尧 阅读(970) 评论(0) 推荐(0) 编辑
摘要:干支纪年法的简便算法 传统的计算法,必须知道一个已知年,然后往前后推算,有一定的难度,下面我给大家介绍一种干支纪年法的简便算法,每个字都有对应的一个符号。如下表: 4 5 6 7 8 9 0 1 2 3 甲 乙 丙 丁 戊 己 庚 辛 壬 癸 4 5 6 7 8 9 10 11 0 1 2 3 子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥 4 5 6 7 8 9 10 11 0 1 2 3 鼠 牛 虎 兔 龙 蛇 马 羊 猴 鸡 狗 猪 年份的最后一个数字就对应天干的相应的字,然后用年份除以12所得的余数,用这个余数去找相对应的地支字和十二生肖字,于是天干地支字的组合便是干支纪年... 阅读全文
posted @ 2014-03-11 12:27 翛尧 阅读(1595) 评论(0) 推荐(0) 编辑
摘要:原文: http://liyiwen.iteye.com/blog/705489之前 comp.graphic.algorithms 上有一个讨论,是关于怎么样使用曲线对多边形进行插值处理,使得最终产生的曲线是光滑的而且能通过所有的顶点。Gernot Hoffmann 建议说使用著名的 B-Spline 来进行插值。这里有他当时的文章。B-Spline 在这里效果很好,它看起来就像是一个固定在多边形顶点上的橡皮尺(elastic ruler)。 但我有个大胆的推测,我觉得肯定还存在更简单的方法。比如,使用三次贝塞曲线(cubic Bezier)进行近似。贝塞尔曲线有两个固定点(起点和终点),另 阅读全文
posted @ 2013-09-18 10:12 翛尧 阅读(396) 评论(0) 推荐(0) 编辑
摘要:#include <cstdlib>#include <iostream>using namespace std;template<typename T>class Heap//最大堆{private: int currentSize; int maxSize; T *heapArray; public: Heap(int size = 10) { currentSize = 0; maxSize = size; //初始化堆容量 heapArray = new T[maxSize]; //创建堆... 阅读全文
posted @ 2012-10-22 14:25 翛尧 阅读(300) 评论(0) 推荐(0) 编辑
摘要:从给定的N个正数中选取若干个数之和最接近M阶段是:在前N件物品中,选取若干件物品放入背包中; 状态是:在前N件物品中,选取若干件物品放入所剩空间为W的背包中的所能获得的最大价值; 决策是:第N件物品放或者不放; 由此可以写出动态转移方程: 我们用f[i,j]表示在前 i 件物品中选择若干件放在所剩空间为 j 的背包里所能获得的最大价值 f[i,j]=max{f[i-1,j-Wi]+Pi (j>=Wi), f[i-1,j]} 这个方程非常重要,基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以有必要将它详细解释一下:“将前i件物品放入容量为v的背包中”这个子问题,若只考虑... 阅读全文
posted @ 2012-10-22 10:49 翛尧 阅读(183) 评论(0) 推荐(0) 编辑
摘要:#include<iostream>#include<math.h>#define N 100000 //生成100000个质数using namespace std;int prime[N]; //一个全局数组,用来保存质数表void makeprime()//生成质数表的子函数{ int j,n=29,i=9,sqrtn;//从第10个质数开始计算,第10个质数是29 prime[0]=2; prime[1]=3; prime[2]=5; prime[3]=7; prime[4]=11; prime[5]=13; prime[6]=17; ... 阅读全文
posted @ 2012-10-16 21:53 翛尧 阅读(388) 评论(0) 推荐(0) 编辑
摘要:bool findString(char* str, char* t){ if('\0' == *t) return 1; if('*' == *t){while('\0' != *str){if(findString(str++, t + 1))return 1;}}if('\0' == *str)return 0;if('?' == *t || *str == *t){return findString(str + 1, t + 1);}return 0;} 阅读全文
posted @ 2012-09-28 23:05 翛尧 阅读(373) 评论(0) 推荐(0) 编辑
摘要:#include<iostream>#include<string>usingnamespacestd;stringadd(conststring&a,conststring&b){stringresult;//用于记录计算结果intlen_a=a.length()-1;intlen_b=b.length()-1;intcarry=0;//进位for(;len_a>=0&&len_b>=0;len_a--,len_b--){intt=(a[len_a]-'0')+(b[len_b]-'0')+c 阅读全文
posted @ 2012-09-21 23:17 翛尧 阅读(380) 评论(0) 推荐(0) 编辑
摘要:#include <cstdlib>#include <iostream>using namespace std;int Partition(int list[], int low, int high){ int pivotkey = list[low]; while(low < high) { while(low<high && list[high]>= pivotkey) high--; //找到第一个小于key的记录 if(low < high) list[low++] = list[high];//相当于交换了list[i 阅读全文
posted @ 2012-09-07 16:00 翛尧 阅读(106) 评论(0) 推荐(0) 编辑
摘要:#include <stdio.h>#include <stdlib.h>static char Queen[8][8];static int a[8];static int b[15];static int c[15];static int iQueenNum = 0;//record statevoid qu(int i);int main(int argc, char *argv[]){ //init int line, column; for(line = 0;line < 8;line++) { a[line] = 0; for(colu... 阅读全文
posted @ 2012-09-06 16:06 翛尧 阅读(127) 评论(0) 推荐(0) 编辑
摘要:使用异或a = a^b;b = a^b;a = a^b;异或运算法则 1. a ^ b = b ^ a 2. a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c; 3. d = a ^ b ^ c 可以推出 a = d ^ b ^ c. 4. a ^ b ^ a = b. 阅读全文
posted @ 2012-09-05 16:31 翛尧 阅读(216) 评论(0) 推荐(0) 编辑
摘要:int max = ((a+b) + abs(a-b)) / 2; 阅读全文
posted @ 2012-09-05 16:26 翛尧 阅读(171) 评论(0) 推荐(0) 编辑
摘要:1 //判断射线与也线段是否相交, 相交返回1,不相交返回0,在边上返回-1 2 int IsIntersectant( CPoint ptStart, CPoint ptEnd, CPoint pd ) 3 { 4 double tempx = 0; 5 double tempy = 0; 6 //记录多边形边的端点坐标; 7 double startx = ptStart.x; 8 double starty = ptStart.y; 9 double endx = ptEnd.x; 10 double endy = ptEnd.y; 11 ... 阅读全文
posted @ 2011-12-09 09:42 翛尧 阅读(1877) 评论(1) 推荐(0) 编辑
摘要:1 //判断矩形是否相交 2 bool FMath::IsRectIntersect(const FRect& rect1, const FRect& rect2) 3 { 4 bool bResult = true; 5 6 double dWidthRectA; 7 double dHeightRectA; 8 9 double dWidthRectB;10 double dHeightRectB;11 12 //方便计算,中心点坐标为实际坐标的2倍13 CPoint centerRectA;14 CPoint cent... 阅读全文
posted @ 2011-12-09 09:38 翛尧 阅读(362) 评论(0) 推荐(0) 编辑