「java经典问题算法大全」java 问题

博主:adminadmin 2023-03-17 16:15:10 246

今天给各位分享java经典问题算法大全的知识,其中也会对java 问题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java递归算法

1.汉诺塔问题

import javax.swing.JOptionPane;

public class Hanoi {

private static final String DISK_B = "diskB";

private static final String DISK_C = "diskC";

private static final String DISK_A = "diskA";

static String from=DISK_A;

static String to=DISK_C;

static String mid=DISK_B;

public static void main(String[] args) {

String input=JOptionPane.showInputDialog("please input the number of the disks you want me move.");

int num=Integer.parseInt(input);

move(num,from,mid,to);

}

private static void move(int num, String from2, String mid2, String to2) {

if(num==1){

System.out.println("move disk 1 from "+from2+" to "+to2);

}

else {

move(num-1,from2,to2,mid2);

System.out.println("move disk "+num+" from "+from2+" to "+to2);

move(num-1,mid2,from2,to2);

}

}

}

2. 这是一个排列的例子,它所做的工作是将输入的一个字符串中的所有元素进行排序并输出,例如:你给出的参数是"abc" 则程序会输出:

abc

acb

bac

bca

cab

cba

(1)算法的出口在于:low=high也就是现在给出的排列元素只有一个时。

(2)算法的逼近过程:先确定排列的第一位元素,也就是循环中i所代表的元素,

然后low+1开始减少排列元素,如此下去,直到low=high

public static void permute(String str) {

char[] strArray = str.toCharArray();

permute(strArray, 0, strArray.length - 1);

}

public static void permute(char[] list, int low, int high) {

int i;

if (low == high) {

String cout = "";

for (i = 0; i = high; i++)

cout += list[i];

System.out.println(cout);

} else {

for (i = low; i = high; i++) {

char temp = list[low];

list[low] = list[i];

list[i] = temp;

permute(list, low + 1, high);

temp = list[low];

list[low] = list[i];

list[i] = temp;

}

}

}

3。这是一个组合的例子,与上述的例子相似,只是它所做的工作是,输出所给字符串中制定数目的元素的组合种类

(1)程序出口在于n=1,此时只要输出目标数组的所有元素即可

(2)逼近过程,当n1 的时候,我们先取第一个元素放入目标数组中,然后n-1,如此下去,最后出来。

import javax.swing.JOptionPane;

public class Combination {

/**

* @param args

*/

public static void main(String[] args) {

String input = JOptionPane.showInputDialog("please input your String: ");

String numString = JOptionPane.showInputDialog("please input the number of your Combination: ");

int num = Integer.parseInt(numString);

Combine(input, num);

}

private static void Combine(String input, int num) {

char[] a = input.toCharArray();

String b = "";

Combine(a, num, b, 0, a.length);

}

private static void Combine(char[] a, int num, String b, int low, int high) {

if (num == 0) {

System.out.println(b);

} else {

for (int i = low; i a.length; i++) {

b += a[i];

Combine(a, num - 1, b, i+1, a.length);

b=b.substring(0, b.length()-1);

}

}

}

}

java算法问题

以数字的形式保留x位小数 java是做不到的,因为double和float都无法精确精度。

但是如果你想输出的时候或已其他对象模拟精确的小数是可以做到的。

那么你的第一个数组就无法是基本类型的数组

我的建议的,首先遍历2个数组X,Y,暂声明length为数组长度,遍历时通过DecimalFormat对象

将X数组中的值封装为String类型,利用Map中的键唯一的特性来过滤重复。

考虑到你可能需要保序,所以使用LinkedMap来存储

DecimalFormat formater = new DecimalFormat("0.0"); //格式化用

MapString,Double X_Y_Location = new LinkedHashMap();

for(int i=0;ilength;i++)

{

    String keyX = formater.format(X[i]).toString();

    if(!X_Y_Location.containsKey(keyX))

        X_Y_Location.put(keyX,Y[i]);

}

System.out.println(X_Y_Location);//打印结果测试

求 Java 一些经典例子算法

1:HelloWorldApp

1.//

2.//

HelloWorld

应用示例

3.//

4.public

class

HelloWorldApp{

5.

public

static

void

main

(String

args[])

{

6.System.out.println

("Hello

World!");

7.

}

8.}

以上程序行是在你的屏幕上打印“Hello

World!”所需的最少代码。

java实现几种常见排序算法

下面给你介绍四种常用排序算法:

1、冒泡排序

特点:效率低,实现简单

思想(从小到大排):每一趟将待排序序列中最大元素移到最后,剩下的为新的待排序序列,重复上述步骤直到排完所有元素。这只是冒泡排序的一种,当然也可以从后往前排。

2、选择排序

特点:效率低,容易实现。

思想:每一趟从待排序序列选择一个最小的元素放到已排好序序列的末尾,剩下的位待排序序列,重复上述步骤直到完成排序。

3、插入排序

特点:效率低,容易实现。

思想:将数组分为两部分,将后部分元素逐一与前部分元素比较,如果当前元素array[i]小,就替换。找到合理位置插入array[i]

4、快速排序

特点:高效,时间复杂度为nlogn。

采用分治法的思想:首先设置一个轴值pivot,然后以这个轴值为划分基准将待排序序列分成比pivot大和比pivot小的两部分,接下来对划分完的子序列进行快排直到子序列为一个元素为止。

Java简单算法问题

初步做了一个出来,但是效率并不是很高,前100个计算速度还可以,但是往后就很慢了。如果什么时候有空的话可以再看看,先给你代码吧,不知道能不能帮上你

public class AlisandaNumber {

private static final int MAX_INDEX = 1000; // 可以先把这个常量改为1-6,验证正确性

public static void main(String[] args) {

int a = 0;

int index = 0;

while(index  MAX_INDEX) {

a += 6; // 每次循环自增6,由题目规律可知A是6的倍数

boolean breakOut = false;

// 最大的约数为此数的平方根,因为如果是两个平方根相乘的话,剩下的就只有1了

int maxNum = (int) Math.ceil(Math.sqrt(a));

p:

for(int p = 1; p = maxNum; p ++) {

if(a % p != 0) {

continue; // 如果不是约数的话,没必要考虑,下同

}

// 最大约数为平方根的相反数,原理同上

maxNum = (int) Math.ceil(Math.sqrt(a / p));

for(int q = -1; q = -maxNum; q --) { // q和r必为负数

if(a % q != 0) {

continue;

}

int r = a / (p * q);

int nonZero = p * q + p * r + q * r;

if (nonZero == 0) {

continue;

}

if((a == p * q * r)  (a == (p * q * r) / (nonZero))) {

index ++;

breakOut = true;

break p; // 跳出外层循环

}

}

}

if(breakOut) {

System.out.println(String.format("第%d个压力山大数是%d", index, a));

}

}

}

}

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经典问题算法大全和java 问题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。