1.编写一个程序,输入n,求n!(用递归的方式实现)。

  1. public static long fac(int n){
  2. if(n<=0) return 0;
  3. else if(n==1) return 1;
  4. else return n*fac(n-1);
  5. }
  6. public static void main(String [] args) {
  7. System.out.println(fac(6));
  8. }

2.编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

3.编写一个程序,将text1.txt文件中的单词与text2.txt文件中的单词交替合并到text3.txt文件中。text1.txt文件中的单词用回车符分隔,text2.txt文件中用回车或空格进行分隔。

  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4.  
  5. public class text{
  6. public static void main(String[] args) throws Exception{
  7. String[] a = getArrayByFile("text1.txt",new char[]{'\n'});
  8. String[] b = getArrayByFile("text2.txt",new char[]{'\n',' '});
  9. FileWriter c = new FileWriter("text3.txt");
  10. int aIndex=0;
  11. int bIndex=0;
  12.  
  13. while(aIndex<a.length){
  14. c.write(a[aIndex++] + "\n");
  15. if(bIndex<b.length)
  16. c.write(b[bIndex++] + "\n");
  17. }
  18.  
  19. while(bIndex<b.length){
  20. c.write(b[bIndex++] + "\n");
  21. }
  22. c.close();
  23. }
  24.  
  25. public static String[] getArrayByFile(String filename,char[] seperators) throws Exception{
  26. File f = new File(filename);
  27. FileReader reader = new FileReader(f);
  28. char[] buf = new char[(int)f.length()];
  29. int len = reader.read(buf);
  30. String results = new String(buf,0,len);
  31. String regex = null;
  32. if(seperators.length >1 ){
  33. regex = "" + seperators[0] + "|" + seperators[1];
  34. }else{
  35. regex = "" + seperators[0];
  36. }
  37. return results.split(regex);
  38. }
  39.  
  40. }

4.639172每个位数上的数字都是不同的,且平方后所得数字的所有位数都不会出现组成它自身的数字。(639172*639172=408540845584),类似于639172这样的6位数还有几个?分别是什么?

这题采用的HashMap结构判断有无重复,也可以采用下题的数组判断。

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

5.比如,968548+968545=321732732它的答案里没有前面两个数里的数字,有多少这样的6位数。

  1. public void selectNum(){
  2. for(int n = 10; n <= 99;n++){
  3. for(int m = 10; m <= 99;m++){
  4. if(isRepeat(n,m)){
  5. continue;
  6. }
  7. else{
  8. System.out.println("组合是"+n+","+m);
  9. }
  10. }
  11. }
  12. }
  13.  
  14. public boolean isRepeat(int n,int m){
  15. int[] a={0,0,0,0,0,0,0,0,0,0};
  16. int s=n+m;
  17. while(n!=0){
  18. a[n%10]=1;
  19. n=n/10;
  20. }
  21.  
  22. while(m!=0){
  23. a[m%10]=1;
  24. m=m/10;
  25. }
  26.  
  27. while(s!=0){
  28. if(a[s%10]==1){
  29. return true;
  30. }
  31. s=s/10;
  32. }
  33. return false;
  34. }
  35.  
  36. public static void main(String args[]){
  37. new test().selectNum();
  38. }

6.给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。

  1. public static void main(String [] args) {
  2. int count = 0;
  3. String str="hello world hello hi";
  4. String newStr="";
  5. HashMap<String,String> m=new HashMap<String,String>();
  6. String [] a=str.split(" ");
  7. for (int i=0;i<a.length;i++){
  8. if(!m.containsKey(a[i])){
  9. m.put(a[i],"1");
  10. count++;
  11. newStr=newStr+" "+a[i];
  12. }
  13. }
  14. System.out.println("这段短文单词的个数是:"+count+","+newStr);
  15. }

7.写出程序运行结果。

  1. public class Test1 {
  2. private static void test(int[]arr) {
  3. for (int i = 0; i < arr.length; i++) {
  4. try {
  5. if (arr[i] % 2 == 0) {
  6. throw new NullPointerException();
  7. } else {
  8. System.out.print(i);
  9. }
  10. }
  11. catch (Exception e) {
  12. System.out.print("a ");
  13. }
  14. finally {
  15. System.out.print("b ");
  16. }
  17. }
  18. }
  19.  
  20. public static void main(String[]args) {
  21. try {
  22. test(new int[] {0, 1, 2, 3, 4, 5});
  23. } catch (Exception e) {
  24. System.out.print("c ");
  25. }
  26. }
  27.  
  28. }

运行结果:a b 1b a b 3b a b 5b

  1. public class Test1 {
  2. private static void test(int[]arr) {
  3. for (int i = 0; i < arr.length; i++) {
  4. try {
  5. if (arr[i] % 2 == 0) {
  6. throw new NullPointerException();
  7. } else {
  8. System.out.print(i);
  9. }
  10. }
  11.  
  12. finally {
  13. System.out.print("b ");
  14. }
  15. }
  16. }
  17.  
  18. public static void main(String[]args) {
  19. try {
  20. test(new int[] {0, 1, 2, 3, 4, 5});
  21. } catch (Exception e) {
  22. System.out.print("c ");
  23. }
  24. }
  25.  
  26. }

