下面是某些企业常见的算法面试试题,现总结如下,仅供学员参考与学习。
1.比较两个字符串如果不等返回True?
答案:
Java代码 复制代码
package com.test.kaoshi;
public class StringDemo {
private static String a = "abc";
private static String b = "abcg";
public static boolean equalString() {
if (a.equals(b)) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
StringDemo sd = new StringDemo();
System.out.println("主要考察返回Boolean变量和字符串比较使用的方法?+sd.equalString());
}
}
[java] view plain copy
package com.test.kaoshi;
public class StringDemo {
private static String a = "abc";
private static String b = "abcg";
public static boolean equalString() {
if (a.equals(b)) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
StringDemo sd = new StringDemo();
System.out.println("主要考察返回Boolean变量和字符串比较使用的方法?+sd.equalString());
}
}
2.随机产生20个字符并且排序?
答案:
Java代码 复制代码
package com.test.kaoshi;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class RadomDemo {
/**
* 随机产生20个字符串并且字符串不能重复 且进行排序
* @param random
* @param len
* @return
*/
public Set getChar(){
Set numberSet01 = new HashSet();
Random rdm = new Random();
char ch;
while(numberSet01.size()<20){
int rdGet = Math.abs(rdm.nextInt())%26+97;//产生97到122的随机数a-z值
ch=(char)rdGet;
numberSet01.add(ch);
//Set中是不能放进重复的值的,当它有20个时,就满足你的条件了
}
return numberSet01;
}
public static void main(String[] args) {
RadomDemo rd = new RadomDemo();
Set numberSet01=rd.getChar();
Set numberSet = new TreeSet();
numberSet.addAll(numberSet01);
for(Iterator it=numberSet01.iterator();it.hasNext();){
System.out.print(it.next());
}
System.out.println();
for(Iterator it=numberSet.iterator();it.hasNext();){
System.out.print(it.next());
}
}
}
[java] view plain copy
package com.test.kaoshi;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class RadomDemo {
/**
* 随机产生20个字符串并且字符串不能重复 且进行排序
* @param random
* @param len
* @return
*/
public Set getChar(){
Set numberSet01 = new HashSet();
Random rdm = new Random();
char ch;
while(numberSet01.size()<20){
int rdGet = Math.abs(rdm.nextInt())%26+97;//产生97到122的随机数a-z值
ch=(char)rdGet;
numberSet01.add(ch);
//Set中是不能放进重复的值的,当它有20个时,就满足你的条件了
}
return numberSet01;
}
public static void main(String[] args) {
RadomDemo rd = new RadomDemo();
Set numberSet01=rd.getChar();
Set numberSet = new TreeSet();
numberSet.addAll(numberSet01);
for(Iterator it=numberSet01.iterator();it.hasNext();){
System.out.print(it.next());
}
System.out.println();
for(Iterator it=numberSet.iterator();it.hasNext();){
System.out.print(it.next());
}
}
}
3.50个人围成一圈数到三和三的倍数时出圈,问剩下的人是谁?在原来的位置是多少?
答案:
Java代码 复制代码
package com.test.kaoshi;
import java.util.Iterator;
import java.util.LinkedList;
public class YouXi {
public static int removeNM(int n, int m) {
LinkedList ll = new LinkedList();
for (int i = 0; i < n; i++)
ll.add(new Integer(i + 1));
int removed = -1;
while (ll.size() > 1) {
removed = (removed + m) % ll.size();
ll.remove(removed--);
}
return ((Integer) ll.get(0)).intValue();
}
public static void main(String[] args) {
System.out.println(removeNM(50, 3));
}
}
资讯来源说明:本文章来自网络收集,如侵犯了你的权益,请联系QQ:850873385进行删除。
Copyright © 2014-2018 500d.me. All Rights Reserved.