面试-java算法题:
1.编写一个程序,输入n,求n!(用递归的方式实现)。
public static long fac(int n){ if(n<=0) return 0; else if(n==1) return 1; else return n*fac(n-1);
} public static void main(String [] args) {
System.out.println(fac(6));
}
2.编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
public static void main(String [] args) { int i, j, k; int m=0; for(i=1;i<=4;i++) for(j=1;j<=4;j++) for(k=1;k<=4;k++){ if(i!=j&&k!=j&&i!=k){
System.out.println(""+i+j+k);
m++;
}
}
System.out.println("能组成:"+m+"个");
}
3.编写一个程序,将text1.txt文件中的单词与text2.txt文件中的单词交替合并到text3.txt文件中。text1.txt文件中的单词用回车符分隔,text2.txt文件中用回车或空格进行分隔。
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class text{
public static void main(String[] args) throws Exception{
String[] a = getArrayByFile("text1.txt",new char[]{'\n'});
String[] b = getArrayByFile("text2.txt",new char[]{'\n',' '});
FileWriter c = new FileWriter("text3.txt");
int aIndex=0; int bIndex=0;
while(aIndex<a.length){
c.write(a[aIndex++] + "\n");
if(bIndex<b.length)
c.write(b[bIndex++] + "\n");
}
while(bIndex<b.length){
c.write(b[bIndex++] + "\n");
}
c.close();
}
public static String[] getArrayByFile(String filename,char[] seperators) throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf = new char[(int)f.length()];
int len = reader.read(buf);
String results = new String(buf,0,len);
String regex = null;
if(seperators.length >1 ){
regex = "" + seperators[0] + "|" + seperators[1];
}else{
regex = "" + seperators[0];
}
return results.split(regex);
}
}
4.639172每个位数上的数字都是不同的,且平方后所得数字的所有位数都不会出现组成它自身的数字。(639172*639172=408540845584),类似于639172这样的6位数还有几个?分别是什么?
这题采用的HashMap结构判断有无重复,也可以采用下题的数组判断。
public void selectNum(){
for(long n = 100000; n <= 999999;n++){
if(isSelfRepeat(n)) //有相同的数字,则跳过
continue;
else if(isPingFangRepeat(n*n,n)){ //该数的平方中是否有与该数相同的数字
continue;
} else{ //符合条件,则打印 System.out.println(n);
}
}
} public boolean isSelfRepeat(long n){
HashMap<Long,String> m=new HashMap<Long,String>(); //存储的时候判断有无重复值
while(n!=0){ if(m.containsKey(n%10)){ return true;
} else{
m.put(n%10,"1");
}
n=n/10;
} return false;
} public boolean isPingFangRepeat(long pingfang,long n){
HashMap<Long,String> m=new HashMap<Long,String>(); while(n!=0){
m.put(n%10,"1");
n=n/10;
} while(pingfang!=0){ if(m.containsKey(pingfang%10)){ return true;
}
pingfang=pingfang/10;
} return false;
} public static void main(String args[]){ new test().selectNum();
}
5.比如,968548+968545=321732732它的答案里没有前面两个数里的数字,有多少这样的6位数。
public void selectNum(){
for(int n = 10; n <= 99;n++){
for(int m = 10; m <= 99;m++){ if(isRepeat(n,m)){ continue;
} else{
System.out.println("组合是"+n+","+m);
}
}
}
} public boolean isRepeat(int n,int m){ int[] a={0,0,0,0,0,0,0,0,0,0}; int s=n+m; while(n!=0){
a[n%10]=1;
n=n/10;
} while(m!=0){
a[m%10]=1;
m=m/10;
} while(s!=0){ if(a[s%10]==1){ return true;
}
s=s/10;
} return false;
} public static void main(String args[]){ new test().selectNum();
}
6.给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。
public static void main(String [] args) { int count = 0;
String str="hello world hello hi";
String newStr="";
HashMap<String,String> m=new HashMap<String,String>();
String [] a=str.split(" "); for (int i=0;i<a.length;i++){ if(!m.containsKey(a[i])){
m.put(a[i],"1");
count++;
newStr=newStr+" "+a[i];
}
}
System.out.println("这段短文单词的个数是:"+count+","+newStr);
}
7.写出程序运行结果。
public class Test1 { private static void test(int[]arr) { for (int i = 0; i < arr.length; i++) { try { if (arr[i] % 2 == 0) { throw new NullPointerException();
} else {
System.out.print(i);
}
} catch (Exception e) {
System.out.print("a ");
} finally {
System.out.print("b ");
}
}
}
public static void main(String[]args) { try {
test(new int[] {0, 1, 2, 3, 4, 5});
} catch (Exception e) {
System.out.print("c ");
}
}
}
运行结果:a b 1b a b 3b a b 5b
public class Test1 { private static void test(int[]arr) { for (int i = 0; i < arr.length; i++) { try { if (arr[i] % 2 == 0) { throw new NullPointerException();
} else {
System.out.print(i);
}
}
finally {
System.out.print("b ");
}
}
}
public static void main(String[]args) { try {
test(new int[] {0, 1, 2, 3, 4, 5});
} catch (Exception e) {
System.out.print("c ");
}
}
}
运行结果:b c
8.单词数
统计一篇文章里不同单词的总数。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组值输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
#
Sample Output
4
public static void main(String [] args) {
List<Integer> countList=new ArrayList<Integer>(); int count;
HashMap<String,String> m;
String str; //读取键盘输入的一行(以回车换行为结束输入) String[] a;
Scanner in=new Scanner(System.in);
while( !(str=in.nextLine()).equals("#") ){
a=str.split(" ");
m=new HashMap<String,String>();
count = 0; for (int i=0;i<a.length;i++){ if(!m.containsKey(a[i]) && (!a[i].equals(""))){
m.put(a[i],"1");
count++;
}
}
countList.add(count);
}s for(int c:countList)
System.out.println(c);
}
B. java排列组合算法
//这个程序是以前用高分求来的,现在稍作修改,呵呵
public class Zuhe {
public static void main(String[] args) {
String s = "122345";//这里是要用到的所有数组成的一个字符串,其它字符同样适用
char[] c = s.toCharArray();
new Zuhe().zuhe(c,c.length,0);
System.out.println("可能的组合数:"+kk);
}
static int kk=0;
private void zuhe(char[] array, int n, int k) {
if (n == k) {
if(array[2]!='4'){//第三个位置不能出现4
String str = new String(array);
if(str.indexOf("53")<0&&str.indexOf("35")<0){//3,5不能连续出现
System.out.println(str);
++kk;
}
}
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
zuhe(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
========结果=========
122345
122543
123245
123254
123425
123452
125432
125423
125243
125234
122345
122543
123245
123254
123425
123452
125432
125423
125243
125234
132245
132254
132425
132452
132542
132524
132245
132254
132425
132452
132542
132524
142325
142523
143225
143252
143225
143252
142325
142523
145232
145223
145223
145232
152342
152324
152432
152423
152243
152234
152342
152324
152432
152423
152243
152234
212345
212543
213245
213254
213425
213452
215432
215423
215243
215234
221345
221543
223145
223154
223415
223451
225431
225413
225143
225134
232145
232154
232415
232451
232541
232514
231245
231254
231425
231452
231542
231524
242315
242513
243215
243251
243125
243152
241325
241523
245132
245123
245213
245231
252341
252314
252431
252413
252143
252134
251342
251324
251432
251423
251243
251234
221345
221543
223145
223154
223415
223451
225431
225413
225143
225134
212345
212543
213245
213254
213425
213452
215432
215423
215243
215234
231245
231254
231425
231452
231542
231524
232145
232154
232415
232451
232541
232514
241325
241523
243125
243152
243215
243251
242315
242513
245231
245213
245123
245132
251342
251324
251432
251423
251243
251234
252341
252314
252431
252413
252143
252134
322145
322154
322415
322451
322541
322514
321245
321254
321425
321452
321542
321524
325142
325124
325412
325421
325241
325214
322145
322154
322415
322451
322541
322514
321245
321254
321425
321452
321542
321524
325142
325124
325412
325421
325241
325214
312245
312254
312425
312452
312542
312524
312245
312254
312425
312452
312542
312524
315242
315224
315422
315422
315242
315224
342125
342152
342215
342251
342521
342512
341225
341252
341225
341252
341522
341522
342125
342152
342215
342251
342521
342512
345122
345122
345212
345221
345221
345212
422315
422513
423215
423251
423125
423152
421325
421523
425132
425123
425213
425231
422315
422513
423215
423251
423125
423152
421325
421523
425132
425123
425213
425231
432215
432251
432125
432152
432512
432521
432215
432251
432125
432152
432512
432521
431225
431252
431225
431252
431522
431522
412325
412523
413225
413252
413225
413252
412325
412523
415232
415223
415223
415232
452312
452321
452132
452123
452213
452231
451322
451322
451232
451223
451223
451232
452312
452321
452132
452123
452213
452231
522341
522314
522431
522413
522143
522134
523241
523214
523421
523412
523142
523124
521342
521324
521432
521423
521243
521234
522341
522314
522431
522413
522143
522134
523241
523214
523421
523412
523142
523124
521342
521324
521432
521423
521243
521234
542321
542312
542231
542213
542123
542132
543221
543212
543221
543212
543122
543122
542321
542312
542231
542213
542123
542132
541322
541322
541232
541223
541223
541232
512342
512324
512432
512423
512243
512234
513242
513224
513422
513422
513242
513224
512342
512324
512432
512423
512243
512234
可能的组合数:396
C. java排列组合的算法 譬如我有(A,B,C,D),我想输出的结果是
我觉得可以看成数字的排列如 1 2 3 4分别代表A B C D
就是将1 2 3 4排列
四位的就是1234
三位的就是从这四个数字中取出三个数字,得到的三位数是最小的,如:
取 1 2 3 可以得到123 213 321 132等等 其中123是最小的
两为数字的跟三位数字的一样
D. 高分 java 遗传算法 组卷 一条染色体在变异后 影响 其他都变异 怎么办在线等!急急急!
我是搞j2ee开发的,现在在深圳科技园上班。我看你的代码,不是很难,但是不太懂你的业务逻辑。你不要着急,如果方便,你详细说下你的业务逻辑和,希望我能帮到你。
E. Java的数组的几种经典算法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法、冒泡法、选择排序法、插入排序法。
快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现。
冒泡法是运用遍历数组进行比较,通过不断的比较将最小值或者最大值一个一个的遍历出来。
选择排序法是将数组的第一个数据作为最大或者最小的值,然后通过比较循环,输出有序的数组。
插入排序是选择一个数组中的数据,通过不断的插入比较最后进行排序。下面我就将他们的实现方法一一详解供大家参考。
<1>利用Arrays带有的排序方法快速排序
public class Test2{ public static void main(String[] args){ int[] a={5,4,2,4,9,1}; Arrays.sort(a); //进行排序 for(int i: a){ System.out.print(i); } } }
<2>冒泡排序算法
public static int[] bubbleSort(int[] args){//冒泡排序算法 for(int i=0;i<args.length-1;i++){ for(int j=i+1;j<args.length;j++){ if (args[i]>args[j]){ int temp=args[i]; args[i]=args[j]; args[j]=temp; } } } return args; }
<3>选择排序算法
public static int[] selectSort(int[] args){//选择排序算法 for (int i=0;i<args.length-1 ;i++ ){ int min=i; for (int j=i+1;j<args.length ;j++ ){ if (args[min]>args[j]){ min=j; } } if (min!=i){ int temp=args[i]; args[i]=args[min]; args[min]=temp; } } return args; }
<4>插入排序算法
public static int[] insertSort(int[] args){//插入排序算法 for(int i=1;i<args.length;i++){ for(int j=i;j>0;j--){ if (args[j]<args[j-1]){ int temp=args[j-1]; args[j-1]=args[j]; args[j]=temp; }else break; } } return args; }
F. java实现组卷功能
组卷一般分两种模式
一种是固定试卷 你直接在你题库选择题目 拼成了试卷 生成你直接拼接字符串生成一个html文件就可以了
一种是随机试卷,你设计好 选择题几个 问答题几个 。。。类似 然后一键生成 根据你设定的参数去随机取题目 然后 按一定的规律拼接就可以了 比如3个选择 2个填空 1个问答 剩下那些拼接都是布局的问题了
G. 用JAVA写一个a,b,c,d,e排列组合算法,谢谢了
public class Paixu {
public static void main(String[] args) {
char[] in = "abcde".toCharArray();
new Paixu().paixu(in, in.length, 0);
}
private void paixu(char[] array, int n, int k) {
if (n == k) {
char[] out = new char[n];
for (int i = 0; i < array.length; i++) {
out[i] = array[i];
}
System.out.println(new String(out));
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
paixu(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
H. 如何用Java实现随机出题
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.jdbc.driver.OracleDriver;
public class GenPaperServlet extends HttpServlet
{
Connection conn;
Statement stmt;
ResultSet rs;
int total_question_num;
int total_question_in_paper;
int total_paper_num;
String curr_classid;
public GenPaperServlet()
{
conn = null;
stmt = null;
rs = null;
total_question_num = 0;
total_question_in_paper = 0;
total_paper_num = 0;
curr_classid = "";
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("<html><head></head><body><center>");
printwriter.println("请以POST方式提交");
printwriter.println("</center></body></html>");
printwriter.close();
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
String s = httpservletrequest.getParameter("classid"); //"20"
String s1 = httpservletrequest.getParameter("paper_num"); //"1"
if(s == null || s1 == null)
{
printwriter.println("<center>");
printwriter.println("请按照正常方式提交数据<br>");
printwriter.println("<a href=/test/admin/genpaper.jsp>单击这里设置生成试卷的参数</a>");
printwriter.println("</center>");
}
total_paper_num = Integer.parseInt(s1);
curr_classid = s;
int i = 0;
if(!open_db(curr_classid))
{
printwriter.println("打开数据库错误!");
return;
}
if(!setParams(curr_classid))
{
System.out.println("设置系统参数错误!");
return;
}
if(!verify_QuertionLib())
{
printwriter.println("试题库中试卷不足,请增加新的试题!");
printwriter.println("班级代号:" + curr_classid);
printwriter.println("该班级一套试卷中的试题数:" + total_question_in_paper);
printwriter.println("目前题库中该班级的试题总数:" + total_question_num);
return;
}
i = genPaper(total_paper_num, curr_classid);
if(i == 0)
{
printwriter.println("生成试卷操作失败!");
return;
}
if(!updateOtherTable(i, curr_classid))
{
printwriter.println("更新相关表操作失败!");
return;
} else
{
printwriter.println("<center>");
printwriter.println("动态组卷成功!<br>");
printwriter.println("共生成了 " + i + " 套试卷<br>");
printwriter.println("<a href=/test/admin/genpaper.jsp>单击这里设置生成试卷的参数</a>");
printwriter.println("</center>");
return;
}
}
public boolean open_db(String s)
{
try
{
new OracleDriver();
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora9", "scott", "tiger");
stmt = conn.createStatement();
}
catch(Exception exception)
{
return false;
}
return true;
}
public boolean setParams(String s)
{
String s1 = "";
try
{
String s2 = "select count(questionid) as countquestionid from test_question_lib ";
s2 = s2 + "where classid='" + s + "'";
rs = stmt.executeQuery(s2);
rs.next();
total_question_num = rs.getInt("countquestionid");
s2 = "select totalques from test_classinfo ";
s2 = s2 + "where classid='" + s + "'";
rs = stmt.executeQuery(s2);
rs.next();
total_question_in_paper = rs.getInt("totalques");
}
catch(Exception exception)
{
return false;
}
return true;
}
public boolean verify_QuertionLib()
{
return total_question_num >= total_question_in_paper;
}
public boolean updateOtherTable(int i, String s)
{
int j = 0;
String s1 = "update test_classinfo set totalpaper=totalpaper+";
s1 = s1 + i + " where classid='" + s + "'";
try
{
j = stmt.executeUpdate(s1);
}
catch(Exception exception)
{
return false;
}
return j == 1;
}
public int genPaper(int i, String s)
{
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
boolean flag3 = false;
String s1 = "";
try
{
int ai[] = new int[total_question_num];
int i1 = 0;
boolean flag4 = false;
String s2 = "select max(paper_id) as max_paper_id from test_paper_lib";
rs = stmt.executeQuery(s2);
rs.next();
int j = rs.getInt("max_paper_id") + 1;
s2 = "select questionid from test_question_lib where classid='" + s + "'";
for(rs = stmt.executeQuery(s2); rs.next();)
ai[i1++] = rs.getInt("questionid");
for(int k1 = 0; k1 < i; k1++)
{
int k = ai.length; //8
for(int l1 = 0; l1 < total_question_in_paper; l1++)
{
// int ai[] ={1 ,3 , 5 ,9 , 56,30 96 ,25};
int j1 = (int)Math.floor(Math.random() * (double)k); // 4
String s3 = "insert into test_paper_lib values(";
s3 = s3 + "test_seq_paper.nextval," + j + "," + ai[j1] + ")";
stmt.executeUpdate(s3);
int l = ai[k - 1];
ai[k - 1] = ai[j1];
ai[j1] = l;
k--;
}
j++;
}
}
catch(Exception exception)
{
return 0;
}
return i;
}
}
I. 求一个组卷算法,数据库用到哪些表,用jsp+servlet实现就行。
.......用arraylist挨个装入字符串,字符串内容赋值为字符串,然后看这里:http://wenku..com/view/f6195810a21614791711282c.html
J. java中的算法,一共有多少种,哪几种,怎么分类。
就好比问,汉语中常用写作方法有多少种,怎么分类。
算法按用途分,体现设计目的、有什么特点
算法按实现方式分,有递归、迭代、平行、序列、过程、确定、不确定等等
算法按设计范型分,有分治、动态、贪心、线性、图论、简化等等
作为图灵完备的语言,理论上”Java语言“可以实现所有算法。
“Java的标准库'中用了一些常用数据结构和相关算法.
像apache common这样的java库中又提供了一些通用的算法