您的位置:首页 > 编程语言 > Java开发

java写的常见算法

2014-08-31 12:39 253 查看
一些我们的教科书上的常用算法考试,这些可能也会出现在面试中,所以自己花了一些时间整理了一下,程序全部通过执行,有的鲁棒性可能还不够好,希望大家可以添加一些常用的算法,自己以后还会进一步添加。

package Algorithm;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

//testBonus();
//perSqrt();
//selectSort();
//statistics();
//gcdAndLcm1();
//prime(124);
//Sum20();
//classSum();
//positiveIntegerLengthAndConverse();
/*while(true) {
palindrom();
}*/
//intercept();
//count3Quit();
//selectFun(3);
peachCount();
}

//海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分成5份,多了一个,这只猴子吃了多的一个桃子,拿走了一份。
//第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样吃了多的拿走了一份,第三、第四、第五只猴子都是这样,问海滩上原来最少有多少个桃子?
public static void peachCount() {
int n = count(1);
System.out.println(n);
}

public static int count(int n) {
if(n==5) return 6;
return 5*count(n+1)+1;
}

//编写一个函数,输入n为偶数时,调用函数1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n;
public static void selectFun(int n) {
float sum = 0f;
float temp = 0f;
for(;n>0;n=n-2) {
temp = (float)1/n;
sum += temp;
}
System.out.println(sum);
}

