这就是没理解这道题考察的意图,不是考察你javaAPI的使用,而是看看你的思维和代码编程能力。开发工程师的主要工作就是处理各种逻辑。比如给你一个真实的工作需求,让你把一个数据作排序,但是相同的数只保留两个,或者给一个字符串按第个字母进行排序。只会使用API或者粘贴复制是远远不够的,而排序算法是逻辑最直接的,最好表达,也是行数较少的思维考查,所以笔试面试里见面的次数就比较多。
Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。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=1i<=4i++) for(j=1j<=4j++)for(k=1k<=4k++){ 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.639172每个位数上的数字都是不同的,且平方后所得数字的所有位数都不会出现组成它自身的数字。(639172*639172=408540845584),类似于639172这样的6位数还有几个?分别是什么?
这题采用的HashMap结构判断有无重复,也可以采用下题的数组判断。
public void selectNum(){
for(long n = 100000n <= 999999n++){
if(isSelfRepeat(n))//有相同的数字,则跳过
continue
else if(isPingFangRepeat(n*n,n)){//该数的平方中是否有与该数相同的数字
continue
}else{//符合条件,则打印 System.out.println(n)
}
}
}public boolean isSelfRepeat(long n){
HashMap<Long,String>m=new HashMap<Long,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){
HashMap<Long,String>m=new HashMap<Long,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 = 10n <= 99n++){
for(int m = 10m <= 99m++){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=0i<a.lengthi++){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 = 0i <arr.lengthi++) {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 = 0i <arr.lengthi++) {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=0i<a.lengthi++){if(!m.containsKey(a[i]) &&(!a[i].equals(""))){
m.put(a[i],"1")
count++
}
}
countList.add(count)
}sfor(int c:countList)
System.out.println(c)
}
1. 努力学习Java知识:为了能够通过Java面试,程序员小白首先需要努力学习Java基础知识,包括Java语法、面向对象编程思想、泛型、集合、多线程、IO流、数据结构、算法等。
2. 加强实践:学习完Java基本知识之后,程序员小白需要加强实践,多编写一些小程序来实践,以此来检验自己学习的知识是否正确。
3. 掌握数据结构与算法:在准备面试的过程中,程序员小白还需要掌握数据结构和算法,这些基础知识会让一个程序员变得更强。
4. 熟悉Java框架:要想通过Java面试,程序员小白也需要熟悉常用的Java框架,如Spring、Hibernate、Struts2等,这些框架也是面试经常考查的知识点。
5. 掌握设计模式:设计模式是Java程序员很重要的知识点,因此程序员小白在准备面试的时候也要学习常用的设计模式,这样才能应对面试官的提问。