验证中...
Languages: Java
Categories: 嵌入式开发
Latest update 2018-12-09 10:09
Snippet 1 Snippet 2 Snippet 3 Snippet 4 Snippet 5
求100以内素数
Raw Copy
package com.lzq;
public class Test {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
for (int k = 2; k <= i; k++) {
if (i % k == 0 && i == k) {
System.out.println(i);
}
if(i%k==0&&i!=k){
break;
}
}
}
}
}
字符串的反转
Raw Copy
String str="abcd";
char c[]=str.toCharArray();
int i=0;
int in=c.length-1 ;
while(true){
if(i>=in){
break;
}
char temp=c[i];
c[i]=c[in];
c[in]=temp;
i++;
in--;
}
str=new String(c);
System.out.println(str);
递归,n的阶乘
Raw Copy
public static int Jiecheng(int n){
if(n==0||n==1){
return n;
}
return n*Jiecheng(n-1);
}
递归,斐波那契数列
Raw Copy
/**
* 斐波那契数列 1 1 2 3 5 8 13
* @param n
* @return
*/
public static int Bofei(int n){
if(n==1||n==0){
return 1;
}
return Bofei(n-1)+Bofei(n-2);
}
递归,二分查找
Raw Copy
/**
* 二分查找
* @param args
*/
public static int Er(int a[],int key,int min,int max){
if(key<a[min]||key>a[max]||min>max){
throw new RuntimeException("无查找对象");
}
int mid=(max+min)/2;
if(key<a[mid]){
return Er(a,key,min,mid-1);
}
if(key>a[mid]){
return Er(a,key,mid+1,max);
}else{
return mid;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search