華爲這部分的機試題我是從最後開始刷的:
1、求最小公倍數
最小公倍數 = 兩數之積除以最大公約數
最大公約數的我採用了輾轉相除法和更相減損法這兩種,當然也可以窮舉。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int d=getMaxCommonDivisor1(a,b);
System.out.println((a*b)/d);
}
/**
* 求最大公約數 輾轉相除法
* @param m
* @param n
* @return
*/
private static int getMaxCommonDivisor(int m,int n) {
if(n<=0){
return m;
}
int b= m%n;
m=n;
n=b;
return getMaxCommonDivisor(m,n);
}
/**
* 求最大公約數 更相減損法
* @param m
* @param n
* @return
*/
private static int getMaxCommonDivisor1(int m,int n) {
if(m==n){
return m;
}
if(m>n){
m=m-n;
}
if(m<n){
n=n-m;
}
return getMaxCommonDivisor1(m,n);
}
}
2、求解立方根
•計算一個數字的立方根,不使用庫函數
詳細描述:
•接口說明
原型:
public static double getCubeRoot(double input)
輸入:double 待求解參數
返回值:double 輸入參數的立方根,保留一位小數
輸入描述:
待求解參數 double類型
輸出描述:
輸入參數的立方根 也是double類型
示例1
輸入
216
輸出
6.0
第一種方法:
牛頓迭代法。設f(x)=x3-y, 求f(x)=0時的解x,即爲y的立方根。
根據牛頓迭代思想,
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b= getCubeRoot(a);
System.out.printf("%.1f", b);
}
public static double getCubeRoot(double input) {
double x = 1.0;
while (Math.abs(x * x * x - input) > 1e-9)
x = x - ((x * x * x - input) / (3 * x * x));
return x;
}
}
第二種方法:
二分查找法 下面的這個寫法雖然通過測試用例了,但是這個是有問題的,現在這種寫法只適用與大於1的正數。
這個思想感覺還是可以的,不過需要進一步完善
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b= getCubeRoot1(a);
System.out.printf("%.1f", b);
}
public static double getCubeRoot1(double input) {
double max = input;
double min = 0;
double mid = 0;
while ((max - mid) >= 0.0001) {// 注意,這裏的精度要提高一點,否則某些測試用例無法通過
if (mid * mid * mid > input) {
max = mid;
}
if (mid * mid * mid < input) {
min = mid;
}
if (mid * mid * mid == input) {
return mid;
}
mid = (max + min) / 2.0;
}
return max;
}
}
3、字符串分割
連續輸入字符串(輸出次數爲N,字符串長度小於100),請按長度爲8拆分每個字符串後輸出到新的字符串數組,
長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。
首先輸入一個整數,爲要輸入的字符串個數。
例如:
輸入:2
abc
12345789
輸出:abc00000
12345678
90000000
接口函數設計如下:
/*****************************************************************************
功能:存儲輸入的字符創
輸入:字符串
輸出:無
返回:0表示成功,其它返回-1
******************************************************************************/
int AddString(char *strValue);
/****************************************************************************
功能:獲取補位後的二維數組的長度
輸入:無
輸出:無
返回:二維數組長度
*****************************************************************************/
int GetLength();
/*****************************************************************************
功能:將補位後的二維數組,與輸入的二維數組做比較
輸入:strInput:輸入二維數組,iLen:輸入的二維數組的長度
輸出:無
返回:若相等,返回0;不相等,返回-1.其它:-1;
******************************************************************************/
int ArrCmp(char strInput[][9],int iLen);
輸入描述:
首先輸入數字n,表示要輸入多少個字符串。連續輸入字符串(輸出次數爲N,字符串長度小於100)。
輸出描述:
按長度爲8拆分每個字符串後輸出到新的字符串數組,長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。
示例1
輸入
2
abc
123456789
輸出
abc00000
12345678
90000000
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
int n= sc.nextInt();
for(int i=0;i<n;i++){
String temp=sc.next();
while(temp.length()%8!=0) {
temp+= "0";
}
for(int j=0; j<temp.length(); j+=8) {
System.out.println(temp.substring(j, j+8));
}
}
}
}
}
4、