第十一届蓝桥杯真题解析JavaC组
蓝桥杯题目
- ***第十一届蓝桥杯真题解析JavaC组***
- 前言
- 1.指数计算
- 2.解密
- 3.跑步训练
- 4.合并检测
- 5.REPEAT程序
- 6.分类计数
- 7.整除序列
- 8.走方格
- 9.字符串编码
- 10.整数小拼接
- 总结
前言
写这些除了是我自己能够更好的学习,记录下自己的学习笔记;还有是为了能够帮助和我一样热爱学习的人。
提示:以下是本篇文章正文内容,下面案例可供参考
1.指数计算
本题总分:5分
问题描述:
7 月 1 日是建党日,从 1921 年到 2020 年,中国*已经带领中国人民
走过了 99 年。
请计算:7 ^ 2020 mod 1921,其中 A mod B 表示 A 除以 B 的余数。
答案提交:
这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
480
这道题是方程简单的,就是开局给你自信的。
只需要使用到BigInteger这个类。如果不知道的话,可以去网上搜一下或者有jdk1.8的可以去查一查。
还有一种方法:是使用电脑上的计算器可实现(有点取巧啊,哈哈)
代码如下(示例):
import java.math.BigInteger;
public class Eleven1_指数计算 {
public static void main(String[] args) {
BigInteger a = new BigInteger("7");
BigInteger b = a.pow(2020).mod(new BigInteger("1921"));
System.out.println(b);
//结果为480
//这下面的是我自己对这些方法的尝试,为了熟悉
// BigInteger c = a.pow(2);
// System.out.println(c);
// BigInteger d = c.mod(new BigInteger("8"));
// System.out.println(d);
}
}
2.解密
本题总分:5分
题目描述:
小明设计了一种文章加密的方法:对于每个字母c,将它变成某个另外的字符Tc。下表给出了字符变换的规则:
例如,将字符串 YeRi 加密可得字符串 EaFn。
小明有一个随机的字符串,加密后为
EaFnjISplhFviDhwFbEjRjfIBBkRyY
(由 30 个大小写英文字母组成,不包含换行符),请问原字符串是多少?
(如果你把以上字符串和表格复制到文本文件中,请务必检查复制的内容
是否与文档中的一致。在试题目录下有一个文件 str.txt,第一行为上面的字符
串,后面 52 行依次为表格中的内容。)
答案提交:
这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
只包含 30 个大小写英文字母的字符串,在提交答案时只填写这个字符串,填写
多余的内容将无法得分。
YeRikGSunlRzgDlvRwYkXkrGWWhXaA
这道题也有取巧的地方:就是根据题目给的表一个一个的对过去(不过一定要仔细!)
当然,上面这个方法是鉴于题目给出的数据小。
使用代码也不难,只要掌握了Map集合的运用即可。
代码如下(示例):
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Eleven2_解密 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next(); //加密之后的字符串
//使用Map集合。原因是:这里是成对出现的
Map<String, String> map = new HashMap<String, String>();
//循环52次:因为大小写字母共52个
for (int i = 0; i < 52; i++) {
String s1 = scanner.next(); //加密前的字符
String s2 = scanner.next(); //加密后的字符
map.put(s2, s1);//加密后的为键,加密前的为值。因为我们已知的是加密后的
}
String str2 = "";
//循环字符串
for (int i = 0; i < str.length(); i++) {
str2 += map.get(str.charAt(i) + ""); //拿到加密前的字符 拿到之后做拼接
}
System.out.println(str2);
}
}
3.跑步训练
本题总分:10分
题目描述:
小明要做一个跑步训练。
初始时,小明充满体力,体力值计为 10000。如果小明跑步,每分钟损耗
600 的体力。如果小明休息,每分钟增加 300 的体力。体力的损耗和增加都是
均匀变化的。
小明打算跑一分钟、休息一分钟、再跑一分钟、再休息一分钟……如此循
环。如果某个时刻小明的体力到达 0,他就停止锻炼。
请问小明在多久后停止锻炼。为了使答案为整数,请以秒为单位输出答案。
答案中只填写数,不填写单位。
答案提交:
这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
3880
思路:
可以看出,奇数时,就是在跑步;偶数时,就是在休息。
要注意,不一定会刚刚好用完体力,而剩余的体力照样可以继续跑步
由题中可推算出,每秒钟跑步消耗10体力
推算:10000 / (600 - 300) = 33.33…
32 * (600 - 300) = 9600
剩:400
总时间:32 * 2 * 60 + 400 / 10 = 3880
所以这道题除了可使用代码实现,还可以直接计算出来
注意:题目输出的最终结果是秒,不是分钟!
代码如下(示例):
public class Eleven3_跑步训练 {
//初始体力
static int power = 10000;
public static void main(String[] args) {
int sum = 0;//统计跑了多久时间(分钟)
boolean juge = true;
for (int i = 1;; i++) {
if(power < 600 && juge) {
break;
}
//跑步
if(i % 2 != 0 && juge) {
power -= 600;
juge = false;
}else { //休息
power += 300;
juge = true;
}
sum++;
}
System.out.println(sum * 60 + power / 10);
}
}
4.合并检测
本题总分:10分
题目描述:
新冠疫情由新冠病毒引起,最近在 A 国蔓延,为了尽快控制疫情,A 国准
备给大量民众进病毒核酸检测。
然而,用于检测的试剂盒紧缺。
为了解决这一困难,科学家想了一个办法:合并检测。即将从多个人(k
个)采集的标本放到同一个试剂盒中进行检测。如果结果为阴性,则说明这 k
个人都是阴性,用一个试剂盒完成了 k 个人的检测。如果结果为阳性,则说明
至少有一个人为阳性,需要将这 k 个人的样本全部重新独立检测(从理论上看,
如果检测前 k − 1 个人都是阴性可以推断出第 k 个人是阳性,但是在实际操作中
不会利用此推断,而是将 k 个人独立检测),加上最开始的合并检测,一共使用
了 k + 1 个试剂盒完成了 k 个人的检测。
A 国估计被测的民众的感染率大概是 1%,呈均匀分布。请问 k 取多少能
最节省试剂盒?
答案提交:
这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
10
代码如下(示例):
public class Eleven4_合并检测 {
/**
* 这里是学习视频上的
* 思路:由题意得每一百个人中至少有一个人是被感染的,所以由此可推出k的取值范围为[1,100)。
* 假设A国有n个人,每k个人为一组
* n/k为所分的组数
* n/100为感染的人数
* 所以感染的人均匀分布在n/k这么多组里面
*
* 假设一组中只有一个感染的人(最坏的情况)
* 所需要的个数:
* n/k(表示一开始每组都需要一个) + n/100*k(最坏的情况下,需要这么多试剂盒)
* n(1/k + k/100)
* @param args
*/
public static void main(String[] args) {
int allPerson = 1000;//随机确定的人数
int num = 0;//试剂盒的个数
int minx = 9999999;//试剂盒最少的个数
int answer = 0;//答案
for (int k = 1; k < allPerson; k++) {
if(allPerson % k == 0) {
num = (allPerson / k) + (int) Math.floor(0.01 * allPerson * k);
}else {
num = (allPerson / k) + (int) Math.floor(0.01 * allPerson * k + 1);
}
//判断出最小试剂盒的情况就ok了
if(num < minx) {
minx = num;
// System.out.println(num);
answer = k;
}
}
System.out.println(answer);
print();
}
public static void print() {
int people = 10000;//假设需要检测的人数
int min = people;//最少要用到的试剂盒
int k = 1;
int m = (int)(people * 0.01);//感染人数
for (int i = 2; i <= people; i++) { //每i个人检测一次
int count = people / i;//表示第一次检测所需的试剂盒
if(people % i != 0) count++;
if(m >= count) { //感染人数大于检测盒子数目,表明每个盒子都是阳性
count += people;
}else {
count += m * i;
}
if(count < min) {
k = i;
min = count;
}
}
System.out.println(k);
}
}
这道题我做的时候是弄了好久才搞懂的。
其实只要get到那个点就可以了。现在再重新看这道题还是会出现一下子懵逼的情况,唉,还是太菜了!
5.REPEAT程序
本题总分:15分
题目描述:
附件 prog.txt 中是一个用某种语言写的程序。
其中 REPEAT k 表示一个次数为 k 的循环。循环控制的范围由缩进表达,
从次行开始连续的缩进比该行多的(前面的空白更长的)为循环包含的内容。
例如如下片段:
REPEAT 2:
A = A + 4
REPEAT 5:
REPEAT 6:
A = A + 5
A = A + 7
A = A + 8
A = A + 9
该片段中从 A = A + 4 所在的行到 A = A + 8 所在的行都在第一行的循环两次中。
REPEAT 6: 所在的行到 A = A + 7 所在的行都在 REPEAT 5: 循环中。
A = A + 5 实际总共的循环次数是 2 × 5 × 6 = 60 次。
请问该程序执行完毕之后,A 的值是多少?
答案提交:
这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
241830
如果这道题只是用举例中的那是很简单的,只要将其改成for循环即可。
但是题目给出的是一千多行的,所以不可能这么写。
这道题可以使用Python的,但是我不会。
代码如下(示例):
import java.util.ArrayList;
import java.util.List;
public class Eleven5_REPEAT程序 {
public static void main(String[] args) {
//先实现举例中的
int A = 0;
for (int i = 0; i < 2; i++) {
A = A + 4;
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 6; k++) {
A = A + 5;
}
A = A + 7;
}
A = A + 8;
}
A = A + 9;
System.out.println(A);
//例子的最终结果是403
print();
}
//最终结果为241830
/**
* 方法:替换
* 这道题使用的方法是将它们进行替换。替换成for循环中的语句,最后将替换后得到的for循环运行一下,输出结果
* 这是通过看视频的学习到的,需要重新写一遍(不借助任何)
*
* 注:貌似可以使用正则表达式来实现,但是我不会,然后网上的我看不懂。哎,罢了罢了,把这个吸收了吧!
*/
public static void print() {
// String str1 = "REPEAT 2:\n" +
// " A = A + 4\n" +
// " REPEAT 5:\n" +
// " REPEAT 6:\n" +
// " A = A + 5\n" +
// " A = A + 7\n" +
// " A = A + 8\n" +
// "A = A + 9\n";
String str1 = "REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" REPEAT 5:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 5\r\n" +
" A = A + 7\r\n" +
" REPEAT 6:\r\n" +
" A = A + 7\r\n" +
" REPEAT 4:\r\n" +
" A = A + 2\r\n" +
" A = A + 7\r\n" +
" A = A + 2\r\n" +
" REPEAT 7:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 8\r\n" +
" A = A + 7\r\n" +
" A = A + 4\r\n" +
" A = A + 5\r\n" +
" A = A + 8\r\n" +
" REPEAT 8:\r\n" +
" A = A + 5\r\n" +
" REPEAT 1:\r\n" +
" A = A + 2\r\n" +
" REPEAT 7:\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" REPEAT 2:\r\n" +
" REPEAT 3:\r\n" +
" A = A + 1\r\n" +
" A = A + 1\r\n" +
" REPEAT 5:\r\n" +
" A = A + 1\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 5\r\n" +
" A = A + 1\r\n" +
" REPEAT 6:\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" REPEAT 2:\r\n" +
" A = A + 5\r\n" +
" REPEAT 3:\r\n" +
" A = A + 9\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" REPEAT 2:\r\n" +
" A = A + 9\r\n" +
" REPEAT 1:\r\n" +
" A = A + 6\r\n" +
" A = A + 6\r\n" +
" A = A + 4\r\n" +
" REPEAT 3:\r\n" +
" A = A + 7\r\n" +
" A = A + 1\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" REPEAT 5:\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
" A = A + 4\r\n" +
" A = A + 3\r\n" +
"REPEAT 4:\r\n" +
" A = A + 4\r\n" +
" A = A + 3\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 5\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 3\r\n" +
" REPEAT 3:\r\n" +
" A = A + 3\r\n" +
" A = A + 1\r\n" +
" A = A + 8\r\n" +
" A = A + 2\r\n" +
" REPEAT 9:\r\n" +
" A = A + 5\r\n" +
" REPEAT 1:\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
"A = A + 6\r\n" +
"REPEAT 3:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 9\r\n" +
" REPEAT 5:\r\n" +
" A = A + 2\r\n" +
" A = A + 1\r\n" +
" REPEAT 9:\r\n" +
" A = A + 9\r\n" +
" A = A + 2\r\n" +
" REPEAT 1:\r\n" +
" A = A + 6\r\n" +
" A = A + 8\r\n" +
" REPEAT 2:\r\n" +
" A = A + 9\r\n" +
" A = A + 4\r\n" +
" A = A + 7\r\n" +
" REPEAT 2:\r\n" +
" REPEAT 7:\r\n" +
" A = A + 3\r\n" +
" A = A + 5\r\n" +
" REPEAT 3:\r\n" +
" A = A + 5\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" A = A + 4\r\n" +
" REPEAT 9:\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
" A = A + 2\r\n" +
" A = A + 3\r\n" +
" REPEAT 2:\r\n" +
" REPEAT 8:\r\n" +
" A = A + 5\r\n" +
" A = A + 1\r\n" +
" A = A + 6\r\n" +
" A = A + 1\r\n" +
" A = A + 2\r\n" +
" REPEAT 6:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 3\r\n" +
" REPEAT 1:\r\n" +
" A = A + 2\r\n" +
" REPEAT 4:\r\n" +
" A = A + 7\r\n" +
" A = A + 1\r\n" +
" A = A + 8\r\n" +
" REPEAT 6:\r\n" +
" A = A + 5\r\n" +
" REPEAT 6:\r\n" +
" A = A + 3\r\n" +
" REPEAT 2:\r\n" +
" A = A + 2\r\n" +
" A = A + 9\r\n" +
" A = A + 7\r\n" +
" REPEAT 9:\r\n" +
" A = A + 8\r\n" +
" REPEAT 9:\r\n" +
" A = A + 8\r\n" +
" A = A + 9\r\n" +
" A = A + 3\r\n" +
" A = A + 2\r\n" +
" REPEAT 6:\r\n" +
" A = A + 3\r\n" +
" REPEAT 9:\r\n" +
" A = A + 1\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" A = A + 9\r\n" +
" A = A + 8\r\n" +
" REPEAT 5:\r\n" +
" A = A + 6\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" A = A + 2\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 3:\r\n" +
" A = A + 4\r\n" +
" REPEAT 7:\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" REPEAT 8:\r\n" +
" A = A + 9\r\n" +
" A = A + 8\r\n" +
" REPEAT 4:\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" A = A + 3\r\n" +
"A = A + 5\r\n" +
"REPEAT 6:\r\n" +
" A = A + 7\r\n" +
"REPEAT 7:\r\n" +
" A = A + 2\r\n" +
" A = A + 2\r\n" +
"A = A + 1\r\n" +
"REPEAT 8:\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 6\r\n" +
" A = A + 6\r\n" +
" A = A + 2\r\n" +
" REPEAT 5:\r\n" +
" A = A + 4\r\n" +
" A = A + 8\r\n" +
" A = A + 4\r\n" +
" REPEAT 1:\r\n" +
" A = A + 5\r\n" +
" REPEAT 7:\r\n" +
" A = A + 8\r\n" +
" REPEAT 6:\r\n" +
" A = A + 4\r\n" +
" A = A + 4\r\n" +
" A = A + 8\r\n" +
" REPEAT 4:\r\n" +
" A = A + 2\r\n" +
" REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" REPEAT 1:\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
" REPEAT 2:\r\n" +
" A = A + 7\r\n" +
" REPEAT 8:\r\n" +
" A = A + 6\r\n" +
" A = A + 1\r\n" +
" A = A + 7\r\n" +
" REPEAT 8:\r\n" +
" A = A + 2\r\n" +
" REPEAT 8:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 1\r\n" +
" A = A + 6\r\n" +
" REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" A = A + 1\r\n" +
" A = A + 7\r\n" +
" A = A + 4\r\n" +
"REPEAT 4:\r\n" +
" REPEAT 9:\r\n" +
" A = A + 2\r\n" +
" REPEAT 1:\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
"REPEAT 8:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 3\r\n" +
" REPEAT 4:\r\n" +
" A = A + 1\r\n" +
" A = A + 6\r\n" +
" A = A + 1\r\n" +
" REPEAT 7:\r\n" +
" A = A + 7\r\n" +
" REPEAT 7:\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" A = A + 9\r\n" +
" REPEAT 3:\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" A = A + 6\r\n" +
" A = A + 2\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" REPEAT 2:\r\n" +
" A = A + 7\r\n" +
" REPEAT 1:\r\n" +
" A = A + 7\r\n" +
" REPEAT 4:\r\n" +
" A = A + 7\r\n" +
" A = A + 2\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" A = A + 9\r\n" +
" REPEAT 5:\r\n" +
" A = A + 5\r\n" +
" REPEAT 1:\r\n" +
" A = A + 6\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" A = A + 2\r\n" +
" A = A + 6\r\n" +
" A = A + 8\r\n" +
" A = A + 8\r\n" +
" A = A + 7\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
"REPEAT 2:\r\n" +
" A = A + 1\r\n" +
" A = A + 7\r\n" +
"A = A + 3\r\n" +
"REPEAT 2:\r\n" +
" A = A + 7\r\n" +
"A = A + 1\r\n" +
"A = A + 4\r\n" +
"REPEAT 1:\r\n" +
" REPEAT 7:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
" A = A + 6\r\n" +
" A = A + 1\r\n" +
"A = A + 2\r\n" +
"A = A + 4\r\n" +
"A = A + 9\r\n" +
"REPEAT 1:\r\n" +
" A = A + 8\r\n" +
"REPEAT 8:\r\n" +
" REPEAT 4:\r\n" +
" REPEAT 8:\r\n" +
" A = A + 4\r\n" +
" REPEAT 3:\r\n" +
" A = A + 1\r\n" +
" A = A + 8\r\n" +
" REPEAT 7:\r\n" +
" A = A + 8\r\n" +
" REPEAT 7:\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" REPEAT 7:\r\n" +
" A = A + 6\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 3\r\n" +
" REPEAT 4:\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" A = A + 4\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 3:\r\n" +
" A = A + 4\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" A = A + 3\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" REPEAT 6:\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" A = A + 1\r\n" +
" A = A + 2\r\n" +
" A = A + 6\r\n" +
" A = A + 6\r\n" +
" REPEAT 9:\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" A = A + 7\r\n" +
" REPEAT 3:\r\n" +
" A = A + 6\r\n" +
" REPEAT 5:\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" REPEAT 9:\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" REPEAT 5:\r\n" +
" A = A + 8\r\n" +
" A = A + 8\r\n" +
" REPEAT 3:\r\n" +
" A = A + 7\r\n" +
" A = A + 9\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
"REPEAT 6:\r\n" +
" A = A + 9\r\n" +
"A = A + 1\r\n" +
"REPEAT 4:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 7\r\n" +
" REPEAT 9:\r\n" +
" A = A + 2\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
"A = A + 2\r\n" +
"A = A + 8\r\n" +
"A = A + 7\r\n" +
"A = A + 9\r\n" +
"A = A + 6\r\n" +
"REPEAT 4:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" REPEAT 3:\r\n" +
" A = A + 4\r\n" +
" A = A + 4\r\n" +
"REPEAT 6:\r\n" +
" A = A + 6\r\n" +
"A = A + 1\r\n" +
"A = A + 5\r\n" +
"A = A + 8\r\n" +
"REPEAT 2:\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 2\r\n" +
" REPEAT 3:\r\n" +
" A = A + 1\r\n" +
" REPEAT 1:\r\n" +
" A = A + 8\r\n" +
" A = A + 7\r\n" +
" A = A + 4\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
" A = A + 4\r\n" +
" REPEAT 5:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 8\r\n" +
" REPEAT 9:\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" REPEAT 5:\r\n" +
" A = A + 5\r\n" +
" REPEAT 3:\r\n" +
" REPEAT 5:\r\n" +
" A = A + 4\r\n" +
" REPEAT 4:\r\n" +
" A = A + 6\r\n" +
" A = A + 3\r\n" +
" REPEAT 7:\r\n" +
" A = A + 3\r\n" +
" A = A + 3\r\n" +
" A = A + 1\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" A = A + 6\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" A = A + 9\r\n" +
" A = A + 3\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 1\r\n" +
" REPEAT 8:\r\n" +
" A = A + 5\r\n" +
" REPEAT 8:\r\n" +
" A = A + 6\r\n" +
" REPEAT 4:\r\n" +
" A = A + 9\r\n" +
" A = A + 4\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 7\r\n" +
" A = A + 5\r\n" +
" A = A + 8\r\n" +
" A = A + 7\r\n" +
" A = A + 8\r\n" +
" A = A + 5\r\n" +
" REPEAT 2:\r\n" +
" A = A + 5\r\n" +
" A = A + 7\r\n" +
" A = A + 8\r\n" +
"A = A + 5\r\n" +
"A = A + 9\r\n" +
"REPEAT 2:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" A = A + 8\r\n" +
" A = A + 7\r\n" +
" A = A + 1\r\n" +
" A = A + 5\r\n" +
" REPEAT 3:\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" A = A + 7\r\n" +
" REPEAT 3:\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" REPEAT 6:\r\n" +
" A = A + 1\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 7:\r\n" +
" A = A + 3\r\n" +
" REPEAT 5:\r\n" +
" A = A + 5\r\n" +
" A = A + 8\r\n" +
" A = A + 8\r\n" +
" A = A + 1\r\n" +
" A = A + 2\r\n" +
" REPEAT 4:\r\n" +
" A = A + 6\r\n" +
" REPEAT 3:\r\n" +
" A = A + 3\r\n" +
" A = A + 7\r\n" +
" REPEAT 8:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 7\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" A = A + 1\r\n" +
"A = A + 2\r\n" +
"A = A + 4\r\n" +
"A = A + 7\r\n" +
"REPEAT 1:\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" A = A + 6\r\n" +
" REPEAT 9:\r\n" +
" A = A + 1\r\n" +
" A = A + 6\r\n" +
" REPEAT 5:\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" A = A + 7\r\n" +
" A = A + 2\r\n" +
" REPEAT 2:\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" REPEAT 7:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 6\r\n" +
" A = A + 8\r\n" +
" REPEAT 6:\r\n" +
" A = A + 6\r\n" +
" REPEAT 2:\r\n" +
" A = A + 1\r\n" +
" A = A + 7\r\n" +
" A = A + 6\r\n" +
" A = A + 7\r\n" +
" REPEAT 4:\r\n" +
" REPEAT 7:\r\n" +
" A = A + 1\r\n" +
" REPEAT 2:\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" A = A + 8\r\n" +
" A = A + 2\r\n" +
"A = A + 1\r\n" +
"A = A + 4\r\n" +
"REPEAT 8:\r\n" +
" A = A + 5\r\n" +
"A = A + 6\r\n" +
"REPEAT 7:\r\n" +
" REPEAT 6:\r\n" +
" REPEAT 9:\r\n" +
" A = A + 7\r\n" +
" A = A + 8\r\n" +
" REPEAT 4:\r\n" +
" A = A + 6\r\n" +
" A = A + 4\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" REPEAT 9:\r\n" +
" A = A + 3\r\n" +
" REPEAT 9:\r\n" +
" A = A + 2\r\n" +
" A = A + 7\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
"REPEAT 7:\r\n" +
" REPEAT 8:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 4\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" REPEAT 4:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 6\r\n" +
" A = A + 8\r\n" +
" REPEAT 1:\r\n" +
" A = A + 6\r\n" +
" A = A + 4\r\n" +
" A = A + 6\r\n" +
" REPEAT 3:\r\n" +
" A = A + 7\r\n" +
" REPEAT 3:\r\n" +
" A = A + 4\r\n" +
" A = A + 4\r\n" +
" A = A + 2\r\n" +
" A = A + 3\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 6\r\n" +
" A = A + 5\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 8:\r\n" +
" A = A + 5\r\n" +
" REPEAT 3:\r\n" +
" A = A + 6\r\n" +
" REPEAT 9:\r\n" +
" A = A + 4\r\n" +
" A = A + 3\r\n" +
" REPEAT 6:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 1\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
"A = A + 2\r\n" +
"A = A + 7\r\n" +
"REPEAT 4:\r\n" +
" A = A + 7\r\n" +
"A = A + 9\r\n" +
"A = A + 2\r\n" +
"REPEAT 8:\r\n" +
" A = A + 9\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" A = A + 2\r\n" +
" A = A + 1\r\n" +
" A = A + 5\r\n" +
" REPEAT 9:\r\n" +
" A = A + 1\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" REPEAT 7:\r\n" +
" A = A + 2\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 3\r\n" +
" REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" REPEAT 8:\r\n" +
" A = A + 9\r\n" +
" REPEAT 5:\r\n" +
" A = A + 5\r\n" +
" A = A + 4\r\n" +
" A = A + 2\r\n" +
" A = A + 4\r\n" +
" REPEAT 6:\r\n" +
" A = A + 2\r\n" +
" REPEAT 5:\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" REPEAT 8:\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" REPEAT 1:\r\n" +
" A = A + 8\r\n" +
" A = A + 5\r\n" +
" A = A + 1\r\n" +
" A = A + 1\r\n" +
" A = A + 5\r\n" +
" REPEAT 2:\r\n" +
" A = A + 6\r\n" +
" REPEAT 6:\r\n" +
" A = A + 9\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" REPEAT 4:\r\n" +
" A = A + 7\r\n" +
" A = A + 1\r\n" +
" REPEAT 6:\r\n" +
" A = A + 8\r\n" +
" A = A + 4\r\n" +
" REPEAT 3:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 1\r\n" +
" A = A + 5\r\n" +
" REPEAT 2:\r\n" +
" A = A + 7\r\n" +
" REPEAT 9:\r\n" +
" A = A + 6\r\n" +
" A = A + 8\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 3:\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" A = A + 9\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 7\r\n" +
" A = A + 2\r\n" +
" A = A + 1\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" A = A + 5\r\n" +
"A = A + 1\r\n" +
"REPEAT 8:\r\n" +
" A = A + 4\r\n" +
"A = A + 2\r\n" +
"A = A + 2\r\n" +
"A = A + 8\r\n" +
"REPEAT 4:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 8\r\n" +
" REPEAT 7:\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
" REPEAT 2:\r\n" +
" A = A + 6\r\n" +
" REPEAT 4:\r\n" +
" A = A + 8\r\n" +
" A = A + 6\r\n" +
" A = A + 1\r\n" +
" A = A + 3\r\n" +
"A = A + 2\r\n" +
"A = A + 7\r\n" +
"A = A + 4\r\n" +
"REPEAT 8:\r\n" +
" A = A + 2\r\n" +
" A = A + 4\r\n" +
"REPEAT 5:\r\n" +
" REPEAT 3:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 8\r\n" +
" A = A + 1\r\n" +
" A = A + 6\r\n" +
" A = A + 5\r\n" +
" A = A + 9\r\n" +
"REPEAT 8:\r\n" +
" A = A + 7\r\n" +
"REPEAT 6:\r\n" +
" A = A + 4\r\n" +
"A = A + 5\r\n" +
"REPEAT 3:\r\n" +
" A = A + 1\r\n" +
" REPEAT 1:\r\n" +
" REPEAT 5:\r\n" +
" A = A + 6\r\n" +
" A = A + 2\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 3\r\n" +
" REPEAT 9:\r\n" +
" A = A + 9\r\n" +
" A = A + 8\r\n" +
" REPEAT 8:\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 4\r\n" +
" REPEAT 9:\r\n" +
" A = A + 3\r\n" +
" A = A + 4\r\n" +
" A = A + 5\r\n" +
"REPEAT 9:\r\n" +
" REPEAT 7:\r\n" +
" A = A + 5\r\n" +
" REPEAT 3:\r\n" +
" A = A + 7\r\n" +
" REPEAT 9:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 4\r\n" +
" A = A + 6\r\n" +
" REPEAT 5:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 5\r\n" +
" A = A + 3\r\n" +
" A = A + 3\r\n" +
" A = A + 3\r\n" +
" A = A + 5\r\n" +
" REPEAT 7:\r\n" +
" A = A + 5\r\n" +
" REPEAT 2:\r\n" +
" A = A + 5\r\n" +
" A = A + 6\r\n" +
" REPEAT 2:\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" A = A + 3\r\n" +
"A = A + 5\r\n" +
"A = A + 5\r\n" +
"REPEAT 4:\r\n" +
" A = A + 2\r\n" +
" A = A + 1\r\n" +
" REPEAT 9:\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" A = A + 6\r\n" +
" A = A + 2\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" REPEAT 9:\r\n" +
" A = A + 5\r\n" +
" A = A + 4\r\n" +
" REPEAT 4:\r\n" +
" REPEAT 4:\r\n" +
" A = A + 1\r\n" +
" A = A + 2\r\n" +
" REPEAT 6:\r\n" +
" A = A + 9\r\n" +
" A = A + 3\r\n" +
" REPEAT 2:\r\n" +
" A = A + 5\r\n" +
" A = A + 1\r\n" +
" A = A + 1\r\n" +
" A = A + 3\r\n" +
" A = A + 8\r\n" +
" REPEAT 7:\r\n" +
" A = A + 4\r\n" +
" REPEAT 6:\r\n" +
" A = A + 9\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" A = A + 4\r\n" +
" A = A + 6\r\n" +
"REPEAT 7:\r\n" +
" A = A + 9\r\n" +
"REPEAT 9:\r\n" +
" A = A + 4\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" A = A + 3\r\n" +
" REPEAT 5:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" A = A + 4\r\n" +
" REPEAT 8:\r\n" +
" A = A + 9\r\n" +
" A = A + 6\r\n" +
" A = A + 2\r\n" +
" REPEAT 3:\r\n" +
" A = A + 4\r\n" +
" A = A + 4\r\n" +
" REPEAT 3:\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" A = A + 4\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
" A = A + 3\r\n" +
" REPEAT 3:\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" A = A + 8\r\n" +
" A = A + 2\r\n" +
" A = A + 5\r\n" +
" REPEAT 8:\r\n" +
" REPEAT 2:\r\n" +
" A = A + 6\r\n" +
" A = A + 7\r\n" +
" A = A + 6\r\n" +
" A = A + 9\r\n" +
" A = A + 2\r\n" +
"REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" REPEAT 8:\r\n" +
" A = A + 7\r\n" +
" A = A + 2\r\n" +
" A = A + 1\r\n" +
" A = A + 4\r\n" +
" A = A + 1\r\n" +
" A = A + 5\r\n" +
" A = A + 2\r\n" +
" A = A + 1\r\n" +
" REPEAT 1:\r\n" +
" A = A + 1\r\n" +
" REPEAT 6:\r\n" +
" A = A + 4\r\n" +
" A = A + 3\r\n" +
" A = A + 3\r\n" +
" REPEAT 5:\r\n" +
" A = A + 3\r\n" +
" REPEAT 6:\r\n" +
" REPEAT 1:\r\n" +
" A = A + 5\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" A = A + 9\r\n" +
" A = A + 8\r\n" +
" REPEAT 1:\r\n" +
" A = A + 2\r\n" +
" REPEAT 5:\r\n" +
" A = A + 8\r\n" +
" REPEAT 3:\r\n" +
" A = A + 2\r\n" +
" A = A + 9\r\n" +
" A = A + 6\r\n" +
" A = A + 3\r\n" +
" REPEAT 5:\r\n" +
" REPEAT 6:\r\n" +
" A = A + 5\r\n" +
" A = A + 5\r\n" +
" REPEAT 4:\r\n" +
" A = A + 5\r\n" +
" A = A + 4\r\n" +
" REPEAT 8:\r\n" +
" A = A + 9\r\n" +
" A = A + 1\r\n" +
" REPEAT 8:\r\n" +
" A = A + 8\r\n" +
" A = A + 1\r\n" +
" A = A + 4\r\n" +
" REPEAT 6:\r\n" +
" A = A + 6\r\n" +
" REPEAT 2:\r\n" +
" A = A + 3\r\n" +
" A = A + 9\r\n" +
" A = A + 6\r\n" +
" A = A + 9\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" REPEAT 3:\r\n" +
" A = A + 3\r\n" +
" A = A + 4\r\n" +
" A = A + 2\r\n" +
" A = A + 8\r\n" +
" REPEAT 2:\r\n" +
" A = A + 4\r\n" +
" A = A + 1\r\n" +
" REPEAT 9:\r\n" +
" A = A + 2\r\n" +
" A = A + 9\r\n" +
" A = A + 7\r\n" +
"REPEAT 7:\r\n" +
" REPEAT 7:\r\n" +
" REPEAT 5:\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 1\r\n" +
" A = A + 1\r\n" +
" REPEAT 5:\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" REPEAT 9:\r\n" +
" A = A + 4\r\n" +
" A = A + 1\r\n" +
" REPEAT 6:\r\n" +
" A = A + 8\r\n" +
" A = A + 5\r\n" +
" REPEAT 1:\r\n" +
" A = A + 4\r\n" +
" REPEAT 5:\r\n" +
" A = A + 8\r\n" +
" A = A + 7\r\n" +
" A = A + 2\r\n" +
" REPEAT 3:\r\n" +
" A = A + 3\r\n" +
" REPEAT 8:\r\n" +
" REPEAT 8:\r\n" +
" A = A + 4\r\n" +
" A = A + 7\r\n" +
" REPEAT 5:\r\n" +
" A = A + 1\r\n" +
" REPEAT 8:\r\n" +
" A = A + 7\r\n" +
" A = A + 8\r\n" +
" A = A + 4\r\n" +
" A = A + 7\r\n" +
" A = A + 6\r\n" +
" A = A + 9\r\n" +
" A = A + 5\r\n" +
"REPEAT 3:\r\n" +
" A = A + 5\r\n" +
" REPEAT 9:\r\n" +
" A = A + 1\r\n" +
" A = A + 7\r\n" +
"REPEAT 1:\r\n" +
" A = A + 8\r\n" +
"A = A + 4\r\n" +
"REPEAT 8:\r\n" +
" REPEAT 7:\r\n" +
" A = A + 2\r\n" +
" REPEAT 4:\r\n" +
" A = A + 6\r\n" +
" A = A + 6\r\n" +
" REPEAT 1:\r\n" +
" A = A + 7\r\n" +
" A = A + 1\r\n" +
"REPEAT 9:\r\n" +
" REPEAT 5:\r\n" +
" A = A + 6\r\n" +
" A = A + 5\r\n" +
" REPEAT 7:\r\n" +
" A = A + 3\r\n" +
" A = A + 6\r\n" +
" A = A + 8\r\n" +
" REPEAT 2:\r\n" +
" A = A + 7\r\n" +
" A = A + 1\r\n" +
" A = A + 9\r\n" +
" REPEAT 3:\r\n" +
" REPEAT 3:\r\n" +
" A = A + 5";
// System.out.println(str1);
String[] arr = str1.split("\n");//以换行符为分割 分割成一个数组
int temp = 1;
List<Integer> list = new ArrayList<Integer>(); //代表要加反括号(“}”)的缩减位置
for (int i = 0; i < arr.length; i++) {
String str2 = arr[i];
int a = str2.indexOf("R");//先找R
if(a == -1) {
a = str2.indexOf("A");//没找到R,就找A
if(list.contains(new Integer(a))) {
System.out.print("}");
list.remove(new Integer(a));
}
// str2 += ";";
System.out.println(str2 + ";");
}else {
if(list.contains(new Integer(a))) {
System.out.print("}");
list.remove(new Integer(a));
}
//变成像for循环的样子
// str2.replace("REPEAT", "for (int i " + temp +" = 0; i " + temp +" <").replace(":", "; i " + temp + "++) {");
System.out.println(str2.replace("REPEAT", "for (int i" + temp +" = 0; i" + temp +" <").replace(":", "; i" + temp + "++) {"));
temp++;
//需要在以后某一行加“}”
list.add(a);
}
}
for (Integer integer : list) {
System.out.println("}");
}
}
}
之后将得到的for循环运出来即可。
6.分类计数
时间限制: 1.0s 内存限制: 512.0MB 本题总分:15 分
问题描述:
输入一个字符串,请输出这个字符串包含多少个大写字母,多少个小写字
母,多少个数字。
输入格式:
输入一行包含一个字符串。
输出格式:
输出三行,每行一个整数,分别表示大写字母、小写字母和数字的个数。
测试样例:
Input:
1+a=Aab
Output:
1
3
1
评测用例规模与约定
对于所有评测用例,字符串由可见字符组成,长度不超过 100。
这道题很简单。
思路:
大写字母就是A-Z之间,小写字母是a-z之间,数字就是0-9之间
代码如下(示例):
import java.util.Scanner;
public class Eleven6_分类计数 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
scanner.close();
//记录大写字母的个数
int capital = 0;
//记录小写字母的个数
int lowercase = 0;
//记录数字的个数
int nums = 0;
for (int i = 0; i < str.length(); i++) {
char arr = str.charAt(i);
if(arr >= 'A' && arr <= 'Z') {
capital++;
}else if(arr >= 'a' && arr <= 'z') {
lowercase++;
}else if(arr >= '0' && arr <= '9') {
nums++;
}
}
System.out.println(capital);
System.out.println(lowercase);
System.out.println(nums);
}
}
7.整除序列
时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分
问题描述:
有一个序列,序列的第一个数是 n,后面的每个数是前一个数整除 2,请输
出这个序列中值为正数的项。
输入格式
输入一行包含一个整数 n。
输出格式
输出一行,包含多个整数,相邻的整数之间用一个空格分隔,表示答案。
测试样例:
Input:
20
Output:
20 10 5 2 1
评测用例规模与约定
对于 80% 的评测用例,1 ≤ n ≤ 10^9。
对于所有评测用例,1 ≤ n ≤ 10^18。
这道题同样不难。
代码如下(示例):
import java.util.Scanner;
public class Eleven7_整除序列 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
while(n > 0) {
System.out.print(n + " ");
// n /= 2;
n = n >> 1;
}
}
}
8.走方格
时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分
题目描述:
在平面上有一些二维的点阵。
这些点的编号就像二维数组的编号一样,从上到下依次为第 1 至第 n 行,从左到右依次为第 1 至第 m 列,每一个点可以用行号和列号来表示。
现在有个人站在第 1 行第 1 列,要走到第 n 行第 m 列。只能向右或者向下走。
注意,如果行号和列数都是偶数,不能走入这一格中。
问有多少种方案。
输入格式
输入一行包含两个整数 n, m。
输出格式
输出一个整数,表示答案。
测试样例1:
Input:
3 4
Output:
2
测试案例2:
Input:
6 6
Output:
0
评测用例规模与约定:
对于所有评测用例,1 ≤ n ≤ 30, 1 ≤ m ≤ 30。
这道题说难也不难。就是要思路清晰,并且能写出来。 通过推算得出:当n,m的值均为偶数时,是不可能走到目标位的,所以输出的就是0 使用深搜
代码如下(示例):
import java.util.Scanner;
public class Eleven8_走方格 {
static int n;
static int m;
static int sum;//记录最终需要多少方法的变量
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
scanner.close();
sum = 0;
long startTime = System.currentTimeMillis();
dfs(1, 1);
System.out.println(sum);
long midTime = System.currentTimeMillis();
System.out.println((midTime - startTime) + "ms"); //通过测试,得出这种方法很耗时间,效率不高
print(n, m);
long endTime = System.currentTimeMillis();
System.out.println((endTime - midTime) + "ms"); //通过测试,显然这个更快
}
public static void dfs(int i, int j) {
if(i > n || j > m) {
return;
}
//如果为偶数
if(i % 2 == 0 && j % 2 == 0) {
return;
}
if(i == n && j == m) {
sum++;
return;
}
//走的方向:向右或者向下
dfs(i, j + 1);
dfs(i + 1, j);
}
//优化
public static void print(int n, int m) {
int[][] arr = new int[30][30];
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
if(i == 0 || j == 0) {
arr[i][j] = 1;
}else if(i % 2 == 1 && j % 2 == 1) {
arr[i][j] = 0;
}else {
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
}
}
System.out.println(arr[n - 1][m - 1]);
}
}
9.字符串编码
时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分
问题描述:
小明发明了一种给由全大写字母组成的字符串编码的方法。对于每一个大
写字母,小明将它转换成它在 26 个英文字母中序号,即 A → 1, B → 2, … Z →
26。
这样一个字符串就能被转化成一个数字序列:
比如 ABCXYZ → 123242526。
现在给定一个转换后的数字序列,小明想还原出原本的字符串。当然这样
的还原有可能存在多个符合条件的字符串。小明希望找出其中字典序最大的字
符串。
输入格式
一个数字序列
输出格式
一个只包含大写字母的字符串,代表答案
测试样例:
Input:
123242526
Output:
LCXYZ
评测用例规模与约定
对于 20% 的评测用例,输入的长度不超过 20。
对于所有评测用例,输入的长度不超过 200000。
这道题也没什么难度,理解题目,理好思路即可。 代码如下(示例):
import java.util.Scanner;
public class Eleven9_字符串编码 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
scanner.close();
String result = "";
for (int i = 0; i < str.length(); i++) {
//判断i+2是否小于等于26,此时还有加上i+2的大小是否会超出字符串的长度
if(i + 2 <= str.length() && (Integer.valueOf(str.substring(i, i + 2))) <= 26) {
int k = (Integer.valueOf(str.substring(i, i + 2)));
char ch = (char)('A' - 1 + k);
result += ch;
i++;//加一的原因是:此时已经取了两位了
}else {
int k = (Integer.valueOf(str.substring(i, i + 1)));
char ch = (char)('A' - 1 + k);
result += ch;
}
}
System.out.println(result);
}
}
10.整数小拼接
时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分
问题描述:
给定义个长度为 n 的数组 A1 , A2 , ⋅ ⋅ ⋅ , An。
你可以从中选出两个数 Ai和 Aj(i不等于j),然后将 Ai和 Aj一前一后拼成一个新的整数。例如 12 和 345 可以拼成 12345 或 34512 。
注意交换 Ai和Aj的顺序总是被视为 2 种拼法,即便是 Ai=Aj时。
请你计算有多少种拼法满足拼出的整数小于等于 K。
输入格式:
第一行包含 2 个整数n和 K。
第二行包含 n 个整数 A1 , A2 , ⋅ ⋅ ⋅ , An.
输出格式:
一个整数代表答案。
测试样例:
Input:
4 33
1 2 3 4
Output:
8
评测用例规模与约定:
对于 30% 的评测用例,1 ≤ N ≤ 1000, 1 ≤ K ≤ 10^8,1 <= Ai <= 10^4.
对于所有评测用例,1 ≤ N ≤ 100000,1 ≤ K ≤ 10^10,1 <= Ai <= 10^9.
这道题嘛,单单使用暴力破解法是没啥问题的。 但这只能得一些分,要拿全部分需要进行优化
代码如下(示例):
import java.util.Arrays;
import java.util.Scanner;
public class Eleven10_整数小拼接 {
static int[] nums;
static int n;
static int k;
static String m;
public static void main(String[] args) {
inits();
}
//暴力破解法——对于 30% 的评测用例,1 ≤ N ≤ 1000, 1 ≤ K ≤ 10^8,1 <= Ai <= 10^4.
public static void inits() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.nextInt();
nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(i != j) {
String str = "" + nums[i] + nums[j];
int newArray = Integer.parseInt(str);
if(newArray <= k) {
sum++;
}
}
}
}
System.out.println(sum);
}
这个优化嘛,我也是借助他人的来理解。
import java.util.Arrays;
import java.util.Scanner;
public class Eleven10_整数小拼接优化 {
static int[] nums;
static int n;
static String k;
public static void main(String[] args) {
print();
}
//优化——对于所有评测用例,1 ≤ N ≤ 100000,1 ≤ K ≤ 10^10,1 <= Ai <= 10^9.
public static void print() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.next();
nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();
Arrays.sort(nums);
int sum = 0;
for (int i = 0; i < nums.length; i++) {
String is = nums[i] + ""; //放在前面的数
if(is.length() >= k.length()) { //如果数组中的某一个元素起始长度大于要比较的数字k时,直接跳出。
break;
}
//先截取前面的数的位数
String begin = k.substring(0, is.length());
if(begin.compareTo(is) < 0) { //说明 345678 45后只能接999之内的数字(不能大于)
int a = k.length() - is.length(); //后面有多少位
int temp = (int)(Math.pow(10, a - 1)) - 1;
sum += findIndex(temp);
//如果我nums[i]这个数(放在前面的数),比temp还要小
if(nums[i] <= temp) {
sum--;
}
}else if(begin.compareTo(is) == 0) {
int temp = Integer.parseInt(k.substring(is.length()));
sum += findIndex(temp);
//如果我nums[i]这个数(放在前面的数),比temp还要小
if(nums[i] <= temp) {
sum--;
}
}else {
int a = k.length() - is.length(); //后面有多少位
int temp = (int)(Math.pow(10, a)) - 1;
sum += findIndex(temp);
//如果我nums[i]这个数(放在前面的数),比temp还要小
if(nums[i] <= temp) {
sum--;
}
}
}
System.out.println(sum);
}
//二分查找
public static int findIndex(int k) {
int left = 0;
int right = nums.length - 1;
int mid = (left + right) >> 1;
while(left < right) {
if(nums[mid] == k) {
return mid + 1;
}else if(nums[mid] < k) {
left = mid + 1;
}else {
right = mid;
}
mid = (left + right) >> 1;
}
//如果用二分法没有找到k
if(nums[mid] < k) {
return mid + 1; //k比中间数大,那么只要小于等于中间数的,都比k小
}else {
return mid; //k比中间数小,那么只要小于等于中间数下标为-1的这个数,比k小
}
}
}
总结
OK!到这里,我就完全写完了。这是我第一次写博客,我也不知道怎么写(懵逼懵逼)。不过还好,最后还是写出来了(原来也没那么难啊,哈哈)对于这些算法题,我也是第一次做,马上就要比赛,已经刷了一些题目了,但感觉还是不够啊!感觉自己太菜了,所以对于18号的比赛好慌啊!(但还是对自己要说加油加油!)
最后,如果有什么写的不好,可以提出了,毕竟我还是一个小菜鸡啊!