第八届传智杯程序设计赛道初赛(第一场)
A 蛇鸟
题目
蛇鸟是一种类似于蛇和鸟的可爱生物。 蛇鸟的主要食物是水果,每吃一个水果,它的长度就会增加1。水果离地面具有一定的高度,第i (1≤i≤N )个果实的高度为hi。 蛇鸟可以吃到小于等于其长度的水果。当蛇鸟的初始长度为L时,求它吃水果能达到的最大长度。
代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int L = in.nextInt();
int [] h = new int [N];
for(int i=0;i<N;i++) {
h[i] = in.nextInt();
}
Arrays.sort(h);
for(int j=0;j<N;j++) {
if (L>=h[j]) {
L++;
}
}
System.out.println(L);
in.close();
}
}
B 字符串解密
题目
给定一个由字符 ‘0’ 和 ‘1’ 构成的字符串 s。我们从字符串开头开始,按照长度依次取出以下子串并解析为十进制数:
- ∙长度 1 的子串
- ∙长度 2 的子串
- ∙长度 3 的子串
- ···
- 长度 10 的子串
然后循环回长度 1,依此类推;子串被取走后,视为删除,下一次从剩余的字符串中继续取子串,直到剩余字符不足以组成当前长度的子串
请输出解析得到的十进制数字序列
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
char c[] = s.toCharArray();
int L=1;
int i=0;
int count=1;
StringBuffer result = new StringBuffer();
while(i+L<=s.length()) {
String substring = s.substring(i,i+L);
int decimalVal = Integer.parseInt(substring,2);
if (result.length()>0) {
result.append(" ");
count++;
}
result.append(decimalVal);
i += L;
L = (L%10)+1;
}
System.out.println(count);
System.out.println(result.toString());
scanner.close();
}
}
写不下去了,太难辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣辣