第八届传智杯程序设计赛道模拟考
A 判断闰年
题目
给定一个整数 n,判断其是否为闰年。闰年的判定规则如下:
- 如果 n 能被 400 整除,则为闰年;
- 否则如果 n 能被 4 整除且不能被 100 整除,则为闰年;
- 否则,不是闰年。
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if((year%400==0)||((year%4==0)&&(year%100!=0))) {
System.out.println("yes");
}else {
System.out.println("no");
}
sc.close();
}
}
B 吃瓜群众
题目
群众想要吃瓜,于是给你一个瓜让你切,但是作为考验
告诉你西瓜的重量,问你能否将这个西瓜分成两部分,每个部分都是偶数。
注意:这里说的是能否分成两部分,不是能否平均分成两部分
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int weight = sc.nextInt();
if (weight==2) {
System.out.println("NO, you can't divide the watermelon into two even parts.");
}else {
if (weight%2==0) {
System.out.println("YES, you can divide the watermelon into two even parts.");
}else {
System.out.println("NO, you can't divide the watermelon into two even parts.");
}
}
sc.close();
}
}
C [CSP2019]数字游戏
题目
小 K 同学向小 P 同学发送了一个长度为 8 的 01 字符串来玩数字游戏,小 P 同学想 要知道字符串中究竟有多少个1。
注意:01 字符串为每一个字符是 0 或者 1 的字符串,如“101”(不含双引号)为一 个长度为 3 的 01 字符串。
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入
String s = sc.nextLine();
//计数器
int count=0;
char c[] = s.toCharArray();
//遍历查找
for(int i=0;i<8;i++) {
if (c[i]=='1') {
count++;
}
}
System.out.println(count);
sc.close();
}
}
D 字符金字塔
题目
请打印输出一个字符金字塔,字符金字塔的特征请参考样例
A
ABA
ABCBA
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
char c = s.charAt(0);
int len = c-'A'+1;
char c1 ;
for(int i=0;i<len;i++) {
for(int j=len-i-1;j>0;j--) {
System.out.print(" ");
}
for(int j=0;j<=i;j++) {
c1 = (char) ('A'+j);
System.out.print(c1);
}
for(int k=i-1;k>=0;k--) {
c1 = (char) ('A'+k);
System.out.print(c1);
}
System.out.println();
}
sc.close();
}
}
E 牛牛学数列
题目
牛牛开始学习数列啦。现在他想计算以下数列前 n 项的和:
请你计算并输出 S(n) 的值
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = 1,sum=0;
for (int i = 1; i <= n; i++) {
sum+=k*i;
k*=-1;
}
System.out.println(sum);
sc.close();
}
}
F [CQOI2010]扑克牌
题目
你有
种牌,第
种牌的数目为
。另外有
张特殊的 Joker 牌。
你有如下方法来组成一套牌:
- 不使用 Joker 牌,
种牌各一张;
- 使用一张 Joker 牌,其他
种牌各一张;
例如,当
时,一共有四种不同的组合方式:
现在,给出
、
和
,你的任务是组成尽量多套牌。每张牌最多只能用在一副套牌里(可以有牌不使用)。
代码
import java.util.Scanner;
import java.util.Arrays;
public class Main {
static boolean check(long mid,int m,int a[]) {
long need = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] < mid) {
need += mid - a[i];
}
}
return need <= m && need <= mid;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//读入数据
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
//排序
Arrays.sort(a);
//二分查找
long ans=0;
long L = 0;
long R = a[a.length-1]+m;
long mid;
while (L <= R) {
mid = L + (R - L) / 2;
if (check(mid,m,a)) {
L = mid + 1;
ans = mid;
} else {
R = mid - 1;
}
}
System.out.println(ans);
sc.close();
}
}