第一题
import java.util.Scanner;
public class NewMain {
static int comp(int n){
if (n>36 || n<1){
return 0;
}
else if (n==1){
return 1;
}else if (n==2){
return 2;
}
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = 1;
for (int i=2;i<=n;i++){
dp[i] = dp[i-1]+dp[i-2];
}
return dp[n];
}
public static void main(String[] args) {
//输入:1 1
//输出:2
// Scanner in = new Scanner(System.in);
// while (in.hasNextInt()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
// int a = in.nextInt();
// int b = in.nextInt();
// System.out.println(a + b);
// }
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
int a = in.nextInt();
int res = comp(a);
System.out.println(res);
}
}
}
第二题
# @param person int整型一维数组
# @return int整型
#
class Solution:
def house(self , person ):
# write code here
res = []
count = 0
for i in enumerate(person):
count += 1
if count>1:
if i[1] < person[i[0]-1]:
if count == 2:
res[i[0]-1] = res[i[0]-1]+1
res.append(1)
elif i[1] > person[i[0]-1]:
res.append(res[i[0]-1]+1)
elif i[1] == person[i[0]-1]:
res.append(1)
else:
res.append(1)
return sum(res)
全部评论
(3) 回帖