200 points question
1 Number not containing 101 TODO
It’s a bit difficult to get full marks for this one. Let’s write about violence first. It is said that violence is worth 120 points.
java
import java.util.*;
//不含101
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int r = sc.nextInt();
int cnt = 0;
for(int i = l ; i <= r; i ++ ) {
String s = Integer.toBinaryString(l);
if(!s.contains("101")) cnt ++ ;
}
System.out.println(cnt);
}
}
2 Take out as few balls as possible
java
import java.util.*;
//取出尽量少的球
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = sc.nextInt();//球的总数
int n = sc.nextInt();//桶的个数, 数组长度
int[] ints = new int[n];
int cnt = 0;
int max_num = 0;
for(int i = 0; i < n; i ++ ) {
ints[i] = sc.nextInt();
cnt += ints[i];
max_num = Math.max(ints[i], max_num);
}
//球小于sum的情况
if(cnt <= sum) {
System.out.println("[]");
return;
}
//超过sum的情况
int max_Capacity = 0;//求最大的容量 max_Capacity 满足 总数 < sum
//容量的范围:
int min = sum / n;//取整ok
int max = max_num;
int total = 0;
for(int i = max; i >= min; i -- ) {
for(int j = 0; j < n; j ++ ) {
// if(ints[j] > i)
// total += i;
// else
// total += ints[j];
total += Math.min(i, ints[j]);
}
if(total <= sum) {
for(int j = 0; j < n; j ++ ) {
ints[j] = Math.max(0,ints[j] - i);
}
System.out.println(Arrays.toString(ints));
return;
}
total = 0;
}
}
}
6 points bonus
I got this question haha
Simple
java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//10000人
int[] self = new int[n];//员工分到的随机金额
for(int i = 0; i < n; i ++ ){
int sj = sc.nextInt();//随机的值 1-100000
self[i] = sj;
}
for(int i = 0; i < n - 1; i ++ ){
int res = 0;
boolean flag = false;
for(int j = i + 1; j < n; j ++ ) {
if(self[i] < self[j]) {//往后的每一个数字都遍历一下
res = (j - i) * (self[j] - self[i]);
flag = true;
break;
}
}
if(!flag) res = self[i];
System.out.print(res);
System.out.print(" ");
flag = false;
}
System.out.println(self[n-1]);
}
}