运行结果:b c

8.单词数

统计一篇文章里不同单词的总数。

Input

  有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output

  每组值输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input

you are my friend

#

Sample Output

4

  1. public static void main(String [] args) {
  2. List<Integer> countList=new ArrayList<Integer>();
  3. int count;
  4. HashMap<String,String> m;
  5. String str; //读取键盘输入的一行(以回车换行为结束输入)
  6. String[] a;
  7.  
  8. Scanner in=new Scanner(System.in);
  9.  
  10. while( !(str=in.nextLine()).equals("#") ){
  11. a=str.split(" ");
  12. m=new HashMap<String,String>();
  13. count = 0;
  14. for (int i=0;i<a.length;i++){
  15. if(!m.containsKey(a[i]) && (!a[i].equals(""))){
  16. m.put(a[i],"1");
  17. count++;
  18. }
  19. }
  20. countList.add(count);
  21. }s
  22.  
  23. for(int c:countList)
  24. System.out.println(c);
  25. }

面试-java算法题的更多相关文章

  1. 一道java算法题分析

    最近在面试中遇到这样的一道算法题:       求100!的结果的各位数之和为多少?       如:5!=5*4*3*2*1=120,那么他们的和为1+2+0=3这道题不算难,不过倒是注意的细节也有 ...

  2. 面试经典算法题集锦——《剑指 offer》小结

    从今年 3 月份开始准备找实习,到现在校招结束,申请的工作均为机器学习/数据挖掘算法相关职位,也拿到了几个 sp offer.经历这半年的洗礼,自己的综合能力和素质都得到了一个质的提升. 实话说对于未 ...

  3. 面试 | Java 算法的 ACM 模式

    (Java 算法的 ACM 模式) 前言 经常在 LeetCode 上用核心代码模式刷题的小伙伴突然用 ACM 模式可能会适应不过来,把时间花在输入输出上很浪费时间,因此本篇笔记对 Java 算法的 ...

  4. Java面试常见算法题

    1.实现字符串反转 提供七种方案实现字符串反转 import java.util.Stack; public class StringReverse { public static String re ...

  5. 几个面试经典算法题Java解答

    题目一: public class testClockwiseOutput { //顺时针打印一个矩阵 @Test public void test(){ int[][] num = new int[ ...

  6. 【JAVA算法题】职业抢劫

    题目 /*You are a professional robber planning to rob houses along a street. * Each house has a certain ...

  7. 25道经典Java算法题

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   //这是一个菲波拉契数列问题 [Java] 纯 ...

  8. 50道java算法题(一)

    [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析:   兔子的规律为数列1 ...

  9. 趣味Java算法题(附答案)

    [程序1]    题目:古典问题:有一对兔子,从出生后第3个月起每一个月都生一对兔子,小兔子长到第三个月后每一个月又生一对兔子,假如兔子都不死,问每一个月的兔子总数为多少?    //这是一个菲波拉契 ...

随机推荐

  1. Linux目录结构介绍-http://yangrong.blog.51cto.com/6945369/1288072

    1.树状目录结构图 2./目录 目录 描述 / 第一层次结构的根.整个文件系统层次结构的根目录. /bin/ 需要在单用户模式可用的必要命令(可执行文件):面向所有用户,例如:cat.ls.cp,和/ ...

  2. locate 命令详解

    locate :http://www.cnblogs.com/peida/archive/2012/11/12/2765750.html 作用:locate命令可以在搜寻数据库时快速找到档案,数据库由 ...

  3. jingtai ip

    BOOTPROTO=staticONBOOT=yesIPADDR=192.168.1.109NETMASK=255.255.255.0GATEWAY=192.168.1.1 HWADDR=00:e0: ...

  4. Webpack 2 视频教程 016 - Webpack 2 中生成 SourceMaps

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  5. 如何将外部的obj模型导入OpenGL

    1.关于obj的说明. obj中存放的是顶点坐标信息(v),面的信息(f),法线(vn),纹理坐标(vt),以及材质(这个放在mtl)中 我使用CINEMA 4D导出用VS查看后的信息: CINEMA ...

  6. Java中使用LocalDate根据日期来计算年龄

    Java中和日期直接相关的类有很多,平时最常用到的就是java.util package下面的Date和Calendar,需要用到格式的时候还会用到java.text.SimpleDateFormat ...

  7. JSP的三种注释方式

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...

  8. springboot mybatis 事务管理

    本文主要讲述springboot提供的声明式的事务管理机制. 一.一些概念 声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优 ...

  9. svn conflict 冲突解决

    1. 同一处修改文件冲突 开发人员都知道代码管理工具是开发中一个必不可少的工具,这里也不废话详细介绍了.不管你个人喜欢git还是svn还是其他,但还有一大部分公司在使用svn做代码管理工具.这里详细介 ...

  10. C# ASP.NET 转换为int型的方法 很实用

    很多新手在搞c#或者.net开发的时候总会碰到一些小问题,如何知道字符能不能为int型  在这里我写了一个小的函数仅供大家参考: /// <summary> /// 判断是不是int型 / ...