導航:首頁 > 源碼編譯 > java組卷演算法

java組卷演算法

發布時間:2022-05-22 22:56:03

A. java面試有哪些演算法

面試-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庫中又提供了一些通用的演算法

閱讀全文

與java組卷演算法相關的資料

熱點內容
數據加密過程簡述 瀏覽:809
python基礎教程pdf下載 瀏覽:123
如何統計伺服器 瀏覽:742
蘋果和安卓怎麼贈送模組 瀏覽:803
伺服器倒計時怎麼弄 瀏覽:30
excel文件夾更新 瀏覽:435
億點連接app哪裡好 瀏覽:788
java掃碼支付 瀏覽:875
單片機行車記錄儀 瀏覽:393
oppo雲伺服器什麼意思 瀏覽:82
51單片機可以編譯多少公里 瀏覽:27
用什麼工具製作安卓應用 瀏覽:488
單片機數碼管的代碼 瀏覽:779
第一款安卓手機是什麼牌子 瀏覽:396
java非同步web 瀏覽:274
51單片機讀tf卡 瀏覽:940
linux下獲取文件 瀏覽:320
加密文件電腦顯示無屏幕截取許可權 瀏覽:356
虛榮安卓用什麼充值 瀏覽:754
阿里雲沒有伺服器如何備案 瀏覽:708