//有n个人围成一圈,顺序排号,从第一个人开始报数(从1到3报数)凡报到3的人退出圈子,问最后留下的是原来第几号的那位
public static void count3Quit() {
System.out.println("请输入人数:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
boolean[] b = new boolean
;
for(int i=0;i<b.length;i++) {
b[i] = true;
}
int cout = 0;
int leftCount = n;
int index = 0;
while(leftCount>1) {
if(b[index]) {
cout++;
if(cout==3) {
cout = 0;
b[index] = false;
leftCount--;
}
}
index++;
if(index==n) {
index = 0;
}
}
for(int i=0;i<n;i++) {
if(b[i]) {
System.out.println(i+1);
}
}
}

/*	数三减一(利用java类的实现)
public class Count3Quit {
public static void main(String []args) {
KidCircle kc = new KidCircle(500);
int countNum = 0;
Kid k = kc.first;
while(kc.count>1) {
countNum++;
if(countNum==3) {
countNum = 0;
kc.delete(k);
}
k = k.right;
}

System.out.println(kc.first.id);
}
}

class Kid {
int id;
Kid left;
Kid right;
}

class KidCircle {
int count = 0;
Kid first,last;

KidCircle(int n){
for(int i=0;i<n;i++) {
add();
}
}

void add() {
Kid k = new Kid();
k.id = count;
if(count==0) {
first = k;
last = k;
k.left = k;
k.right = k;
}else{
last.right = k;
k.left = last;
k.right = first;
first.left = k;
last = k;
}
count++;
}

void delete(Kid k) {
if(count<=0) return;
else if(count == 1) {
first = last = null;
}
else {
k.left.right = k.right;
k.right.left = k.left;
if(k==first) {
first = k.right;
}
if(k==last) {
last = k.left;
}
}
count--;
}

}*/

//取一个整数a从右端开始的4~7位。
public static void intercept() {
int a = 0;
long b = 18745678;
a = (int)Math.floor(b%Math.pow(10, 7)/Math.pow(10, 3));
System.out.println(a);
}

//一个五位数,判断它是不是回文数。即12321是回文数,个位和万位相同,十位和千位相同
public static void palindrom() {
System.out.println("请输入一个五位的回文数:");
Scanner scan = new Scanner(System.in);
long l = scan.nextLong();
if(l<10000||l>99999) {
System.out.println("请输入正确的回文数!");
return;
}
int a[] = new int[5];
int b[] = new int[5];
boolean is = false;
for(int i=4;i>=0;i--) {
a[i] = (int)l/(int)Math.pow(10, i);
l = l%(int)Math.pow(10, i);
}
for(int i=0;i<=4;i++) {
b[i] = a[i];
}
for(int i=0,j=4;i<5;i++,j--) {
if(a[i]!=b[j]) {
is = false;
break;
}else {
is = true;
}
}
if(is) {
System.out.println("is a Palindrom!");
}else {
System.out.println("is not a Palindrom!");
}
}

//给一个不多于5位数的正整数,要求:一 求它是几位数 二 逆序打印出各位数字
public static void positiveIntegerLengthAndConverse() {
Scanner scan = new Scanner(System.in);
long lvalue = scan.nextLong();
if(lvalue<0||lvalue>=100000) {
System.out.println("请输入不大于五位的整数!");
System.exit(0);
}
String str  = lvalue +"";
int length = str.length();
char[] c = str.toCharArray();
System.out.println(lvalue+"是"+length+"位数;");
System.out.print("逆序打印为:");
for(int i=c.length-1;i>=0;i--) {
System.out.print(c[i]+" ");
}
}

//求1+2!+3!+...+20!
//这种情况下用循环比用递归的效率要高很多
public static void classSum() {
long sum=0;
long fac=1;
for(int i=1;i<=3;i++) {
fac = fac*i;
sum +=fac;
}
System.out.println("20的阶层和为:"+sum);
}

//有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
public static void Sum20() {
float a,b,c,t,sum;
a = 1;
b = 2;
sum = 0;
for(int i=0;i<20;i++) {
c = b/a;
sum += c;
t = a;
a = b;
b = t+b;
}
System.out.println(""+sum);
}

//两个乒乓球队进行比赛,各出三人。甲队为啊a,b,c三人,乙队为x,y,z三人。
//已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程打出三队赛手的名单。
//思想:定义一个类,三个成员变量abc,然后根据xyz初始化这个类,循环满足条件的都放入ArrayList中,在输出;

//将一个正整数分解质因子。例如:124=2*2*31。
public static void prime(int n) {
for(int i=2;i<=n/2;i++) {
if(n%i==0) {
System.out.print(i+"*");
prime(n/i);
}
}
System.out.print(n);
System.exit(0);
}

//输入两个正整数,求其最大公约数和最小公倍数
//利用斩除法(欧几里德算法)
//写法1
public static void gcdAndLcm1() {
System.out.println("请输入两个整数:");
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int a1 = a;
int b1 = b;
while(true) {

if((a=a%b)==0) {
System.out.println("最大公约数:" +b+"\n最小公倍数:"+a1*b1/b);
return;
}

if((b=b%a)==0) {
System.out.println("最大公约数:" +a+"\n最小公倍数:"+a1*b1/a);
return;
}
}
}

//写法2
public static void gcdAndLcm() {
System.out.println("请输入两个整数:");
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int a1 = a;
int b1 = b;
int temp,t;
if(a<b) {
temp = a;
a = b;
b = temp;
}
if(b==0) {
System.out.println("除数不能为0");
return;
}
while(a%b!=0) {
t = a%b;
a=b;
b=t;
}
System.out.println("最大公约数:" +b+"\n最小公倍数:"+a1*b1/b);
}

//输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
public static void statistics() {
System.out.println("请输入字符串:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
String e1 = "[a-zA-Z]";
String e2 = " ";
String e3 = "[0-9]";
int letterCount = 0;
int spaceCount = 0;
int digitalCount = 0;
int otherCount = 0;
char[] c = str.toCharArray();
String[] arrStr  = new String[c.length];
for(int i=0;i<c.length;i++) {
arrStr[i] = String.valueOf(c[i]);
}
for(int i=0;i<arrStr.length;i++) {
if(arrStr[i].matches(e1)) {
letterCount++;
}else if(arrStr[i].matches(e2)) {
spaceCount++;
}else if(arrStr[i].matches(e3)) {
digitalCount++;
}else{
otherCount++;
}
}
System.out.println("英文字母:"+letterCount+"\n空格:"+spaceCount+"\n数字:"+digitalCount+"\n其他:"+otherCount);
}
//插入排序
/*int []a = {1,4,2,7,5,9,98,-2,7979,6,987};
insertSort(a);
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+" ");
}*/
public static void insertSort(int[]a) {
int length = a.length;
int i,j;
for(i=1;i<length;i++) {
j = a[i];
while(i>0&&j<a[i-1]) {
a[i] = a[i-1];
i--;
}
a[i] = j;
}
}

//排序
public static void selectSort() {
System.out.println("请输入三个数:\n");
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
int j = scan.nextInt();
int z = scan.nextInt();
int l = i;
if(i>j){
l = i;
i=j;
j= l;
}
if(i>z){
l = i;
i=z;
z= l;
}
if(j>z){
l = z;
z=j;
j= l;
}
System.out.println(i+"," +j+","+z +"");
}
//完全平方
public static void perSqrt() {
long k = 0;
for(k=1;k<1000000L;k++) {
if((Math.floor(Math.sqrt(k+100))==Math.sqrt(k+100))&&(Math.floor(Math.sqrt(k+168))==Math.sqrt(k+168))) {
System.out.println(k);
}
}
}

//求利润
public static void testBonus(){
System.out.println("请输入利润(万元):");
Scanner scan = new Scanner(System.in);
Double p = scan.nextDouble();
Double bonus = 0.0;
if(p<=10){
bonus = p*0.1;
}else if(p<20) {
bonus = 10*0.1 + (p-10)*0.075;
}else if(p<40) {
bonus = 10*0.1 + 10*0.075 + (p-20)*0.05;
}else if(p<60) {
bonus = 10*0.1 + 10*0.075 + 20*0.05 + (p-40)*0.03;
}else if(p<=100) {
bonus = 10*0.1 + 10*0.075 + 20*0.05 + 20*0.03 + (p-60)*0.015;
}else {
bonus = 10*0.1 + 10*0.075 + 20*0.05 + 20*0.03 + 40*0.015 +(p-100)*0.01;
}
System.out.println(bonus);
}

//直接插入排序
//运行结果:2 3 5 9 12 34 67 75 100
public static void insertSort(int[]a,int n) {
int temp;
for(int i=2;i<n;i++) {
temp = a[i];
int j=i-1;
for(;a[j]>temp;j--) {
a[j+1] = a[j];
}
a[j+1] = temp;
}
}

//希尔排序
//这个排序写了很久,i必须是++,这样便于后面的遍历
public static void shellSort(int[]a,int n) {
int i,j,d,temp;
d=n/2;
while(d>0) {
for(i=d;i<n;i++) {
j=i-d;
for(;j>=0&&a[j]>a[j+d];j=j-d) {
temp = a[j];
a[j] = a[j+d];
a[j+d] = temp;
}
}
d=d/2;
}
}

//冒泡排序
public static void bubbleSort(int[]a,int n) {
int i,j,temp;
for(i=n;i>0;i--) {
for(j=0;j<i-1;j++) {
if(a[j]>a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}

//快速排序
//一定要加上j>i&&,不然没结果
public static void quickSort(int[]a,int s,int t) {
int i,j,temp;
i=s;j=t;
if(i<j) {
temp = a[i];
while(i!=j) {
while(j>i&&a[j]>temp) j--;
if(i<j){a[i]=a[j];i++;}
while(j>i&&a[i]<temp) i++;
if(i<j){a[j]=a[i];j--;}
}
a[i] = temp;
quickSort(a,s,i-1);
quickSort(a,i+1,t);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: