初級為了看懂可以看:演算法設計與分析(第三版)這本高校教材。這本是Java寫的,把上面每個演算法研究清楚,
後期為了拓展可以看Java數據結構與演算法分析等。淘寶直接搜索就可以知道書的具體信息
到了比較大神級的可以拓展看演算法導論、編程之美這些大牛書籍。
⑵ java 演算法,請問這是什麼演算法啊
Math.sqrt(i)是強製取整,你可以直接把這句話理解為while(j <= i){...
限制條件j<=i 是為了避免重復運算 比如j=3,i=5的情況 跟j=5,i=3的情況 後者就沒必要去驗證了對不對?這樣可以提高程序效率。
⑶ 用java做演算法好嗎
java是建立在許多軟體基礎上的編程語言,對於演算法程序的優化手段受到很大限制,這些都不利於做演算法
可以考慮用c做演算法,可以在兼顧可移植性的同時盡量優化,然後在java中調用。
⑷ java演算法有哪些分別
您好:
java中的演算法,常見的有:遞歸、迭代、查找、排序(包含冒泡排序、選擇排序、插入排序、快速排序四種) 等,演算法有很多,一般數據結構中涉及到的都可以用java語言實現。
舉幾個例子:
1.遞歸的例子:
不一一舉例,僅供參考!
⑸ 誰能給我解釋一下Java和Java的演算法是什麼意思,電腦啥也不懂,請大神用通俗易懂話解釋
這個解釋起來是有點難度的,不知道你的基礎怎麼樣
Java是一種計算機編程語言,
Java的演算法和其他編程語言的演算法沒有什麼區別,都是用計算機數學的方式和邏輯來解決問題。
Java的演算法的理解需要有一定的數學基礎和計算機邏輯思維基礎。
簡單點理解,有點像解題,是比較復雜的數學題,需要用計算機能夠理解和接受的方式來解題。
⑹ java中的演算法,一共有多少種,哪幾種,怎麼分類。
就好比問,漢語中常用寫作方法有多少種,怎麼分類。
演算法按用途分,體現設計目的、有什麼特點
演算法按實現方式分,有遞歸、迭代、平行、序列、過程、確定、不確定等等
演算法按設計范型分,有分治、動態、貪心、線性、圖論、簡化等等
作為圖靈完備的語言,理論上」Java語言「可以實現所有演算法。
「Java的標准庫'中用了一些常用數據結構和相關演算法.
像apache common這樣的java庫中又提供了一些通用的演算法
⑺ 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; }
⑻ 如何用java寫一個演算法
是指一個類調用另一個類的么?
如果是這樣的話。。
//創建一個類
public class Text1{
public static void mian(String []agrs){
//實例化另一個類
Text2 t = new Text2();
t.show();
}
}
創建第二個類
public class Text2(){
//創建一個show方法
public void show(){
System.out.println("我已經被調用了");
}
}
這就是類之間的調用。。
如果是想在方法里調用另一個類的方法
具體的也是和main()方法里的一樣調用。(模仿main()方法)
⑼ 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);
}
⑽ 五子棋的Java演算法
五子棋的演算法是比較簡單的。
把棋盤當作一個 2 維數組。 用2維數組來當作棋盤的坐標系
當落子 之後。 把落子,插入到 數組中 獲得 棋盤 的數組, 循環剛才數組, 判斷,
剛才 數組元素 的 橫向坐標 -5 到剛才 數組元素坐標 + 5 是否都是 一個數字(黑子代表 1 ,白子代表0) 只要其中 有連續 5個 都是 黑子,或者白子, 則黑子或白子 贏了。
判斷,剛才元素 縱向坐標 -5 到 + 5 如上判斷。
判斷 右斜線。 判斷 橫向坐標 -5 y -5 到 橫向坐標 + 5 y + 5
判斷 y + 5 x + 5 到 y-5 x -5
簡單來說。
用2維數組 來代表 棋盤 , 每次在 界面上, 由白子,或黑子 落子之後, 在數組相應坐標,放入 1 或者0 。
然後循環數組判斷, 數組橫向 豎向 右斜線 左斜線 是否是 黑子或者白子 連續的。 如果是,則獲勝。