恒生电子0331暑假实习笔试算法题题解
总的来说,这次笔试没什么难度,第一题暴力,第二题DFS暴力。第二题数据很弱,有些不对的写法都能ac
代码没有格式,可以移步查看恒生电子0331暑假实习笔试算法题
第一题
找到从11-n的所有超完全数,并从大到小排序
超完全数:n的长度为len,n的每位数的len次方之和等于n
比如:153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1534
直接暴力求解即可
```java
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
for(int i = Math.min(10000000, n); i >= 11; i--){
if(fun(i)) {
System.out.println(i);
}
}
}
public static boolean fun(int n){
String s = n + "";
int len = s.length();
int sum = 0;
for(char c : s.toCharArray()) {
int temp = c - '0';
sum += (int)Math.pow(temp, len);
if(sum > n) {
return false;
}
}
return n == sum;
}
}
```
或者直接用数组输出
```java
import java.io.*;
public class Main {
static int[] t = new int[]{153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818,9800817, 9926315};
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
for(int i = 14; i >= 0; i--) {
if(t[i] < n) {
System.out.println(t[i]);
}
}
}
}
```
第二题
买卖股票:DFS之后,返回最大值即可。
```java
import java.util.*;
public class Solution {
/**
*
* 输入:10000,7,[1.0, 2.0, 1.0,2.0, 2.0, 3.0, 2.0],2 输出50000.00000
* 注解:第一天1元买入10000股,第二天卖出20000元
* 第三天1元买入20000股,第六天卖出60000元,盈利50000元
*
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 根据输入计算最大收益
* @param M double浮点型 初始资金
* @param N int整型 历史价格天数
* @param historyPrices double浮点型一维数组 N天历史价格
* @param K int整型 最大允许交易次数
* @return double浮点型
*/
double max = 0;
double yuanlai = 0;
double[] history;
public double get_max_profit (double M, int N, double[] historyPrices, int K) {
// write code here
yuanlai = M;
history = historyPrices;
dfs(0, M, K, 0);
return max - yuanlai;
}
public void dfs(int at, double have, int k, int num) {
max = Math.max(have, max);
if(k == 0 && num == 0) {
return;
}
if(at == history.length) {
return;
}
dfs(at + 1, have, k, num); //不买也不卖
if(num != 0) { //全卖
double mai = num * history[at]; //卖出的钱
dfs(at + 1, have + mai, k, 0);
}
if(have >= history[at]) { //全买
int mai = (int)(have / history[at]);
dfs(at + 1, have - mai * history[at], k - 1, num + mai);
}
}
}
```
代码没有格式,可以移步查看恒生电子0331暑假实习笔试算法题
第一题
找到从11-n的所有超完全数,并从大到小排序
超完全数:n的长度为len,n的每位数的len次方之和等于n
比如:153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1534
直接暴力求解即可
```java
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
for(int i = Math.min(10000000, n); i >= 11; i--){
if(fun(i)) {
System.out.println(i);
}
}
}
public static boolean fun(int n){
String s = n + "";
int len = s.length();
int sum = 0;
for(char c : s.toCharArray()) {
int temp = c - '0';
sum += (int)Math.pow(temp, len);
if(sum > n) {
return false;
}
}
return n == sum;
}
}
```
或者直接用数组输出
```java
import java.io.*;
public class Main {
static int[] t = new int[]{153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818,9800817, 9926315};
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
for(int i = 14; i >= 0; i--) {
if(t[i] < n) {
System.out.println(t[i]);
}
}
}
}
```
第二题
买卖股票:DFS之后,返回最大值即可。
```java
import java.util.*;
public class Solution {
/**
*
* 输入:10000,7,[1.0, 2.0, 1.0,2.0, 2.0, 3.0, 2.0],2 输出50000.00000
* 注解:第一天1元买入10000股,第二天卖出20000元
* 第三天1元买入20000股,第六天卖出60000元,盈利50000元
*
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 根据输入计算最大收益
* @param M double浮点型 初始资金
* @param N int整型 历史价格天数
* @param historyPrices double浮点型一维数组 N天历史价格
* @param K int整型 最大允许交易次数
* @return double浮点型
*/
double max = 0;
double yuanlai = 0;
double[] history;
public double get_max_profit (double M, int N, double[] historyPrices, int K) {
// write code here
yuanlai = M;
history = historyPrices;
dfs(0, M, K, 0);
return max - yuanlai;
}
public void dfs(int at, double have, int k, int num) {
max = Math.max(have, max);
if(k == 0 && num == 0) {
return;
}
if(at == history.length) {
return;
}
dfs(at + 1, have, k, num); //不买也不卖
if(num != 0) { //全卖
double mai = num * history[at]; //卖出的钱
dfs(at + 1, have + mai, k, 0);
}
if(have >= history[at]) { //全买
int mai = (int)(have / history[at]);
dfs(at + 1, have - mai * history[at], k - 1, num + mai);
}
}
}
```