1,写出连接池的伪代码实现,要求实现getConnection()方法,并且有最大最小连接数。2,写一个方法求一个字符串里面的每个字符和它的个数,如 “aabbcccerrrajjjb”;3,狗叫了,主人醒了,小偷吓跑了,主人和小偷是被动的并且还有可扩展性,用什么样的设计模式并写出代码。4,一个队列最多的元素为M,怎么用表达式表达出来,是个选择题,选项不记得了。5,用非递归方法实现下列函数的值。
f(1)=1
f(2)=2
f(n)=f(n-1)+f(n-2)
f(1)=1
f(2)=2
f(n)=f(n-1)+f(n-2)
* 创建 Oracle 数据库的连接
* @param url String
* @param user String
* @param password String
* @throws ClassNotFoundException
* @throws SQLException
* @return Connection
*/
public static Connection getOracleConnection(String url,String user,String password) throws ClassNotFoundException,SQLException {
Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,user,password);
return con;
}
最大最小连接数,web服务器都有的~~!直接配置即可!
非递归的菲波那契(Fibonacci)数列算法相信递归的 菲波那契(Fibonacci) 数列 算法程序大家都会写,在计算比较小的数字的时候没什么问题,但是随着数字增大,运算复杂度则成2的指数级(相乘)倍数递增。不信的话,大家可以写个递归的算法,算一下 Fibonacci(50)看看要多久,再看看Fibonacci(40),Fibonacci(30),差别大的一P。呵呵,,,,
闲极无聊,这里弄个基于加法的算法,复杂度为O(n)
//优化后的Fibonacci数列算法,非递归!!!!long Fibonacci(int n){long coef1=1,coef2=1,coef_tmp=0;int j=0;if(n<3)return 1;j=n-1;while(j>2){coef_tmp=coef1;coef1=coef1+coef2;coef2=coef_tmp;j--;}return coef1+coef2;}//递归算法,参考long Recur_Fibonacci(int n){if (n>1) {return Recur_Fibonacci(n-1)+Recur_Fibonacci(n-2);}if (n==1) {return 1;}if (n==0) {return 0;}return 0;} //// 试了一下,效果还是不错的。
http://calmet.spaces.live.com/blog/cns!3C0C35EFD6F5F063!250.entry
//普通的连接类
public class ConnectionManager {
/**
* 得到数据库连接
*
* @return 数据库连接对象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getCon() {
try {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(driver);
return DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=zf", "sa", "sa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭所有连接
* @param con 数据库连接
* @param ps
* @param rs
*/
public static void closeAll(Connection con, PreparedStatement ps,
ResultSet rs) { try { if (rs != null) {
rs.close(); }
if (ps != null) {
ps.close();
} if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//代理的连接对象
public class ConnectionAdvice implements InvocationHandler {
//获取真实的对象
private Connection realCon;
public ConnectionAdvice(Connection realCon) {
// TODO Auto-generated constructor stub
this.realCon=realCon;
} public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
//获取方法名
String methodName=method.getName();
//判断是否为关闭的方法
if("close".equals(methodName)){
System.out.println("代理该方法");
//将代理对象放到池子中
ConnectionPool.restore((Connection)proxy);
return null;
}else{
//如果不是则用原有连接执行原有的方法
return method.invoke(realCon, args);
}
}}//代理工厂通过真实对象创建代理对象
public class ProxyFactory {
//通过真实的对象获取代理对象
public static Connection create(Connection realCon){
//通过代理类Proxy
Connection proxy=(Connection) Proxy.newProxyInstance(
//类加载器
realCon.getClass().getClassLoader(),
//获取它的所有接口
realCon.getClass().getInterfaces(),
//调用处理器
new ConnectionAdvice(realCon));
return proxy;
}
}//连接池
public class ConnectionPool {
//用LinkedList集合存取连接
private static List<Connection> conList=new LinkedList<Connection>();
//最大连接数
private static final int Total=10;
private static int curNum=5;//最小连接数
static{
for (int i = 0; i < 5; i++) {
//获取代理的连接
Connection proxy=ProxyFactory.create(ConnectionManager.getCon());
//添加到集合中
conList.add(proxy);
}
}
//从池子中获取连接的方法
public static synchronized Connection getCon(){
Connection proxyCon=null;
//如果集合中有连接
if(conList.size()>0){
if(conList.size()<=5){
//从集合中获取一个连接
proxyCon=conList.get(0);
//从集合中删除该连接
conList.remove(0);
return proxyCon;
}else{
curNum-=5;
for (int i = 0; i < 5; i++) {
conList.remove(0);
}
proxyCon=conList.get(0);
//从集合中删除该连接
conList.remove(0);
return proxyCon;
}
}else{
//如果集合中没有连接了
//当前总数小于最大连接数,则增加5
if(curNum<Total){
curNum+=5;
for (int i = 0; i < 5; i++) {
//获取代理的连接
Connection proxy=ProxyFactory.create(ConnectionManager.getCon());
//添加到集合中
conList.add(proxy);
}
proxyCon=conList.get(0);
//从集合中删除该连接
conList.remove(0);
return proxyCon;
}else{
//如果等于最大连接数,则抛出异常让用户等待
try {
throw new Exception("连接已经满,请等待!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
//放回到池子中!
public static synchronized void restore(Connection proxyCon){
conList.add(proxyCon);
}
}2.
//写一个方法求一个字符串里面的每个字符和它的个数
public void show(String s){
if(s!=null){
for (int i = 0; i < s.length(); i++) {
System.out.println(s.substring(i, i+1));
}
System.out.println("它的个数为:"+s.length());
}else{
System.out.println("为NULL");
}
}3.不会4.没看明白5.
/**
* 用非递归方法实现下列函数的值。
f(1)=1
f(2)=2
f(n)=f(n-1)+f(n-2)
*/
public int fun(int n){
if(n<=0)
return -1;//表示异常
if(n==1)
return 1;
if(n==2)
return 2;
int[] num=new int[n];
num[0]=1;
num[1]=2;
//从第三个数开始计算
for (int i = 2; i < num.length; i++) {
num[i]=num[i-1]+num[i-2];
}
return num[n-1];
}
public class DataSourceFactory {
private static BasicDataSource bds;
public static BasicDataSource getDataSource(){
//create a basic data source;
if(bds == null){
bds = new BasicDataSource();
bds.setDriverClassName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
bds.setUrl("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=carSaleManager;SelectMethod=Cursor");
bds.setUsername("sa");
bds.setPassword("123");
bds.setMaxActive(5);
}
return bds;
}
}
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;public class F {
public static void main(String[] args) {
System.out.println("你随意输入:");
Scanner scanner = new Scanner(System.in);
String strs = scanner.nextLine();
char[] charArr = strs.toCharArray();
// 使用TreeMap让其自动排序
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for (char c : charArr) {
Integer count = map.get(c);
count = count == null ? 1 : count + 1;
map.put(c, count);
}
Set<Character> set = map.keySet();
for (Character c : set) {
System.out.println(c + "出现的次数:" + map.get(c));
}
}
}
5public class E {
public static void main(String[] args) {
System.out.println(Fibonacci(20));
System.out.println(Recur_Fibonacci(20));
} // 非递归方法
public static long Fibonacci(int n) {
long coef1 = 1, coef2 = 2, coef_tmp = 0;
if (n < 3)
return n;
else {
while (n > 2) {
if (n == 3) {
// 什么都不做,让其根据n--自动跳出循环并返回结果。
} else {
coef_tmp = coef2; coef2 = coef1 + coef_tmp; coef1 = coef_tmp;
}
n--;
}
}
return coef1 + coef2;
} // 递归方法
public static long Recur_Fibonacci(int n) {
if (n > 2) {
return Recur_Fibonacci(n - 1) + Recur_Fibonacci(n - 2);
} else {
return n;
}
}
}
Map<Character, Integer> map = new HashMap<Character, Integer>();
int[] arr = new int[128];
for (int i = 0; i < s.length(); i++)
arr[s.charAt(i)]++;
for (int i = 0; i < arr.length; i++)
if(arr[i] != 0)
map.put((char)i, arr[i]);
return map;
}
public int deEncursion(int n) {
int a = 1;
int b = 1;
int temp = 0;
if(n == 1 || n == 2) {
temp = 1;
}else {
for(int i = 0; i < n - 2; i++) {
temp = a + b;
b = a;
a = temp;
}
}
return temp;
}
2.
for (char c : charArr) {
Integer count = map.get(c);
count = count == null ? 1 : count + 1;
map.put(c, count);
}for(char c : charArr)表示什么意思啊,for里面我还不知道有这个用法。
在有map.get(c)是获得c在字符数组里面的索引吗?那返回的count就只是c的索引值而不是字符个数吧 ,如果c在索引3那么count开始不就从3开始加了吗?
在有map.get(c)是获得c在字符数组里面的索引吗?map这块已经跟字符数组没关系了。
map.get(c)--------map.get(Object key) 这块的c是一个你要取得值的标识,根据这个标识你就可以取得相应的值。
建议lz看看API的解释
lz运行一下代码,然后去梳理!
[color=[cod//第二题来个C++版的
#include <iostream>
#include <map>
#include <pair.h>
#define my_pair pair<char,int>
using namespace std;
void findCharacterNum(string str)
{
map<char,int> result;
map<char, int>::iterator itr;
pair<map<char, int>::iterator, bool > pa;
for(unsigned int i = 0; i < str.length(); i++)
{
pa = result.insert(my_pair(str[i],1));
if(!pa.second)
{
itr = result.find(str[i]);
itr->second++;
}
}
for(itr = result.begin(); itr != result.end(); itr++)
{
cout<<itr->first<<"="<<itr->second<<endl;
}
}int main(int argc, char *argv[])
{
findCharacterNum("aaabbbcbeff");
return 0;
}
/*结果
a=3
b=4
c=1
e=1
f=2
*/
void CCOUNTDlg::OnButton(CString m_content)
{
// TODO: Add your control notification handler code here
char ch;
CString s;
CString temp;
temp=m_content;
while(temp.GetLength()!=0)
{
int len=temp.GetLength();
int count=0;
ch=temp.GetAt(0);
count=1;
if(temp.GetLength()<2)
{
count=1;
s.Format("字符串中字符%c的个数是:%d\n",ch,count);
m_result+=s;
break;
}
for(int j=1;j<len;j++)
{
if(temp.GetAt(j)==ch)
{
count++;
}
}
s.Format("字符串中字符%c的个数是:%d\n",ch,count);
m_result+=s;
temp.Remove(ch);
}
MessageBox(m_result);
}
其他的题我慢慢想,想好再回答!
希望更多的人把这样的面试题晒出来让大家做,欢迎!
void CCOUNTDlg::OnButton(CString m_content)
{
// TODO: Add your control notification handler code here
char ch;
CString s;
CString temp;
temp=m_content;
while(temp.GetLength()!=0)
{
int len=temp.GetLength();
int count=0;
ch=temp.GetAt(0);
count=1;
if(temp.GetLength()<2)
{
count=1;
s.Format("字符串中字符%c的个数是:%d\n",ch,count);
m_result+=s;
break;
}
for(int j=1;j<len;j++)
{
if(temp.GetAt(j)==ch)
{
count++;
}
}
s.Format("字符串中字符%c的个数是:%d\n",ch,count);
m_result+=s;
temp.Remove(ch);
}
MessageBox(m_result);
}
其他的题我慢慢想,想好再回答!
希望更多的人把这样的面试题晒出来让大家做,欢迎!
{
public static Dictionary<int, SqlConnection> connList = new Dictionary<int, SqlConnection>();
public static int currentNumber = 0;
public static readonly int maxNumber; // 连接池链接数量的最大数
public static string connstring; private static object lockObject = new object(); public static SqlConnectionContainer(int max)
{
maxNumber = max;
} public static DbConnection GetConnection()
{ lock (lockObject) //防止 并发操作
{ for (int i = 0; i < connList.Count; i++)
{
if (connList[i].State == ConnectionState.Closed)
{
return connList[i];
}
}
//没找到 可用的链接 if (currentNumber > maxNumber) //判断连接池是否满了
{
throw new Exception("超过了最大连接池数量");
} SqlConnection conn = new SqlConnection(connstring);
connList.Add(connList.Count+1, conn); currentNumber++;
return conn;
}
}
}
public static void count(String str){
int[] tmp=new int[256];
for(int i=0;i<str.length();i++){
tmp[(int)str.charAt(i)]++;
}
for(int i=0;i<tmp.length;i++){
if(tmp[i]!=0)
System.out.println((char)i+":"+tmp[i]);
}
}
if(s!=null){
for (int i = 0; i < s.length(); i++) {
System.out.println(s.substring(i, i+1));
}
System.out.println("它的个数为:"+s.length());
}else{
System.out.println("为NULL");
}
}输出的是什么啊?输出的就是字符串本身,肯定不行,等我改造!!!
public class test {
public static void main(String args[]) { String s = "";
Scanner num = new Scanner(System.in);
s = num.nextLine();
List<testbean> list=new ArrayList();
char temp[]=s.toCharArray();
for(int i=0;i<temp.length;i++){
int n=0;
for(int j=0;j<temp.length;j++){
if(temp[i]==temp[j]){
n++;
}
}
if(i==0){
testbean test=new testbean();
test.setName(temp[i]);
test.setNum(n);
list.add(test);
}else{
int m=0;
for(int x=0;x<list.size();x++){
if(temp[i]==list.get(x).getName()){
m=1;
}
}
if(m==0){
testbean test=new testbean();
test.setName(temp[i]);
test.setNum(n);
list.add(test);
}
}
}
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).getName());
System.out.print(",");
System.out.println(list.get(i).getNum());
}
}
}
public static void main( String[] args )
{
String s = "aabbwercbnmasgccersdfrrajjjb";
String s2 = "";
String[] arr = s.split( "(?<=\\G.{1})" );
Arrays.sort( arr );
for ( String i : arr )
{
s2 += i;
}
Matcher m = Pattern.compile( "(\\w)\\1*" ).matcher( s2 );
while ( m.find() )
{
System.out.println( m.group().substring( 0, 1 ) + "的个数:"
+ m.group().length() );
} }打印:
a的个数:4
b的个数:4
c的个数:3
d的个数:1
e的个数:2
f的个数:1
g的个数:1
j的个数:3
m的个数:1
n的个数:1
r的个数:4
s的个数:2
w的个数:1
if(n < 3)
return n;
int sum1 = 1;
int sum2 = 2;
while(n-- >= 3){
sum2 += sum1;
sum1 = sum2 - sum1;
}
return sum2;
}
/**
* 得到数据库连接
*
* @return 数据库连接对象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getCon() {
try {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(driver);
return Driver…
* @brief 计算字符串中的各个字符的个数
* @notes 只适用单字符数据,对于类似中文的,不支持, 且只是在函数体打印结果,可返回,但需整理
* @param data 源数据
* @return 完成 0
* 失败 -1
**/
int charactor_num(const char *data)
{
int flag_array[128];
int i = 0;
int data_len = 0;
if (NULL == data) {
return -1;
}
for (i = 0; i < 128; i++) {
flag_array[i] = '\0';
}
data_len = strlen(data);
for (i = 0; i < data_len; i++) {
flag_array[data[i]]++;
}
for (i = 0; i < 128; i++) {
if (0 != flag_array[i]) {
printf("%c num is:%d\n", i, flag_array[i]);
}
}
return 0;
}int main(int argc, char **argv)
{
charactor_num("aslkdfjoaiedfjlefndakslfjadoijlekjwqojdlasfjdkjfnvlzxnvklj");
return 0;
}
{
if(i<1)
{
return 0;
}
else if(i>1 && i<2)
{
return 1;
}
else
{
return getValue(i-1)+getValue(i-2)
}
}
package csdn;//定义人的类
class People
{
public void wakeUp()
{
};
public void runAway()
{
}
}//主人是继承人
class Master extends People
{
public void wakeUp()
{
System.out.println("What happened with the dog?");
}
}
//当然贼也是继承人
class Thief extends People
{
public void runAway()
{
System.out.println("you cann't see me,you cann't see me,you cannn't........");
}
}
//看门狗的类
class Dog
{
void shout()
{
Master master = new Master();
Thief thief = new Thief();
System.out.println("The dog is yelling");
master.wakeUp();
thief.runAway();
}
}public class dog_man_thief
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.shout();
}
}
package csdn;class People
{
public void wakeUp()
{
};
public void runAway()
{
}
}class Master extends People
{
public void wakeUp()
{
System.out.println("What happened with the dog?");
}
}
class Thief extends People
{
public void runAway()
{
System.out.println("you cann't see me,you cann't see me,you cannn't........");
}
}
class Dog
{
void shout()
{
Master master = new Master();
Thief thief = new Thief();
System.out.println("The dog is yelling");
master.wakeUp();
thief.runAway();
}
}public class dog_man_thief
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.shout();
}
}
**** baidu:"java设计模式:追MM与JAVA设计模式 "****
具体名称自己对应
class 狗{
叫方法{
主人1.醒了()//或者传变量
}}
class主人{
public 醒了{
小偷.跑了()//或者传变量
}}
class小偷{
public 跑了{
}}总结类set* get*方法望高手拍砖~
private String dName;
//拥有多个监听者的引用,由狗本身来提醒他们
private List<ActionListener> listeners = new ArrayList<ActionListener>();
//事件源
private Event e = new Event(this);//狗叫的行为
public void jiao(){
System.out.println(dName+"叫了!!");
for(ActionListener l : listeners){
l.actionPerformed(e);
}
}
public void addListener(ActionListener listener){
if(listener != null){listeners.add(listener);}
}
public String getDName() {
return dName;
} public void setDName(String name) {
dName = name;
}
}
//监听器
interface ActionListener{
public void actionPerformed(Event e);
}
//事件源
class Event{
Object obj = null;
public Event(Object obj){
this.obj = obj;
}
}class Master implements ActionListener{
public void actionPerformed(Event e) {
System.out.println("主人醒了!!");
}
}class Thief implements ActionListener{
public void actionPerformed(Event e) {
System.out.println("小偷吓跑了!!");
}
}
public class Test {
public static void main(String[] args) {
Master m = new Master();
Thief t = new Thief();
Dog d = new Dog();
d.setDName("牧羊狗!!");
d.addListener(m);
d.addListener(t);
d.jiao();
}
}