2. 时间日期(现在几点? Do you have the time?)

原文链接:http://talk.oralpractice.com/article_13674.html

- What day is it today?

- It's Friday.

今天是星期几?

(今天)星期五。

 

- I'm going to America.

- Any idea when you'll leave?

- I hope before the end of the month.

- Let me know when you find out.

我打算去美国。

你知道你何时要离开吗?

我希望月底前走。

你确定时间后告诉我。

 

-Do you have the time?

-It's ten thirty.

现在几点?

十点半。

 

展开阅读全文

How Long Do You Have to Draw

10-28

Problem DescriptionnThere are two horizontal lines on the XoY plane. One is y1 = a, the other is y2 = b(a < b). On line y1, there are N points from left to right, the x-coordinate of which are x = c1, c2, ... , cN (c1 < c2 < ... < cN) respectively. And there are also M points on line y2 from left to right. The x-coordinate of the M points are x = d1, d2, ... dM (d1 < d2 < ... < dM) respectively.nNow you can draw segments between the points on y1 and y2 by some segments. Each segment should exactly connect one point on y1 with one point on y2.nThe segments cannot cross with each other. By doing so, these segments, along with y1 and y2, can form some triangles, which have positive areas and have no segments inside them.nThe problem is, to get as much triangles as possible, what is the minimum sum of the length of these segments you draw?n nnInputnThe first line has a number T (T <= 20) , indicating the number of test cases.nFor each test case, first line has two numbers a and b (0 <= a, b <= 104), which is the position of y1 and y2.nThe second line has two numbers N and M (1 <= N, M <= 105), which is the number of points on y1 and y2.nThe third line has N numbers c1, c2, .... , cN(0 <= ci < ci+1 <= 106), which is the x-coordinate of the N points on line y1.nThe fourth line has M numbers d1, d2, ... , dM(0 <= di < di+1 <= 106), which is the x-coordinate of the M points on line y2.n nnOutputnFor test case X, output "Case #X: " first, then output one number, rounded to 0.01, as the minimum total length of the segments you draw.n nnSample Inputn1n0 1n2 3n1 3n0 2 4n nnSample OutputnCase #1: 5.66 问答

What do you do for Singleton?

06-20

I see many programmers do singleton like:rnclass Singleton implements Irnstatic Singleton _inst = null;rnstatic I getInstance()if(_inst==null)_inst = new Singleton();return _instrnrnrnWell, it works.rnBut, think hard. why wouldn't you dornclass Singleton implements Irnstatic final Singleton _inst=new Singleton();rnstatic I getInstance()return _inst;rnrn?rnMuch simpler, right?rnrnMany programmers would say: "the first one is lazy initialization. So should be better."rnrnReally? by using the first one, what do you save? a Singleton object, right? rnrnBut how big is that? From my experience, a singleton is normally quite small. For a small object with 10-20 bytes, is it worthy the extra code "if(......)"? rnthe extra code will not use memory space anyway? rnand it makes your code unnecessarily more complicated.rnrndoes it worth the extra runtime overhead to check the reference everytime the getInstance() is called?rnrnAlso, programmer often ignore the thread-safety of the first approach.rnrnWhat if there are threads calling getInstance() concurrently? Do you need to place "synchronized" on your getInstance()? rnAnd if you place "synchronized", you may also feel uncomfortable because even for non-concurrent usage of your Singleton, you are also paying the locking overhead for each call against getInstance(). Painful!rnrnOf course, for big object, the lazy intialization is certainly useful. But, there's still another approach you may want to consider if the memory space for this big object is really something.rnrnthink about this: if every user of the singleton has finished using this singleton, do I still need to hold this object? Can I reclaim the memory space for it?rnrnThen, you may want to use WeakReference to further optimize it.rnrnfor example:rnrnclass Singleton implements Irnstatic WeakReference _inst = null;rnstatic I getInstance()rnrnif(_inst==null || _inst.get()==null)rn _inst = new WeakReference(new Singleton());rnreturn (I)_inst.get();rnrnrnrnIt works? rnWell, looks like. But no!rn(I give this wrong demo to show that sometimes introducing more code is probably introducing more chances to get bug.)rnrnYou may just return "null" sometimes. This is because of the WeakReference! It does not guarantee to hold the new Singleton instance for you.rnrnSo, a possible version would be:rnclass Singleton implements Irn static WeakReference _inst = null;rn rn static synchronized I getInstance()rn rn I ret;rn if(_inst==null)rn ret = new Singleton();rn _inst = new WeakReference(ret);rn rn elsern rn ret = (I)_inst.get();rn if(ret == null)rn ret = new Singleton();rn _inst = new WeakReference(ret);rn rn rn return ret;rn rnrnquite something to do!rn 论坛

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in

