Sql優(yōu)化是一項(xiàng)復(fù)雜的工作,以下的一些基本原則是本人看書時所記錄下來的,很明確且沒什么廢話:
1. 索引的使用:
(1).當(dāng)插入的數(shù)據(jù)為數(shù)據(jù)表中的記錄數(shù)量的10%以上,首先需要刪除該表的索引來提高數(shù)據(jù)的插入效率,當(dāng)數(shù)據(jù)插入后,再建立索引。
(2).避免在索引列上使用函數(shù)或計(jì)算,在where子句中,如果索引是函數(shù)的一部分,優(yōu)化器將不再使用索引而使用全表掃描。如:
低效:select * from dept where sal*12 >2500;
高效:select * from dept where sal>2500/12;
(3).避免在索引列上使用not和 “!=”,索引只能告訴什么存在于表中,而不能告訴什么不存在于表中,當(dāng)數(shù)據(jù)庫遇到not 和 “!=”時,就會停止使用索引而去執(zhí)行全表掃描。
(4).索引列上>=代替>
低效:select * from emp where deptno > 3
高效:select * from emp where deptno >=4
兩者的區(qū)別在于,前者dbms將直接跳到第一個deptno等于4的記錄,而后者將首先定位到deptno等于3的記錄并且向前掃描到第一個deptno大于3的。
(5).非要對一個使用函數(shù)的列啟用索引,基于函數(shù)的索引是一個較好的方案。
2. 游標(biāo)的使用:
當(dāng)在海量的數(shù)據(jù)表中進(jìn)行數(shù)據(jù)的刪除、更新、插入操作時,用游標(biāo)處理的效率是最慢的,但是游標(biāo)又是必不可少的,所以正確使用游標(biāo)十分重要:
(1). 在數(shù)據(jù)抽取的源表中使用時間戳,這樣每天的維表數(shù)據(jù)維護(hù)只針對更新日期為最新時間的數(shù)據(jù)來進(jìn)行,大大減少需要維護(hù)的數(shù)據(jù)記錄數(shù)。
(2). 在insert和update維表時都加上一個條件來過濾維表中已經(jīng)存在的記錄,例如:
insert into dim_customer select * from ods_customer where ods_customer.code not exists (dim_customer.code)
ods_customer為數(shù)據(jù)源表。dim_customer為維表。
(3). 使用顯式的游標(biāo),因?yàn)殡[式的游標(biāo)將會執(zhí)行兩次操作,第一次檢索記錄,第二次檢查too many rows這個exception,而顯式游標(biāo)不執(zhí)行第二次操作。
3. 據(jù)抽取和上載時的sql優(yōu)化:
(1). Where 子句中的連接順序:
oracle采用自下而上的順序解析where子句,根據(jù)這個原理,表之間的連接必須寫在其他where條件之前,那些可以過濾掉大量記錄的條件必須寫在where子句的末尾。如:
低效:select * from emp e where sal>5000 and job = ‘manager’ and 25<(select count (*) from emp where mgr=e.empno);
高效:select * from emp e where 25<(select count(*) from emp where mgr=e.empno) and sal>5000 and job=’manager’;
(2). 刪除全表時,用truncate 替代 delete,同時注意truncate只能在刪除全表時適用,因?yàn)?/SPAN>truncate是ddl而不是dml。
(3). 盡量多使用commit
只要有可能就在程序中對每個delete,insert,update操作盡量多使用commit,這樣系統(tǒng)性能會因?yàn)?/SPAN>commit所釋放的資源而大大提高。
(4). 用exists替代in ,可以提高查詢的效率。
(5). 用not exists 替代 not in
(6). 優(yōu)化group by
提高group by語句的效率,可以將不需要的記錄在group by之前過濾掉。如:
低效:select job, avg(sal) from emp group by job having job = ‘president’ or job=’manager’;
高效: select job, avg(sal) from emp having job=’president’ or job=’manager’ group by job;
(7). 有條件的使用union-all 替代 union:這樣做排序就不必要了,效率會提高3到5倍。
(8). 分離表和索引
總是將你的表和索引建立在不同的表空間內(nèi),決不要將不屬于oracle內(nèi)部系統(tǒng)的對象存放到system表空間內(nèi)。同時確保數(shù)據(jù)表空間和索引表空間置于不同的硬盤控制卡控制的硬盤上。
轉(zhuǎn)自:
http://blog.csdn.net/eigo/archive/2006/03/02/614157.aspx
posted @
2006-03-04 20:34 風(fēng)蕭蕭 閱讀(464) |
評論 (0) |
編輯 收藏
/*
建表:
dept:
deptno(primary key),dname,loc
emp:
empno(primary key),ename,job,mgr,sal,deptno
*/
1 列出emp表中各部門的部門號,最高工資,最低工資
select max(sal) as 最高工資,min(sal) as 最低工資,deptno from emp group by deptno;
2 列出emp表中各部門job為'CLERK'的員工的最低工資,最高工資
select max(sal) as 最高工資,min(sal) as 最低工資,deptno as 部門號 from emp where job = 'CLERK' group by deptno;
3 對于emp中最低工資小于1000的部門,列出job為'CLERK'的員工的部門號,最低工資,最高工資
select max(sal) as 最高工資,min(sal) as 最低工資,deptno as 部門號 from emp as b
where job='CLERK' and 1000>(select min(sal) from emp as a where a.deptno=b.deptno) group by b.deptno
4 根據(jù)部門號由高而低,工資有低而高列出每個員工的姓名,部門號,工資
select deptno as 部門號,ename as 姓名,sal as 工資 from emp order by deptno desc,sal asc
5 寫出對上題的另一解決方法
(請補(bǔ)充)
6 列出'張三'所在部門中每個員工的姓名與部門號
select ename,deptno from emp where deptno = (select deptno from emp where ename = '張三')
7 列出每個員工的姓名,工作,部門號,部門名
select ename,job,emp.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno
8 列出emp中工作為'CLERK'的員工的姓名,工作,部門號,部門名
select ename,job,dept.deptno,dname from emp,dept where dept.deptno=emp.deptno and job='CLERK'
9 對于emp中有管理者的員工,列出姓名,管理者姓名(管理者外鍵為mgr)
select a.ename as 姓名,b.ename as 管理者 from emp as a,emp as b where a.mgr is not null and a.mgr=b.empno
10 對于dept表中,列出所有部門名,部門號,同時列出各部門工作為'CLERK'的員工名與工作
select dname as 部門名,dept.deptno as 部門號,ename as 員工名,job as 工作 from dept,emp
where dept.deptno *= emp.deptno and job = 'CLERK'
11 對于工資高于本部門平均水平的員工,列出部門號,姓名,工資,按部門號排序
select a.deptno as 部門號,a.ename as 姓名,a.sal as 工資 from emp as a
where a.sal>(select avg(sal) from emp as b where a.deptno=b.deptno) order by a.deptno
12 對于emp,列出各個部門中平均工資高于本部門平均水平的員工數(shù)和部門號,按部門號排序
select count(a.sal) as 員工數(shù),a.deptno as 部門號 from emp as a
where a.sal>(select avg(sal) from emp as b where a.deptno=b.deptno) group by a.deptno order by a.deptno
13 對于emp中工資高于本部門平均水平,人數(shù)多與1人的,列出部門號,人數(shù),按部門號排序
select count(a.empno) as 員工數(shù),a.deptno as 部門號,avg(sal) as 平均工資 from emp as a
where (select count(c.empno) from emp as c where c.deptno=a.deptno and c.sal>(select avg(sal) from emp as b where c.deptno=b.deptno))>1
group by a.deptno order by a.deptno
14 對于emp中低于自己工資至少5人的員工,列出其部門號,姓名,工資,以及工資少于自己的人數(shù)
select a.deptno,a.ename,a.sal,(select count(b.ename) from emp as b where b.sal<a.sal) as 人數(shù) from emp as a
where (select count(b.ename) from emp as b where b.sal<a.sal)>5
轉(zhuǎn)自:http://blog.csdn.net/woolceo/archive/2006/03/02/614094.aspx
posted @
2006-03-04 20:31 風(fēng)蕭蕭 閱讀(2043) |
評論 (1) |
編輯 收藏
在開發(fā)部署PORTAL項(xiàng)目時,遇到異常:
Exception:weblogic.management.ApplicationException: prepare failed for content_repo.jar Module: content_repo.jar Error: Exception preparing module: EJBModule(content_repo.jar,status=NEW) Unable to deploy EJB: content_repo.jar from content_repo.jar: Class not found: com.bea.content.repo.i18n.RepoExceptionTextFormatter java.lang.NoClassDefFoundError: Class not found: com.bea.content.repo.i18n.RepoExceptionTextFormatter at weblogic.ejb20.compliance.EJBComplianceChecker.check([Ljava.lang.Object;)V(EJBComplianceChecker.java:287)
我在weblogic81 sp3的doc中沒有發(fā)現(xiàn)com.bea.content.repo.i18n這個package.
重新安裝了weblogic sp4,就不再出現(xiàn)這個錯誤了。
posted @
2006-02-15 23:14 風(fēng)蕭蕭 閱讀(618) |
評論 (0) |
編輯 收藏
Problem Statement |
|
When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line.
You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end. |
Definition |
|
Class: |
CursorPosition |
Method: |
getPosition |
Parameters: |
String, int |
Returns: |
int |
Method signature: |
int getPosition(String keystrokes, int N) |
(be sure your method is public) | |
|
|
Constraints |
- |
keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive. |
- |
N will be between 1 and 100, inclusive. |
Examples |
0) |
|
|
|
Returns: 7 |
First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7. | | |
1) |
|
|
|
Returns: 2 |
All the right-arrows at the end ensure that we end up at the end of the line. | | |
2) |
|
|
"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL" |
10 | |
Returns: 3 |
| |
3) |
|
|
"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL" |
19 | |
Returns: 12 |
| |
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
答案:
1
2
public class CursorPosition
{
3
public int getPosition(String keystrokes, int N)
{
4
int position = 0;
5
String s = "";
6
for(int i = 0; i < keystrokes.length(); i++)
{
7
s = keystrokes.substring(i, i+1);
8
if("L".equals(s))
{
9
if(position == 0) continue;
10
position--;
11
}
12
if("R".equals(s))
{
13
if(position == N) continue;
14
position++;
15
}
16
if("H".equals(s))
{
17
position = 0;
18
}
19
if("E".equals(s))
{
20
position = N;
21
}
22
23
}
24
25
return position;
26
27
}
28
/** *//**
29
* @param args
30
*/
31
public static void main(String[] args)
{
32
CursorPosition cursorPosition = new CursorPosition();
33
int cursor = cursorPosition.getPosition("ERLLL", 10);
34
System.out.println("cursor:" + cursor);
35
}
36
37
}
38
posted @
2005-11-27 23:42 風(fēng)蕭蕭 閱讀(935) |
評論 (2) |
編輯 收藏
Problem Statement |
|
A square matrix is a grid of NxN numbers. For example, the following is a 3x3 matrix: 4 3 5
2 4 5
0 1 9 One way to represent a matrix of numbers, each of which is between 0 and 9 inclusive, is as a row-major String. To generate the String, simply concatenate all of the elements from the first row followed by the second row and so on, without any spaces. For example, the above matrix would be represented as "435245019".
You will be given a square matrix as a row-major String. Your task is to convert it into a String[], where each element represents one row of the original matrix. Element i of the String[] represents row i of the matrix. You should not include any spaces in your return. Hence, for the above String, you would return {"435","245","019"}. If the input does not represent a square matrix because the number of characters is not a perfect square, return an empty String[], {}. |
Definition |
|
Class: |
MatrixTool |
Method: |
convert |
Parameters: |
String |
Returns: |
String[] |
Method signature: |
String[] convert(String s) |
(be sure your method is public) | |
|
|
Constraints |
- |
s will contain between 1 and 50 digits, inclusive. |
Examples |
0) |
|
|
|
Returns: {"435", "245", "019" } |
| |
1) |
|
|
|
2) |
|
|
|
Returns: { } |
This input has 10 digits, and 10 is not a perfect square. | | |
3) |
|
|
"3357002966366183191503444273807479559869883303524" | |
Returns: {"3357002", "9663661", "8319150", "3444273", "8074795", "5986988", "3303524" } |
| |
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
答案:
1
public class MatrixTool
{
2
3
public String[] convert(String s)
{
4
if (s == null || s.length() == 0 || s.length() > 50)
{
5
return new String[]
{};
6
}
7
int length = s.length();
8
int n = (int)Math.sqrt(length);
9
if(n*n == length)
{
10
String[] result = new String[n];
11
for(int i = 0; i < n; i++)
{
12
result[i] = s.substring(i*n, i*n + n);
13
}
14
return result;
15
}else
{
16
return new String[]
{};
17
}
18
}
19
20
/** *//**
21
* @param args
22
*/
23
public static void main(String[] args)
{
24
MatrixTool matrix = new MatrixTool();
25
String[] result = matrix.convert("3357002966366183191503444273807479559869883303524");
26
for(int i = 0; i < result.length; i++)
{
27
System.out.println(result[i]);
28
}
29
}
30
31
}
32
posted @
2005-11-27 23:40 風(fēng)蕭蕭 閱讀(735) |
評論 (0) |
編輯 收藏
摘要: Problem Statement
A simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. T...
閱讀全文
posted @
2005-11-27 23:37 風(fēng)蕭蕭 閱讀(1157) |
評論 (0) |
編輯 收藏
經(jīng)典面試題
一、面向?qū)ο蟮娜齻€基本特征
2、方法重載和方法重寫的概念和區(qū)別
3、接口和內(nèi)部類、抽象類的特性
4、文件讀寫的基本類
**5、串行化的注意事項(xiàng)以及如何實(shí)現(xiàn)串行化
6、線程的基本概念、線程的基本狀態(tài)以及狀態(tài)之間的關(guān)系
7、線程的同步、如何實(shí)現(xiàn)線程的同步
8、幾種常用的數(shù)據(jù)結(jié)構(gòu)及內(nèi)部實(shí)現(xiàn)原理。
9、Socket通信(TCP、UDP區(qū)別及Java實(shí)現(xiàn)方式)
**10、Java的事件委托機(jī)制和垃圾回收機(jī)制
11、JDBC調(diào)用數(shù)據(jù)庫的基本步驟
**12、解析XML文件的幾種方式和區(qū)別
13、Java四種基本權(quán)限的定義
14、Java的國際化
二、JSP
1、至少要能說出7個隱含對象以及他們的區(qū)別
** 2、forward 和redirect的區(qū)別
3、JSP的常用指令
三、servlet
1、什么情況下調(diào)用doGet()和doPost()?
2、servlet的init()方法和service()方法的區(qū)別
3、servlet的生命周期
4、如何現(xiàn)實(shí)servlet的單線程模式
5、servlet的配置
6、四種會話跟蹤技術(shù)
四、EJB
**1、EJB容器提供的服務(wù)
主要提供聲明周期管理、代碼產(chǎn)生、持續(xù)性管理、安全、事務(wù)管理、鎖和并發(fā)行管理等服務(wù)。
2、EJB的角色和三個對象
EJB角色主要包括Bean開發(fā)者 應(yīng)用組裝者 部署者 系統(tǒng)管理員 EJB容器提供者 EJB服務(wù)器提供者
三個對象是Remote(Local)接口、Home(LocalHome)接口,Bean類
2、EJB的幾種類型
會話(Session)Bean ,實(shí)體(Entity)Bean 消息驅(qū)動的(Message Driven)Bean
會話Bean又可分為有狀態(tài)(Stateful)和無狀態(tài)(Stateless)兩種
實(shí)體Bean可分為Bean管理的持續(xù)性(BMP)和容器管理的持續(xù)性(CMP)兩種
3、bean 實(shí)例的生命周期
對于Stateless Session Bean、Entity Bean、Message Driven Bean一般存在緩沖池管理,而對于Entity Bean和Statefull Session Bean存在Cache管理,通常包含創(chuàng)建實(shí)例,設(shè)置上下文、創(chuàng)建EJB Object(create)、業(yè)務(wù)方法調(diào)用、remove等過程,對于存在緩沖池管理的Bean,在create之后實(shí)例并不從內(nèi)存清除,而是采用緩沖池調(diào)度機(jī)制不斷重用實(shí)例,而對于存在Cache管理的Bean則通過激活和去激活機(jī)制保持Bean的狀態(tài)并限制內(nèi)存中實(shí)例數(shù)量。
4、激活機(jī)制
以Statefull Session Bean 為例:其Cache大小決定了內(nèi)存中可以同時存在的Bean實(shí)例的數(shù)量,根據(jù)MRU或NRU算法,實(shí)例在激活和去激活狀態(tài)之間遷移,激活機(jī)制是當(dāng)客戶端調(diào)用某個EJB實(shí)例業(yè)務(wù)方法時,如果對應(yīng)EJB Object發(fā)現(xiàn)自己沒有綁定對應(yīng)的Bean實(shí)例則從其去激活Bean存儲中(通過序列化機(jī)制存儲實(shí)例)回復(fù)(激活)此實(shí)例。狀態(tài)變遷前會調(diào)用對應(yīng)的ejbActive和ejbPassivate方法。
5、remote接口和home接口主要作用
remote接口定義了業(yè)務(wù)方法,用于EJB客戶端調(diào)用業(yè)務(wù)方法
home接口是EJB工廠用于創(chuàng)建和移除查找EJB實(shí)例
6、客服端調(diào)用EJB對象的幾個基本步驟
一、 設(shè)置JNDI服務(wù)工廠以及JNDI服務(wù)地址系統(tǒng)屬性
二、 查找Home接口
三、 從Home接口調(diào)用Create方法創(chuàng)建Remote接口
四、 通過Remote接口調(diào)用其業(yè)務(wù)方法
五、數(shù)據(jù)庫
1、存儲過程的編寫
2、基本的SQL語句
六、weblogic
1、 如何給weblogic指定大小的內(nèi)存?
在啟動Weblogic的腳本中(位于所在Domian對應(yīng)服務(wù)器目錄下的startServerName),增加set MEM_ARGS=-Xms32m -Xmx200m,可以調(diào)整最小內(nèi)存為32M,最大200M
2、 如何設(shè)定的weblogic的熱啟動模式(開發(fā)模式)與產(chǎn)品發(fā)布模式?
可以在管理控制臺中修改對應(yīng)服務(wù)器的啟動模式為開發(fā)或產(chǎn)品模式之一。或者修改服務(wù)的啟動文件或者commenv文件,增加set PRODUCTION_MODE=true。
3、 如何啟動時不需輸入用戶名與密碼?
修改服務(wù)啟動文件,增加 WLS_USER和WLS_PW項(xiàng)。也可以在boot.properties文件中增加加密過的用戶名和密碼.
4、 在weblogic管理制臺中對一個應(yīng)用域(或者說是一個網(wǎng)站,Domain)進(jìn)行jms及ejb或連接池等相關(guān)信息進(jìn)行配置后,實(shí)際保存在什么文件中?
保存在此Domain的config.xml文件中,它是服務(wù)器的核心配置文件。
5、 說說weblogic中一個Domain的缺省目錄結(jié)構(gòu)?比如要將一個簡單的helloWorld.jsp放入何目錄下,然的在瀏覽器上就可打入http://主機(jī):端口號//helloword.jsp就可以看到運(yùn)行結(jié)果了? 又比如這其中用到了一個自己寫的javaBean該如何辦?
Domain目錄\服務(wù)器目錄\applications,將應(yīng)用目錄放在此目錄下將可以作為應(yīng)用訪問,如果是Web應(yīng)用,應(yīng)用目錄需要滿足Web應(yīng)用目錄要求,jsp文件可以直接放在應(yīng)用目錄中,Javabean需要放在應(yīng)用目錄的WEB-INF目錄的classes目錄中,設(shè)置服務(wù)器的缺省應(yīng)用將可以實(shí)現(xiàn)在瀏覽器上無需輸入應(yīng)用名。
6、 如何查看在weblogic中已經(jīng)發(fā)布的EJB?
可以使用管理控制臺,在它的Deployment中可以查看所有已發(fā)布的EJB
7、 如何在weblogic中進(jìn)行ssl配置與客戶端的認(rèn)證配置或說說j2ee(標(biāo)準(zhǔn))進(jìn)行ssl的配置
缺省安裝中使用DemoIdentity.jks和DemoTrust.jks KeyStore實(shí)現(xiàn)SSL,需要配置服務(wù)器使用Enable SSL,配置其端口,在產(chǎn)品模式下需要從CA獲取私有密鑰和數(shù)字證書,創(chuàng)建identity和trust keystore,裝載獲得的密鑰和數(shù)字證書。可以配置此SSL連接是單向還是雙向的。
8、在weblogic中發(fā)布ejb需涉及到哪些配置文件
不同類型的EJB涉及的配置文件不同,都涉及到的配置文件包括ejb-jar.xml,weblogic-ejb-jar.xmlCMP實(shí)體Bean一般還需要weblogic-cmp-rdbms-jar.xml
9、EJB需直接實(shí)現(xiàn)它的業(yè)務(wù)接口或Home接口嗎,請簡述理由.
遠(yuǎn)程接口和Home接口不需要直接實(shí)現(xiàn),他們的實(shí)現(xiàn)代碼是由服務(wù)器產(chǎn)生的,程序運(yùn)行中對應(yīng)實(shí)現(xiàn)類會作為對應(yīng)接口類型的實(shí)例被使用。
10、說說在weblogic中開發(fā)消息Bean時的persistent與non-persisten的差別
persistent方式的MDB可以保證消息傳遞的可靠性,也就是如果EJB容器出現(xiàn)問題而JMS服務(wù)器依然會將消息在此MDB可用的時候發(fā)送過來,而non-persistent方式的消息將被丟棄。
11、說說你所熟悉或聽說過的j2ee中的幾種常用模式?及對設(shè)計(jì)模式的一些看法
Session Facade Pattern:使用SessionBean訪問EntityBean
Message Facade Pattern:實(shí)現(xiàn)異步調(diào)用
EJB Command Pattern:使用Command JavaBeans取代SessionBean,實(shí)現(xiàn)輕量級訪問
Data Transfer Object Factory:通過DTO Factory簡化EntityBean數(shù)據(jù)提供特性
Generic Attribute Access:通過AttibuteAccess接口簡化EntityBean數(shù)據(jù)提供特性
Business Interface:通過遠(yuǎn)程(本地)接口和Bean類實(shí)現(xiàn)相同接口規(guī)范業(yè)務(wù)邏輯一致性
EJB架構(gòu)的設(shè)計(jì)好壞將直接影響系統(tǒng)的性能、可擴(kuò)展性、可維護(hù)性、組件可重用性及開發(fā)效率。項(xiàng)目越復(fù)雜,項(xiàng)目隊(duì)伍越龐大則越能體現(xiàn)良好設(shè)計(jì)的重要性。
轉(zhuǎn)載自:http://blog.csdn.net/laou2008/archive/2005/11/15/529519.aspx
西門子的一道筆試題
設(shè)計(jì)一個函數(shù),形式如: int func(unsigned int),要求求出不大于輸入?yún)?shù)的最大的素?cái)?shù),比如輸入12,返回11。
轉(zhuǎn)載自:http://community.csdn.net/Expert/topic/4368/4368551.xml?temp=.4177057
微軟MSN在南大的筆試題
羅馬數(shù)字共有七個,即
I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。
按照下面三條規(guī)則可以表示任意正整數(shù)。
重復(fù)數(shù)次:一個羅馬數(shù)字重復(fù)幾次,就表示這個數(shù)的幾倍。
右加左減:在一個較大的羅馬數(shù)字的右邊記上一個較小的羅馬數(shù)字,
表示大數(shù)字加小數(shù)字。在一個較大的數(shù)字的左邊記上一個較小的羅
馬數(shù)字,表示大數(shù)字減小數(shù)字。但是,左減不能跨越等級。
比如,99不可以用IC表示,用XCIX表示
基本數(shù)字Ⅰ、X 、C 中的任何一個,自身連用構(gòu)成數(shù)目,或者放在大數(shù)的右邊連用構(gòu)成數(shù)目,都不能超過三個,比如40不能用XXXX,而用XL表示
設(shè)計(jì)一個函數(shù),將100(包括100)以內(nèi)的整數(shù)轉(zhuǎn)換成羅馬數(shù)字,超過100不考慮
int itor(int n,char* buf,int bufLength)
其中,n是要轉(zhuǎn)換的整數(shù),buf是要輸出的字符串,bufLength是buf的字符長度
成功,返回0,否則,返回 -1;
比如:
char buf[256];
result = itor(n,buf,sizeof(buf));
when n = 28; result = 0, 輸出"XXVIII";
when n = 72; result = 0, 輸出"LXXII";
轉(zhuǎn)載自:http://community.csdn.net/Expert/topic/4386/4386877.xml?temp=.411175
posted @
2005-11-16 09:59 風(fēng)蕭蕭 閱讀(1061) |
評論 (0) |
編輯 收藏