java算法n(java算法面试题)

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

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

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

在java中,用递归方法计算n的阶乘。

用Java求键盘输入的数的阶乘n。(递归算法)packagejiecheng; importjava.util.*;  //导入java.util包中的所有类classrep{ publiclongrep(intn){ longi=0; if(n==0||n==1) i=1;

elsi=n*rep(n-1)  returni; } } publicclassJie {  publicstaticvoidmain(String[] args) {  intn;  //此处定义要输入的数Scanner s = newScanner(System.in);  //以下三行用于n的值得输入System.out.print( "请输入一个整数:"); n = s.nextInt(); rep f= newrep(); System.out.println(n+"!="+f.rep(n)); } }

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

发展历程:20世纪90年代,硬件领域出现了单片式计算机系统,这种价格低廉的系统一出现就立即引起了自动控制领域人员的注意,因为使用它可以大幅度提升消费类电子产品(如电视机顶盒、面包烤箱、移动电话等)的智能化程度。

Sun公司为了抢占市场先机,在1991年成立了一个称为Green的项目小组,帕特里克、詹姆斯·高斯林、麦克·舍林丹和其他几个工程师一起组成的工作小组在加利福尼亚州门洛帕克市沙丘路的一个小工作室里面研究开发新技术,专攻计算机在家电产品上的嵌入式应用。

用java语言计算N的数字根

用java8的stream处理起来,代码更简洁些

可以参考:

public static void main(String[] args) {

    String str = "1782";

    while(str.length() != 1)

    {

        str = String.valueOf(Stream.of(str.split("")).mapToInt(Integer::valueOf).sum());

    }

    System.out.println(str);

}

中间那块函数含义也很简单

1. 先把这个字符分割成字符串数组

2. 字符串数组转化为对应stream

3. 把stream转化为IntStream

4. 直接用IntStream的sum方法求出和

5. 再把和转化为string,赋值给最开始的那个str

6. 然后判断str的长度是否为1,为1了就停止

java算法,例子N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟

需要使用多线程

package org.xuyh.design;

import java.util.Scanner;

/**

 * N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟

 * @author XuYanhang

 *

 */

public class AppleOperation implements Runnable{

public static final int ONE_SECOND = 1000;

private final int[] packages;

public final int operationTime;

public final int minSpacetTime;

private int appleCount;

private boolean timeEnd = false;

public AppleOperation(int appleCount,int packCount,int operationTime,int minSpacetTime){

  this.appleCount = appleCount;

  packages = new int[packCount];

  this.operationTime = operationTime;

  this.minSpacetTime = minSpacetTime;

 }

public int getAppleCount(){

  return appleCount;

 }

public int getPackCount(){

  return packages.length;

 }

public int getOperationTime(){

  return operationTime;

 }

public boolean hasEnd(){

  return timeEnd || appleCount == 0;

 }

public synchronized void setTimeEnd(){

  timeEnd = true;

 }

public void run() {

  int pack = Integer.parseInt(Thread.currentThread().getName());

  while(appleCount0){

   synchronized(this){

    if(hasEnd()){

     break;

    }

    appleCount--;

    packages[pack]++;

    System.out.println("一个苹果放入了第"+(pack+1)+"个袋子中,还剩"+appleCount+"个苹果");

   }

   for(int i = 0 ; i minSpacetTime ; i++){

    synchronized(this){

     if(hasEnd()){

      break;

     }

    }

    try {

     Thread.sleep(ONE_SECOND);

    } catch (InterruptedException e) {

     e.printStackTrace();

    }

   }

  }

  synchronized(this){

   System.out.println("第"+(pack+1)+"个袋子最终有"+packages[pack]+"个苹果");

  }

 }

//Test main method

 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);

  

  int appleCount = -1;

  do{

   System.out.print("N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟。\n"

     + "输入苹果总数N:");

   String string = scanner.next();

   if(null != string string.matches("^\\d{1,8}$")){

    appleCount = Integer.parseInt(string);

   }

  }while(appleCount == -1);

  

  scanner.close();

AppleOperation operation = new AppleOperation(appleCount,20,4*60*60,30*60);

new WaitThread(operation).start();

for (int i = 0; i operation.getPackCount(); i++) {

   

   Thread thread = new Thread(operation,""+i);

   

   thread.start();

   

  }

  

 }

static class WaitThread extends Thread{

private AppleOperation operation;

public WaitThread(AppleOperation operation){

   this.operation = operation;

  }

  

  public void run(){

   try {

    Thread.sleep(operation.getOperationTime()*ONE_SECOND);

   } catch (InterruptedException e) {

    e.printStackTrace();

   }

   

   operation.setTimeEnd();

System.out.println("时间到,苹果装袋操作结束,最后为装袋的苹果个数:"+operation.getAppleCount());

}

 }

}

这里我们额外启动了21个线程,其中一个是计4小时的定时的,其他20个是向每个袋子中放苹果的线程。

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)设计一个算法,计算出n阶乘中尾部零的个数

long 类型对大数阶乘来说存储范围大小,强行使用会溢出,可以使用 BigDecimal。

代码如下:

import java.math.BigDecimal;

public class Test3 {

// 设计一个算法,计算出n阶乘中尾部零的个数

public static void main(String[] args) {

BigDecimal n = getFactorial(25);

System.out.println(n);

int b = trailingZeros(25);

System.out.println(b);

}

public static int trailingZeros(int n) {

int x = 0;

BigDecimal factorial = getFactorial(n);

for (int i = 1; i = n; i++) {

if (factorial.remainder(new BigDecimal(Math.pow(10, i))) != BigDecimal.ZERO) {

x = i - 1;

break;

}

}

return x;

}

public static BigDecimal getFactorial(int n) {

BigDecimal result = new BigDecimal(1);

for(int i = 1; i = n; i++) {

result = result.multiply(new BigDecimal(i));

}

return result;

}

}

运行结果:

关于java算法n和java算法面试题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发布于 2023-04-11 02:04:09
收藏
分享
海报
23
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了