??xml version="1.0" encoding="utf-8" standalone="yes"?>
在开发数据库应用中,l常?x)遇到处理时间的问题Q如查询指定旉的记录等。下面就q些常见的问题,l合自己的一些经验,和大家探讨一下这c问题?br />
首先介绍一下,SQL Server里处理时间的几个主要函数的用法:(x)
getdate()函数Q取得系l当前的日期和时间。返回gؓ(f)datetimecd的。
用法Qgetdate()
例子Q?
select getdate() as dte,dateadd(day,-1,getdate()) as nowdat
输出l果Q?
dte nowdat
--------------------------- ---------------------------
1999-11-21 19:13:10.083 1999-11-20 19:13:10.083
(1 row(s) affected)
datepart()函数Q以整数的Ş式返回时间的指定部分?
用法Qdatepart(datepart,date)
参数说明Qdatepart时要q回的时间的部分Q常用取值year、month、day、hour、minute?br />
date是所指定的时间?br />
例子Q?br />
SELECT DATEPART(month, GETDATE()) AS 'Month Number'
输出l果Q?br />
Month Number
------------
11
(1 row(s) affected)
dateadd()函数Q通过l指定的旉的指定部分加上一个整数gq回一个新旉倹{?br />
用法Qdateadd(datepart,number,date)
参数说明Qdatepart(同上Q?br />
date(同上)
number要增加的|整型Q可正可负,正D回date之后的时间|负D回date
之前的时间?br />
例子Q?br />
select getdate() as today
select dateadd(day,-1,getdate())
select dateadd(day,1,getdate())
输出Q?br />
today
---------------------------
1999-11-21 19:42:41.410
(1 row(s) affected)
yesterday
---------------------------
1999-11-20 19:42:41.410
(1 row(s) affected)
tomorrow
---------------------------
1999-11-22 19:42:41.410
(1 row(s) affected)
datediff()函数Q返回两个时间以指定旉部分来计的差倹{返回整数倹{如1991-6-12?991-6-21之间以天
来算相差9?1998-6-12?999-6-23按年相?q_(d)1999-12-1?999-3-12按月相?个月
用法Qdatediff(darepart,date1,date2)
参数说明QdatepartQ同?
date1、date2(同上date)
例子Q?br />
select datediff(month,'1991-6-12','1992-6-21') as a
输出Q?br />
a
-----------
12
(1 row(s) affected)
]]>
var prop1 = {asd:{def:'abc'}};
var prop2 = {asd:{def:'abc'}};
alert( prop1==prop2)//false
alert( prop1['asd']==prop2['asd'] ) ;//false
alert( prop1['asd']['def']==prop1['asd']['def'] ) ; //true
阅读全文
]]>
你对q个问题困惑q吗Q?
阅读全文
]]>
l过切换不同的关键字Q我最后找C(jin)正确的配|?nbsp; 阅读全文
]]>
(tng)有一?7厘米的细木杆Q在W?厘米?厘米?1厘米?7厘米?3厘米q五个位|上各有一只蚂蚁?木杆很细Q不能同旉过一只蚂蚁。开?Ӟ蚂蚁的头朝左q是朝右是Q意的Q它们只?x)朝前走或调_(d) 但不?x)后退。当L两只蚂蚁头Ӟ两只蚂蚁?x)同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距R?~写E序Q求所有蚂蚁都d木杆 的最时间和最大时间?/p>
看了(jin)q个题目之后,H然很感兴趣,今天搞了(jin)半天把它做出来了(jin),大概׃(jin)1个半时.大公司的题目真是考h.反正都已l用法实现?我就不多说了(jin),大家看代码吧.代码里面注释我也量全写?一共有两个c?一个是Ant的模?一个是控制c?原代?大家可以在这取得:
http://www.aygfsteel.com/Files/itspy/baidu.rar
//////////////////////////////////////
/*癑ֺ面试?br /> (tng)* 有一?7厘米的细木杆Q在W?厘米?厘米?1厘米?7厘米?3厘米q五个位|上各有一只蚂蚁?br /> (tng)* 木杆很细Q不能同旉过一只蚂蚁。开?Ӟ蚂蚁的头朝左q是朝右是Q意的Q它们只?x)朝前走或调_(d)
(tng)* 但不?x)后退。当L两只蚂蚁头Ӟ两只蚂蚁?x)同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距R?br /> (tng)* ~写E序Q求所有蚂蚁都d木杆 的最时间和最大时间?br /> (tng)*
(tng)*
(tng)* 分析:题目中的蚂蚁只可能相遇在整数?不可以相遇在其它?比如3.5cm处之cȝ,也就是可以让每只蚂蚁?1U?然后
(tng)* 查看是否有相遇的卛_.
(tng)*
(tng)* q样我的E序实现思\是,初始?只蚂?让每只蚂蚁走1U?然后看是否有盔R?如果有则做相应处?当每只蚂蚁都
(tng)* 走出木杆?我就记录当前旉.q样可以得到当前状态情况下,需要多久可以走出木?然后遍历所有状态则可以得到所?br /> (tng)* 可能.
(tng)*/
package baidu;
public class Ant {
(tng)/*
(tng) * step 表示蚂蚁每一个单位时间所走的长度
(tng) */
(tng)private final static int step = 1;
(tng)/*
(tng) * position表示蚂蚁所处的初始位置
(tng) */
(tng)private int position;
(tng)/*
(tng) * direction表示蚂蚁的前q方向,如果?表示?7厘米的方向走Q?如果为-1Q则表示往0的方向走?br /> (tng) */
(tng)private int direction = 1;
(tng)/*
(tng) * 此函数运行一ơ,表示蚂蚁前进一个单位时_(d)如果已经C木杆则会(x)抛出异常
(tng) */
(tng)public void walk() {
(tng) (tng)if (isOut()) {
(tng) (tng) (tng)throw new RuntimeException("the ant is out");
(tng) (tng)}
(tng) (tng)position = position + this.direction * step;
(tng)};
(tng)/**
(tng) * (g)查蚂蚁是否已l走出木杆,如果走出q回true
(tng) *
(tng) */
(tng)public boolean isOut() {
(tng) (tng)return position <= 0 || position >= 27;
(tng)}
(tng)/**
(tng) * (g)查此蚂蚁是否已经遇到另外一只蚂?br /> (tng) * @param ant
(tng) * @return 如果遇到q回true
(tng) */
(tng)public boolean isEncounter(Ant ant) {
(tng) (tng)return ant.position == this.position;
(tng)}
(tng)/**
(tng) * 改变蚂蚁的前q方?br /> (tng) */
(tng)public void changeDistation() {
(tng) (tng)direction = -1 * direction;
(tng)}
(tng)/**
(tng) * 构造函?讄蚂蚁的初始前q方?和初始位|?br /> (tng) * @param position
(tng) * @param direction
(tng) */
(tng)public Ant(int position, int direction) {
(tng) (tng)this.position = position;
(tng) (tng)if (direction != 1) {
(tng) (tng) (tng)this.direction = -1;//方向讄初始位置,比如??也将其设|ؓ(f)1.q样可以方便后面的处?br /> (tng) (tng)} else {
(tng) (tng) (tng)this.direction = 1;
(tng) (tng)}
(tng)}
}
(tng)
/////////////////////////////////////////////////////////
package baidu;
public class Controller {
(tng)public static void main(String[] args) {
(tng) (tng)int time = 0;
(tng) (tng)for (int i = 0; i < 32; i++) {
(tng) (tng) (tng)Ant[] antArray = getAntList(getPoistions(), getDirections(i));
(tng) (tng) (tng)while (!isAllOut(antArray)) {
(tng) (tng) (tng) (tng)for (Ant ant : antArray) {
(tng) (tng) (tng) (tng) (tng)if (!ant.isOut()) {
(tng) (tng) (tng) (tng) (tng) (tng)ant.walk();
(tng) (tng) (tng) (tng) (tng)}
(tng) (tng) (tng) (tng)}
(tng) (tng) (tng) (tng)time++;
(tng) (tng) (tng) (tng)// 查看是否有已l相遇的Ant,如果有则更改其前q方?br /> (tng) (tng) (tng) (tng)dealEncounter(antArray);
(tng) (tng) (tng)}
(tng) (tng) (tng)System.out.println(time);
(tng) (tng) (tng)// 时间归0,q样可以重新讄条g,再次得到全部走完所需要的旉.
(tng) (tng) (tng)time = 0;
(tng) (tng)}
(tng)}
(tng)/**
(tng) * q个函数的算法很乱,但暂时能解决问题
(tng) *
(tng) * @param list
(tng) */
(tng)public static void dealEncounter(Ant[] antArray) {
(tng) (tng)int num_ant = antArray.length;
(tng) (tng)for (int j = 0; j < num_ant; j++) {
(tng) (tng) (tng)for (int k = j + 1; k < num_ant; k++) {
(tng) (tng) (tng) (tng)if (antArray[j].isEncounter(antArray[k])) {
(tng) (tng) (tng) (tng) (tng)antArray[j].changeDistation();
(tng) (tng) (tng) (tng) (tng)antArray[k].changeDistation();
(tng) (tng) (tng) (tng)}
(tng) (tng) (tng)}
(tng) (tng)}
(tng)}
(tng)/**
(tng) * 因ؓ(f)?只AntQ所以组合之后有32U组?刚好?位二q制来表C?如果?则表CAnt往0的方向走 如果?,则表C往27的方向走
(tng) *
(tng) * ?在通过Ant的构造函数设|初始值时,通过qo(h)?修改成了(jin)-1.
(tng) */
(tng)public static int[] getDirections(int seed) {
(tng) (tng)int result[] = new int[5];
(tng) (tng)result[0] = seed % 2;
(tng) (tng)result[1] = seed / 2 % 2;
(tng) (tng)result[2] = seed / 4 % 2;
(tng) (tng)result[3] = seed / 8 % 2;
(tng) (tng)result[4] = seed / 16 % 2;
(tng) (tng)System.out.println("directions is " + result[0] + "|" + result[1] + "|"
(tng) (tng) (tng) (tng)+ result[2] + "|" + result[3] + "|" + result[4]);
(tng) (tng)return result;
(tng)}
(tng)/**
(tng) * 扚w讄Ant的初始位|?q样讄不是十分必要,可以直接在代码中讄
(tng) *
(tng) * @return
(tng) */
(tng)public static int[] getPoistions() {
(tng) (tng)return new int[] { 3, 7, 11, 17, 23 };
(tng)}
(tng)/**
(tng) * 取得讄好初始值的5只Ant
(tng) *
(tng) * @param positions
(tng) * @param directions
(tng) * @return
(tng) */
(tng)public static Ant[] getAntList(int[] positions, int[] directions) {
(tng) (tng)Ant ant3 = new Ant(positions[0], directions[0]);
(tng) (tng)Ant ant7 = new Ant(positions[1], directions[1]);
(tng) (tng)Ant ant11 = new Ant(positions[2], directions[2]);
(tng) (tng)Ant ant17 = new Ant(positions[3], directions[3]);
(tng) (tng)Ant ant23 = new Ant(positions[4], directions[4]);
(tng) (tng)return new Ant[] { ant3, ant7, ant11, ant17, ant23 };
(tng)}
(tng)/**
(tng) * 判断是否所有的Ant都已l走Z(jin)木杆,也就是设|退出条?br /> (tng) *
(tng) * @param antArray
(tng) * @return
(tng) */
(tng)public static boolean isAllOut(Ant[] antArray) {
(tng) (tng)for (Ant ant : antArray) {
(tng) (tng) (tng)if (ant.isOut() == false) {
(tng) (tng) (tng) (tng)return false;
(tng) (tng) (tng)}
(tng) (tng)}
(tng) (tng)return true;
(tng)}
}
(tng)