本文目錄一覽:
java演算法題——龜兔賽跑
public class Test1{
public static void main(String[] args) throws InterruptedException {
int v1=25,v2=10,t=20,s=3,l=100;
Scanner scanner=new Scanner(System.in);
v1=scanner.nextInt();
v2=scanner.nextInt();
t=scanner.nextInt();
s=scanner.nextInt();
l=scanner.nextInt();
int sum1=0,sum2=0;
boolean stop=false;
int stopcount=0;
int i=0;
for ( i = 0; i l/v2; i++) {
if(sum1=l||sum2=l)//如果有一個跑到了終點就結束了
break;
if(stopcount==s)
stop=false; //如果休息的時間夠了,就開始跑
if(sum1-sum2=t)
stop=true; //如果超過了t米,就休息
if(!stop)
sum1+=v1; //當兔子不休息的時候跑
else
stopcount++; //休息的時間計數
sum2+=v2;//烏龜每次都會跑
System.out.print(“兔子跑了:”+sum1+”米”);
System.out.println(“烏龜跑了:”+sum2+”米”);
}
if(sum1==sum2)
System.out.println(“D”+i);
else if(sum1=l)
System.out.println(“R”+i);
else if(sum2=l)
System.out.println(“T”+i);
}
}
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;iargs.length-1;i++){ for(int j=i+1;jargs.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;iargs.length-1 ;i++ ){ int min=i; for (int j=i+1;jargs.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;iargs.length;i++){ for(int j=i;j0;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面試有哪些演算法
面試-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!=jk!=ji!=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(aIndexa.length){
c.write(a[aIndex++] + “\n”);
if(bIndexb.length)
c.write(b[bIndex++] + “\n”);
}
while(bIndexb.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){
HashMapLong,String m=new HashMapLong,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){
HashMapLong,String m=new HashMapLong,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=””;
HashMapString,String m=new HashMapString,String();
String [] a=str.split(” “); for (int i=0;ia.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) {
ListInteger countList=new ArrayListInteger(); int count;
HashMapString,String m;
String str; //讀取鍵盤輸入的一行(以回車換行為結束輸入) String[] a;
Scanner in=new Scanner(System.in);
while( !(str=in.nextLine()).equals(“#”) ){
a=str.split(” “);
m=new HashMapString,String();
count = 0; for (int i=0;ia.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經典演算法題——猴子吃桃
public class Monkey
{
public static void main(String[] args)
{
int sum=0,remain=1;
//每天吃剩的桃子加一個正好是前一天桃子的一半,每天桃子的總數就是前一天剩下桃子的數量
for(int day=9;day=1;day–)
{
sum=(remain+1)*2;
remain=sum;
System.out.println(“第”+day+”天還剩”+remain+”個桃子”);
}
System.out.println(sum);
}
}
求 Java 一些經典例子演算法
//前n項階乘分之一的和
public class jiecheng {
public static void main(String[] args)
{
double sum=0;
double j=1;
int n=10;
for(int i=1;i=10;i++)
{
j=j*i;
sum=1/j+sum;
}
System.out.println(“前”+n+”項階乘的和為:”+sum);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/253947.html