3.28 奇虎360笔试大题
第一题直接暴力就好了。。
package A360;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
/**
* @author fancy
* @date 2021/3/28 19:32
*/
public class Test1 {
static int a[] = new int[50010];
static int n;
static long ans;
public static void main(String[] args) throws IOException {
StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
re.nextToken(); n = (int)re.nval;
for(int i = 1;i <= n;i++){
re.nextToken();
a[i] = (int)re.nval;
ans += a[i];
}
for(int i = 1;i <= n;i++)
for(int j = i+1;j <= n;j++)
ans += a[i]|a[j];
System.out.println(ans);
}
}
package A360;
我也没想到暴力也可以过。。
import java.util.Scanner;
/**
* @author fancy
* @date 2021/3/28 19:40
*/
public class Test2 {
static int N,M,K;
static long ans;
static int a[] = new int[110];
static int b[] = new int[110];
// pre : 上一个点是在哪里 cur:当前准备飞到哪个点 cnt:当前飞了几次 sum:当前金币数
static void dfs(int pre,int cur,int cnt,int sum){
ans = Math.max(ans,sum);
if(cnt == K) return;
if(cur > N) return;
if(a[cur]-a[pre] > M) return;
dfs(pre,cur+1,cnt,sum);
dfs(cur,cur+1,cnt+1,sum+b[cur]);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();M = sc.nextInt();K = sc.nextInt();
for(int i = 1;i <= N;i++){
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
// 上一个点是1 准备飞第2个点 一次都还没飞 金币数第一个点的金币数
dfs(1,2,0,b[1]);
System.out.println(ans);
}
}
#360公司##笔试题目#
