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个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

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

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(aIndex<a.length){
c.write(a[aIndex++] + "\n");
if(bIndex<b.length)
c.write(b[bIndex++] + "\n");
} while(bIndex<b.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结构判断有无重复,也可以采用下题的数组判断。

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

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="";
HashMap<String,String> m=new HashMap<String,String>();
String [] a=str.split(" ");
for (int i=0;i<a.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) {
List<Integer> countList=new ArrayList<Integer>();
int count;
HashMap<String,String> m;
String str; //读取键盘输入的一行(以回车换行为结束输入)
String[] a; Scanner in=new Scanner(System.in); while( !(str=in.nextLine()).equals("#") ){
a=str.split(" ");
m=new HashMap<String,String>();
count = 0;
for (int i=0;i<a.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算法题的更多相关文章

  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. ArcGIS API for JavaScript 4.2学习笔记[7] 鹰眼(缩略图的实现及异步处理、Promise、回调函数、监听的笔记)

    文前说明:关于style就是页面的css暂时不做评论,因为官方给的例子的样式实在太简单了,照抄阅读即可. 这篇文章有着大量AJS 4.x版本添加的内容,如监听watch.Promise对象.回调函数. ...

  2. bzoj 2752: [HAOI2012]高速公路(road)

    Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站.Y901高速公路是一条由N-1段路以及N个收 ...

  3. 阅读MDN文档之层叠与继承(二)

    目录 The cascade Importance Specificity Source order A note on rule mixing Inheritance Controlling inh ...

  4. 活动倒计时-兼容ios

    最近要做一个活动,需要用倒计时,写好之后再IOS上无效,经过百度知道了,原来IOS不能识别格式"2017-11-09 --",所以要进行转换才有效 直接上代码了: <!DOC ...

  5. [数据清洗]- Pandas 清洗“脏”数据(三)

    预览数据 这次我们使用 Artworks.csv ,我们选取 100 行数据来完成本次内容.具体步骤: 导入 Pandas 读取 csv 数据到 DataFrame(要确保数据已经下载到指定路径) D ...

  6. solr 的 field, copyfield ,dynamic field

    Field: Field就是一个字段,定义一个Field很简单: <field name="price" type="sfloat" indexed=&q ...

  7. vue 自定义组件 v-model双向绑定、 父子组件同步通信

    父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa"  子组件使用$emit('update:msg', 'msg改变后的值xxx ...

  8. JS 对象API之修改、删除对象的属性

    无论是修改还是删除对象的属性,我们首先要清楚:自有属性.共有属性的处理方法肯定是不同的: 先创建一个对象实例 var obj = { name: '小马扎', age: }; Object.proto ...

  9. 小白的Python之路 day5 random模块和string模块详解

    random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...

  10. 小子给大家分享一个或者多个新手创建tableview经常会遇到的坑(动态创建控件,xib的重用)

    小子最近做了一个根据接口返回的数据在Cell中动态创建控件,感觉应该会一部分人卡在这里,小子就跟大家分享一下: 1.控件重复创建:这个问题出现的原因是动态创建的cell内容的时候,无法进行重用设置,所 ...