一個小時,3道題,A出1.7道,最後一道沒時間看。
題目一(100%)
/**
* 隨着製造工藝的提升,藍廠工人生產手機的效率隨着時間的推移逐漸增加,第一天每天生產一部,接着的兩天每天生產2部,接着的三天每天生產3部手機,以此類推
* 要求輸入一個天數n,返回到那天總共生產的手機的數量。
*/
public class Solution {
/**
* @param n int整型 第n天
* @return int整型
*/
public int solution(int n) {
//產品總數
int sum = 0;
int temp = 0;
//當前日期每天能夠生產的手機的數量
int phone = 1;
for (int i = 0; i < n; i++) {
if (temp == phone) {
temp = 0;
phone++;
}
temp++;
sum += phone;
}
return sum;
}
}
題目二(70%)
/**
* 現給定任意正整數n,請尋找並輸出最小的正整數m(m>9),使得m的各位之乘積爲n,若不存在輸出-1。
*/
public class Solution {
public int solution(int n) {
//返回的結果值
int num = 0;
//數組的下標
int index = 0;
//模擬的數組
int[] m = new int[10];
//乘積結果
int res = n;
//結果的最小的位數
int weishu = 2;
int q = 9;
int s = 0;
boolean marked = false;
//通過循環計算最小的位數
while (res > 9 * q) {
weishu++;
q *= 9;
}
int r = weishu - 1;
while (weishu != 0) {
for (int j = 9; j >= 1 && !marked; j--) {
//乘積的結果
s = res;
//如果結果能被當前的數整除
if (res % j == 0) {
marked = true;
//將這個數記錄在數組中
m[index++] = j;
res /= j;
}
}
if (s == res && res >= 10)
return -1;
--weishu;
marked = false;
}
//此時數組中存儲着從大到小的能夠整除乘積結果的數字
for (;r>=0;r--){
//將當前數組存儲的結果轉化爲數字
num = num * 10 + m[r];
}
return num;
}
}
題目三(0%)
/**
* 現在有一個3X3的智能手機解鎖程序,要求輸入兩個正整數m和n,計算出最少使用m和鍵,最多使用n個鍵的解鎖方式。
* 1.每個模式必須連接至少m個鍵和最多n個鍵
* 2.所有的鍵都必須是不同的
* 3.如果在模式中連接兩個連續鍵的行通過任何其他鍵,則其他鍵必須在模式中選擇,不允許跳過非選擇鍵。
*/