12-01

<%! ResultSet rs=null;%>rnrn <%rn int type=-1;//定义一个标志变量rn tryrn type=Integer.parseInt(request.getParameter("do"));//取得参数do的值,这个值决定了用户对数据库的操作类型rn catch(Exception eee)rn type=0;//如果没有这个参数或者其他原因发生异常,则默认为0rn rn tryrn Statement st=getStatement();//取得Statement对象,getStatement()函数将在步骤8中定义rn String name=request.getParameter("name");//取得参数name的值rnswitch(type)rn//如果为0,表示用户没有操作,则把所有数据库中的所有记录提取出来rn case 0:rn rs=st.executeQuery("select * from friends");//查询全部rn break;rn//如果为1,表示用户执行的是查询操作,则把符合要求的结果提取出来rn case 1:rn name=new String(name.getBytes("ISO-8859-1"));//改变字符编码,否则会出现中文乱码问题rn rs=st.executeQuery("select * from friends where name like '%"+name+"%'");//特定查询rn break;rn //如果为2,表示用户执行的是删除操作,则执行删除后再把控制权转向本页面rn case 2:rn name=new String(name.getBytes("ISO-8859-1"));rn st.executeUpdate("delete from friends where name ='"+name+"'");//删除rn response.sendRedirect("db1.jsp");rn break;rn//如果为3,表示用户执行的是插入操作,则执行插入后再把控制权转向本页面rncase 3:rn name=new String(name.getBytes("ISO-8859-1"));rn String tel=request.getParameter("tel");rn st.executeUpdate("insert into friends values('0','"+name+"','"+tel+"’)");//添加rn response.sendRedirect("db1.jsp");rn break;rn//如果为4,表示用户执行的是修改操作,则执行插入后再把控制权转向本页面rnrncase 4:rn name=new String(name.getBytes("ISO-8859-1"));rn tel=request.getParameter("tel");rn String sql="update friends set tel=’"+tel+"’ where name=’"+name+"'";rn st.executeUpdate(sql);//修改rn response.sendRedirect("db1.jsp");rn break;rnrn%>rnrnsql语句哪错了啊,大神求指教啊 论坛

This is bug, do you think so?

07-20

