欢迎来到天天文库
浏览记录
ID:16430958
大小:973.00 KB
页数:130页
时间:2018-08-09
《[计算机软件及应用]算法题目答案整理》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、N皇后问题/*N皇后题目描述说明西洋棋中的皇后可以直线前进,吃掉遇到的所有棋子,如果棋盘上有八个皇后,则这八个皇后如何相安无事的放置在棋盘上,*1970年与1971年,E.W.Dijkstra与N.Wirth曾经用这个问题来讲解程式设计之技巧。该题要求N皇后的放置结果共有多少种输入描述输入一个正整数N(N<16)输出描述输出结果输入样例8输出样例92**1*2*/packagejava201211;importjava.util.Scanner;/****@authorAdministrator*/publicclassN皇后{staticintn=8;staticint[]lie
2、=newint[n];staticintcount=0;staticbooleancanPlace(intk){booleancanPlace=true;for(inti=0;i3、4、lie[k]==lie[i]){canPlace=false;}}returncanPlace;}staticvoidbacktrack(intm){if(m>n-1){count++;}else{for(inti=0;i5、m+1);}}}}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereScannersc=newScanner(System.in);n=sc.nextInt();lie=newint[n];backtrack(0);System.out.println(count);}}24点/*二十四点题目描述问题描述:输入4个数,通过加、减、乘、除运算看能否得到结果24,每个数只用一次,如果能输出一种公式,输入描述输入四个正整数输出描述能6、通过某种方式得到24则输出1,不能则输出0输入样例6666输出样例16+6+6+6***0123456*0123**1234*343*373-1*1010**-4-1**/packagejava201211;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;/****@authorAdministrator*/publicclass_24点2{staticintdata[]={6,6,6,6};staticfloats=0;staticStringstr="";staticList7、ng>strList=newArrayList();staticintcount=0;staticStringstrii="";staticListlist=newArrayList();staticvoidsearch(Listlist){if(list.size()==1){if(s==24){count++;}//System.out.println(str);}else{for(intn=0;ntlist=newArray8、List();tlist.addAll(list);s=list.get(n)+list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)-list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=-list.get(n)+list.get(i);t9、list.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)*list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)/list.get(
3、
4、lie[k]==lie[i]){canPlace=false;}}returncanPlace;}staticvoidbacktrack(intm){if(m>n-1){count++;}else{for(inti=0;i5、m+1);}}}}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereScannersc=newScanner(System.in);n=sc.nextInt();lie=newint[n];backtrack(0);System.out.println(count);}}24点/*二十四点题目描述问题描述:输入4个数,通过加、减、乘、除运算看能否得到结果24,每个数只用一次,如果能输出一种公式,输入描述输入四个正整数输出描述能6、通过某种方式得到24则输出1,不能则输出0输入样例6666输出样例16+6+6+6***0123456*0123**1234*343*373-1*1010**-4-1**/packagejava201211;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;/****@authorAdministrator*/publicclass_24点2{staticintdata[]={6,6,6,6};staticfloats=0;staticStringstr="";staticList7、ng>strList=newArrayList();staticintcount=0;staticStringstrii="";staticListlist=newArrayList();staticvoidsearch(Listlist){if(list.size()==1){if(s==24){count++;}//System.out.println(str);}else{for(intn=0;ntlist=newArray8、List();tlist.addAll(list);s=list.get(n)+list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)-list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=-list.get(n)+list.get(i);t9、list.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)*list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)/list.get(
5、m+1);}}}}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereScannersc=newScanner(System.in);n=sc.nextInt();lie=newint[n];backtrack(0);System.out.println(count);}}24点/*二十四点题目描述问题描述:输入4个数,通过加、减、乘、除运算看能否得到结果24,每个数只用一次,如果能输出一种公式,输入描述输入四个正整数输出描述能
6、通过某种方式得到24则输出1,不能则输出0输入样例6666输出样例16+6+6+6***0123456*0123**1234*343*373-1*1010**-4-1**/packagejava201211;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;/****@authorAdministrator*/publicclass_24点2{staticintdata[]={6,6,6,6};staticfloats=0;staticStringstr="";staticList7、ng>strList=newArrayList();staticintcount=0;staticStringstrii="";staticListlist=newArrayList();staticvoidsearch(Listlist){if(list.size()==1){if(s==24){count++;}//System.out.println(str);}else{for(intn=0;ntlist=newArray8、List();tlist.addAll(list);s=list.get(n)+list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)-list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=-list.get(n)+list.get(i);t9、list.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)*list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)/list.get(
7、ng>strList=newArrayList();staticintcount=0;staticStringstrii="";staticListlist=newArrayList();staticvoidsearch(Listlist){if(list.size()==1){if(s==24){count++;}//System.out.println(str);}else{for(intn=0;ntlist=newArray
8、List();tlist.addAll(list);s=list.get(n)+list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)-list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=-list.get(n)+list.get(i);t
9、list.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)*list.get(i);tlist.remove(n);tlist.remove(i-1);tlist.add(s);search(tlist);tlist.clear();tlist.addAll(list);s=list.get(n)/list.get(
此文档下载收益归作者所有