java面試筆試題彙總(陸續補充)
阿新 • • 發佈:2018-11-25
最近自己也在準備面試的一方面東西,遇到一些不太熟悉的地方我會陸續補充上來。
1.選擇排序和氣泡排序
/**
* 選擇排序
* @param arr
* @return
*/
public static int[] ChooseSort(int[] arr){
for(int i=0;i<arr.length;i++){
int min=i;
for(int j=i+1;j<arr.length;j++){
if(arr[min]>arr[j]){
min=j;
}
}
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
return arr;
}
/**
* 氣泡排序
* @param arr
* @return
*/
public static int[] BubbleSort(int[] arr){
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-1-i;j++){
if (arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return arr;
}
2.求10000個隨機數中找出最大1000個,寫程式碼
public static void main(String[] args) {
int[] arr=RandomCommon();
for (int i=0;i<1000;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]<arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<1000;i++){
System.out.print(arr[i]+",");
}
}
/**
* 得到1w個隨機數
* @return
*/
public static int[] RandomCommon(){
int max=100000; //最大隨機數
int min=1; //最小隨機數
int n=10000; //隨機數個數
int[] result=new int[n]; //一個長度10000的陣列
int count=0;
while(count<n){
int num=(int) (Math.random()*(max-min)+min);
boolean flag=true;
for(int i=0;i<result.length;i++){
if(result[i]==num){
flag=false;
break;
}
}
if(flag){
result[count]=num;
count++;
}
}
return result;
}
3.求出兩個偶數的素數(只能被1和本身整除的數)之和
public static void main(String[] args) {
System.out.println(PrimeSum(68)+PrimeSum(98));
}
public static int PrimeSum(int a){
int sum=0;
//素數的個數不確定,所以使用集合來存放素數
List<Integer> prime=new ArrayList<Integer>();
for(int i=2;i<=Math.sqrt(a);i++){
//這樣做可以減少迴圈次數,素數是因子為1和本身, 如果數c不是素數,則還有其他因子,其中的因子,假如為a,b.其中必有一個大於sqrt(c) ,一個小於sqrt(c) 。
while(true){
if(a%i==0){
a/=i;
prime.add(i);//元素存放到集合中
}else{
break; //if不是迴圈 跳出while迴圈
}
}
}
if(a!=1){
prime.add(a); //當最後a!=1的時候 即為最大的素數
}
//set的中不能存放重複的元素 使用set.add()判斷所插入的元素是否重複
Set set=new HashSet();
for (int i = 0; i < prime.size(); i++) {
if(set.add(prime.get(i))){
sum+=prime.get(i);
}
}
return sum;
}
4.求0-1000的數求平方根,保留小數位0.00001,不能借助jdk中現有的工具類
public static void main(String[] args) {
int n=555; //要求的數
double x =1;
double temp =1;
double result=0;
do{
temp = x; //儲存上一次計算的值
x = 0.5*(x + n/x); //這個就是牛頓迭代法的基本公式
result=x-temp;
result=result <= 0.0D ? 0.0D - result : result; //Math.abs去絕對值的原始碼方法 result是否比零小 小則取相反數
}while(result>0.00001); //精確度在5位小數以上
System.out.println(String.format("%.5f", x)); //只保留5位小數
}
5.有兩個數字A,B, 他們都是有500位的數字, 如何規定不能使用BigDecimal 去運算的話怎麼求的A+B的結果
public static void main(String[] args) {
String a="123345567789";
String b="987765543321";
System.out.println(a.charAt(11));//charAt得到第從0開始第11位的字元
int c=0;
LinkedList value=new LinkedList();
boolean isNeedCarry=false;
for(int i=a.length()-1;i>=0;i--){
boolean isAdded=false; //是否已經加上進位過來的1
c=Integer.parseInt(String.valueOf(a.charAt(i)))+Integer.parseInt(String.valueOf(b.charAt(i)));
if(isNeedCarry){ //如果進位標誌為true 則加上進位的1,且是否加1的標誌為true
isAdded=true;
value.addFirst(String.valueOf(c+1).substring(1)); //例:相加後的c為9,加上進位1等於10,擷取字串後取得數字位0,存入linkedlist中
c+=1;
}
isNeedCarry=isAddCarry(c); //第二次判斷相加後的c是否需要進位
if(isNeedCarry){ //如果需要進位 則進行字串擷取後一位存入linkedList
if(!isAdded){
value.addFirst(String.valueOf(c).substring(1));
}
}else{ //如果不需要進位,說明是個一位數,直接存入LinkedList
value.addFirst(String.valueOf(c));
}
if(isNeedCarry&&i==0){
//最後一次判斷是否進位,如果進位兩個12位相加則變成一個13位的數字
value.addFirst(1);
}
}
for(int i=0;i<value.size();i++){
System.out.print(value.get(i));
}
}
public static boolean isAddCarry(int i){
boolean flag=false;
if(i>=10){
flag=true;
}
return flag;
}
6.刪除一個資料夾中所有的檔案
public static void main(String[] args) {
DelAllFile("D:/downloads/ipsw");
}
/**
* 刪除檔案
* @param path
* @return
*/
public static boolean DelAllFile(String path){
boolean flag=false;
File file=new File(path);
if(!file.exists()){
return flag;
}
if(!file.isDirectory()){
//如果這個path表示的檔案是一個目錄則返回true
return flag;
}
String[] templist=file.list();//返回資料夾下的檔案目錄
File temp=null;
for(int i=0;i<templist.length;i++){
if(path.endsWith(File.separator)){//File.separator返回的是檔案的分隔符,在這裡是'/'
//path的最後有 '/' 直接進行路徑的拼接,temp為path路徑下的子檔案的路徑
temp=new File(path+templist[i]);
}else{
//path的最後沒有 '/' 進行路徑+'/'+子檔案的拼接,temp為path路徑下的子檔案的路徑
temp=new File(path+File.separator+templist[i]);
}
if(temp.isFile()){ //temp是檔案
temp.delete();
}
if(temp.isDirectory()){ //temp是目錄
DelAllFile(temp.getPath()); //先刪除下面的檔案
temp.delete(); //再刪除這個空資料夾
flag=true;
}
}
return flag;
}
7.序列化和反序列化
public static void main(String[] args) throws Exception {
test1();
test2();
}
/**
* 序列化
* @throws Exception
*/
public static void test1() throws Exception{
OutputStream op=new FileOutputStream("D:/downloads"+File.separator+"a.txt");
ObjectOutputStream oop=new ObjectOutputStream(op);
oop.writeObject(new Person("zhangsan", 12));
oop.close();
}
/**
* 反序列化
* @throws Exception
*/
public static void test2() throws Exception{
InputStream in=new FileInputStream("D:/downloads"+File.separator+"a.txt");
ObjectInputStream ois=new ObjectInputStream(in);
byte[] buffer=new byte[10];
int len=-1;
Person p=(Person) ois.readObject();
System.out.println(p);
ois.close();
}