本帖要说的是在《c专家编程》一个程序里的bug(至于这个是不是就看下大家的看法,不过我觉得挺明显的,要不就是我班门弄斧了,哈哈),还有就是想给大家看下这段程序,这是一个关于解析c语言声明的程序。没放假前的一段时间,我在论坛里看到了不少人在讨论这个声明的问题,lz也给出了分析声明的方法,至于要不要用那些复杂的声明我就不发表意见了(因为我也没有什么经验),但我们总得看明白这些声明啊。我在那个时候刚把《c专家编程》这书看完了,相信看过这书的同学都知道,书里就有一章是关于声明的,里面还有两个用来解析声明的程序(另一个是在后面的章节用有限状态机实现的)。rn 其实要理解声明只要知道一些在声明里的优先级就行,饿...就不扯这个了,进入正题。这里准备说的程序是那个用有限状态机实现的那个(其实这个bug在第一个解析声明的程序也有,不过我觉得这个好玩些,也可以给一些不知道FSM的同学介绍一下这个概念)。rn 为了让大家都可以更好的理解这个程序,我先引用一下书里关于FSM(finite state machine)有限状态机的描述:有限状态机是一个数学概念,如果把它运用于程序中,可以发挥很大的作用。它是一种协议,用于有限数量的子程序(“状态”)的发展变化。每一个子程序进行一些处理并选择下一种状态(通常取决于下一段的输入)。有限状态机可以用于程序的控制结构。它的基本思路是用一张表保存所有可能的状态,并列出进入每个状态时可能执行的所有动作,其中最后一个动作就是计算(通常在当前状态和下一个输入字符的基础上,另外再经过一次表查询)下一个应该进入的状态。你从一个“初始状态”开始。在这过程中,翻译表可能会告诉你进入了一个错误的状态,表示一个预期之外的或错误的输入。你不停的在各种状态间进行转换,直到到达结束状态。rn 饿...扯了这么多,就想给不知道这个的人介绍一下这个概念。说回来,要用有限状态机实现,那就要有一个适合的状态机啊(当然书里都准备好了的),下面就给大家看一个神奇的玩意(好吧,我知道很多人都看过了,不过我还是要把它弄出来):rnrn[img=http://img.my.csdn.net/uploads/201207/19/1342657921_8467.png][/img]rn[img=http://img.my.csdn.net/uploads/201207/19/1342657936_1445.png][/img]rn由于图大的问题,就分成了两部分,希望大家谅解!rn 这是一个可以用来分析c语言声明的环,只要按照它的步骤来就可以解析声明,它的原理就是基于声明的优先级:1.括号里的部分 2.后缀符号[]或() 3.前缀符号* ,和三个规定:1.函数不可以返回函数 2.函数的参数不可以是函数 3.数组的类型不可以是函数 (可以用指针来实现)。rn再引用书里的一段描述:分析环是一个简单的状态机,它的绝大多数状态转换都是按连续的顺序进行的,与输入无关,这意味著不需要建立一个转换表用于匹配状态/输入以获得下一个状态。你可以用一个简单的变量(类型为函数指针)。在每种状态下,需要做的事情之一就是给下个一个状态赋值。在主循环中,程序将调用指针所指向的函数,并循环往复,直到结束函数被调用或遇到一个错误的状态。rn 好吧,终于把这些扯完了,我们可以这个分析环的步骤2,3,4,5看作是一种状态,6是结束状态。贴代码了,bug我在下面说。rn[code=C/C++]rn// 用//是本人的注释rnrn#include rn#include rn#include rn#define MAXTOKENS 100rn#define MAXTOKENLEN 64rnenum type_tag IDENTIFIER, QUALIFIER, TYPE ;rnstruct tokenrnchar type;rnchar string[MAXTOKENLEN];rn;rnint top = -1;rnrn//int t_num = 0; //本人的修改,可先不管rnrn/*在第一个标识符(identifier)前保存所有的标识(token)*/rnstruct token stack[MAXTOKENS];rn/*保存刚读入的标记*/rnstruct token this;rn#define pop stack[top--]rn#define push(s) stack[++top]=srnenum type_tag classify_string(void)rn/*判断标识符的类型*/rnrn char *s = this.string;rn if(!strcmp(s,"const"))rn strcpy(s,"read-only");rn return QUALIFIER;rn rn if(!strcmp(s,"volatile")) return QUALIFIER;rn if(!strcmp(s,"void")) return TYPE;rn if(!strcmp(s,"char")) return TYPE;rn if(!strcmp(s,"signed")) return TYPE;rn if(!strcmp(s,"unsigned")) return TYPE;rn if(!strcmp(s,"short")) return TYPE;rn if(!strcmp(s,"int")) return TYPE;rn if(!strcmp(s,"long")) return TYPE;rn if(!strcmp(s,"double")) return TYPE;rn if(!strcmp(s,"struct")) return TYPE;rn if(!strcmp(s,"union")) return TYPE;rn if(!strcmp(s,"enum")) return TYPE;rn return IDENTIFIER;rnrnrnvoid gettoken(void)rn/*读入下一个标记,保存在“this”中*/rn char *p = this.string;rn /*略过所有的空白字符*/rn while ((*p = getchar()) == ' ');rn if(isalnum(*p))rn /*在标识符中读入A-Z,1-9字符*/rn while (isalnum(*++p = getchar()));rn ungetc(*p,stdin);rn *p = '\0';rn this.type = classify_string();rn return;rn rn this.string[1] = '\0';rn this.type = *p;rn return;rnrnrnvoid initialize(), get_array(), get_params(), get_lparen(), get_ptr_part(), get_type();//各种状态rnvoid (*nextstate)(void) = initialize;rnrnint main()rn/*用有限状态机实现的cdecl*/rnrn/*在不同的状态间转换,直到指针为NULL*/rn while(1)rn while(nextstate != NULL) (*nextstate)();rn nextstate = initialize;rn fflush(stdin);rn rn return 0;rnrnrnvoid initialize()rnrn gettoken();rn while (this.type != IDENTIFIER)rn push(this);rn gettoken();rn rn printf("%s is ", this.string);rn gettoken();rn nextstate = get_array;rnrnrnvoid get_array() //状态1,也就是分析环里的步骤2rnrn nextstate = get_params;rn while (this.type == '[')rn printf("array ");rn gettoken();/*一个数字或']'*/rn if(isdigit(this.string[0]))rn printf("0..%d ",atoi(this.string)-1);rn gettoken();/*读取']'*/rn rn gettoken();/*在']'之后读取*/rn printf("of ");rn nextstate = get_lparen;rn rnrnrn//状态2,也就是分析环里的步骤3。bug就在这个函数里,大家可以先看下rn//这个程序是不对函数里的参数进行分析的rnvoid get_params()rnrn nextstate = get_lparen;rn if(this.type == '(')rn while(this.type != ')')rn gettoken();rn rn gettoken();rn printf("function returning ");rn rnrnrn/*rnvoid get_params() //这是我的修改,为了不影响代码的可读性,bug我在后面再分析了rnrn nextstate = get_lparen;rn if(this.type == '(')rn t_num++;rn while (t_num != 0)rn gettoken();rn if(this.type == '(') t_num++;rn else if(this.type == ')') t_num--;rn rn gettoken();rn printf("function returning ");rn rnrn*/rnrnvoid get_lparen() //状态3,也就是分析环里的步骤4rnrn nextstate = get_ptr_part;rn if(top >= 0)rn if(stack[top].type == '(')rn pop;rn gettoken();/*在'('之后读取*/rn nextstate = get_array;rn rn rnrnrnvoid get_ptr_part() //状态4,也就是分析环里的步骤5rnrn nextstate = get_type;rn if(stack[top].type == '*')rn printf("pointer to ");rn pop;rn nextstate = get_lparen;rn else if (stack[top].type == QUALIFIER)rn printf("%s ",pop.string);rn nextstate = get_lparen;rn rnrnrnvoid get_type() //结束状态rnrn nextstate = NULL;rn /*处理在读入标识符之前被放在堆栈的所有标记*/rn while (top >= 0)rn printf("%s ",pop.string);rn rn printf("\n");rnrnrn/*******************************************************************************************rn对bug的分析:rnwhile(this.type != ')')rn gettoken();rn出现问题的就是上面这一个循环语句,它这里只判断了是不是找到了右括号,但作者可能没想到这个rn右括号可以不是匹配函数左括号的右括号,举个例子:int foo(int(*p)[10]),大家看这个就知道了这rn个循环语句会在把数组指针括起来的右括号就终止了,但实际上它应该找到的是最右边那个函数的右rn括号,就这bug产生了,后面的声明部分可能会被抛弃或错用。由此想到那个环对步骤3“如何阅读”的rn描述:“到右括号为止的内容”也应该说成是和前面左括号匹配的右括号才对。rn 大家可以测试一下这个声明,信号函数:void (*signal(int sig, void(*func)))(int);rn 或者我随便写的一个: int (*foo(int(*p)(void)))[10];rn 至于我修改就是加了一个计数的变量,这样就可以准确找到对应的右括号了,当然可能会有更好rn的方法,哈哈。rn 我写这个也不是贬低这本书,我知道我也没这个能力,这个只是我的一个发现,发上来和大家分rn享一下而已。还有不得不说的就是《c专家编程》的确是一本好书,c语言进阶必备啊。。。哈哈!rn*********************************************************************************************/rnrn[/code]rn要是有错,请各位大大们指出哈。。。。。 论坛

提示:You have an error in your SQL syntax

11-25

[code=Java]rn/**rn * 添加操作记录rn */rn public Statistics saveStatistics(Integer status, List batchProcess) rn Statistics stat = new Statistics();rn Article article = getArticle();rn User user = getLoggedOnUser();rn stat.setType(2);//文章rn stat.setUserId(user.getId());rn Date date = new Date();rn SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");rn dateformat.format(date);rn stat.setStatisticsTime(date);rn if(status != null) rn switch (status) rn case 0://重申rn stat.setDescribe(user.getUsername() + "重申了"+article.getCommUser().getUserName()+"发表的文章" + article.getTitle());rn break;rn case 1://通过rn stat.setDescribe(user.getUsername() +article.getCommUser().getUserName()); //article.getTitle()rn break;rn case 2://屏蔽rn stat.setDescribe(user.getUsername() + "屏蔽了"+article.getCommUser().getUserName()+"发表的文章" + article.getTitle());rn break;rn case 3://删除rn stat.setDescribe(user.getUsername() + "删除了"+article.getCommUser().getUserName()+"发表的文章" + article.getTitle());rn break;rn rn rn if(batchProcess != null) rn stat.setDescribe(user.getUsername() + "处理了"+article.getCommUser().getUserName()+"发表的文章" + article.getTitle());rn rn [color=#FF0000statisticsManager.save(stat);][/color] //这里执行走不了rn this.saveStatisticsDesc(stat,article);rn return stat;rn rn[/code]rnrn错误信息如下:rnrnHibernate: insert into ibokee_statistics (describe, statisticsTime, type, userId) values (?, ?, ?, ?)rnWARN ["http-bio-8080"-exec-9] JDBCExceptionReporter.logExceptions(100) | SQL Error: 1064, SQLState: 42000rnERROR ["http-bio-8080"-exec-9] JDBCExceptionReporter.logExceptions(101) | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe, statisticsTime, type, userId) values ('adminzhender', '2011-11-25 10:4' at line 1rn 论坛

没有更多推荐了,返回首页