面试题-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!=j&&k!=j&&i!=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(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.给定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);
}
面试题-JAVA算法题的更多相关文章
- 一道java算法题分析
最近在面试中遇到这样的一道算法题: 求100!的结果的各位数之和为多少? 如:5!=5*4*3*2*1=120,那么他们的和为1+2+0=3这道题不算难,不过倒是注意的细节也有 ...
- 面试-java算法题
1.编写一个程序,输入n,求n!(用递归的方式实现). public static long fac(int n){ if(n<=0) return 0; else if(n==1) retur ...
- 【JAVA算法题】职业抢劫
题目 /*You are a professional robber planning to rob houses along a street. * Each house has a certain ...
- 25道经典Java算法题
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题 [Java] 纯 ...
- 50道java算法题(一)
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1 ...
- JAVA面试题——JAVA编程题1(2015.07.22——湛耀)
实现代码很简单: package com.xiaozan.shopping; import java.util.Arrays; public class ShoppingCart { ...
- 趣味Java算法题(附答案)
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每一个月都生一对兔子,小兔子长到第三个月后每一个月又生一对兔子,假如兔子都不死,问每一个月的兔子总数为多少? //这是一个菲波拉契 ...
- 蓝桥网试题 java 算法训练 区间k大数查询
-------------------------------------------------------------------------- 数组也有sort方法 尽量把输入和操作分开写 -- ...
- 某厂java算法题实现及改进【有n个人成一圈,顺序排号(编号为1到n),从第一个人开始报数1到3报数】
一.第一种实现: 实现比较简单,直接贴现成的代码了,第一种实现: /** * 总人数 * * @param d */ private static void sortQuerry1(int d) { ...
随机推荐
- EC2(elastic compute cloud,弹性计算云,又称EC2实例)
(一)定义:EC2和实例EC2(elastic compute cloud,弹性计算云),即云中的虚拟服务器. 是用于在云中创建和运行虚拟机的 Amazon Web 服务.简言之,EC2就是一部具有无 ...
- 启动Jmeter4.0 后弹出警告: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0 x80000002. Windows RegCreateKeyEx(...) returned error code 5.
启动Jmeter4.0 后弹出命令窗口提示信息: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at r ...
- abap form格式的异常处理
1: form 中有异常时,需要用 raising 将异常抛出. *&------------------------------------------------------------- ...
- python web框架介绍对比
Django Python框架虽然说是百花齐放,但仍然有那么一家是最大的,它就是Django.要说Django是Python框架里最好的,有人同意也有人 坚决反对,但说Django的文档最完善.市场占 ...
- 拖拽控件java版
Button vv = new Button("vvvv"); DragSource.getDefaultDragSource().createDefaultDragGestur ...
- [LeetCode] 607. Sales Person_Easy tag: SQL
Description Given three tables: salesperson, company, orders.Output all the names in the table sales ...
- cocos2d JS-(JavaScript) 动态生成方法的例子
function User(properties) { for (var i in properties) { (function (which) { var p = i; which["g ...
- oracle 常用(二)
多表查询: 1.等值连接查询: select a.aa,a.bb,b.qq from XX a , CC b where a.aa= b.ee 2.不等值连接: select * fr ...
- nodejs中安卓端的编码如何转换为中文
借助一些模块来转换,比如,html-entities Github var Entities = require('html-entities').XmlEntities; entities = ne ...
- activiti_change_ok
package com.xinwei.process.controller; import java.util.Calendar; import java.util.HashMap; import j ...