python+java蓝桥杯ACM日常算法题训练(一)10基础题
@
算法题训练网站:http://www.dotcpp.com
1.简单的a+b
(1)题目地址:https://www.dotcpp.com/oj/problem1000.html
(2)算法解析: 首先要能够接收到横向用空格分开的数据,并知道当运行的时候,在什么地方可以停止。
(3)语法解析:
用java语法的时候scanner.nextInt();直接可以识别整数,scanner.hasNext()配合while循环可以一直等到输入的最后一个整数;
而用python语法需要用到map函数。
(4)python代码
while True:
try:
a,b=map(int,input().strip().split())
print(a+b)
except:
break
(5)java代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = a + b;
System.out.println(c);
}
}
}
2.第一个HelloWorld程序!
(1)题目地址:https://www.dotcpp.com/oj/problem1001.html
(2)算法解析: 直接输出即可
(3)语法解析: 直接输出即可
(4)python代码
print('**************************')
print('Hello World!')
print('**************************')
(5)java代码
public class Main {
public static void main(String[] args) {
System.out.println("**************************");
System.out.println("Hello World!");
System.out.println("**************************");
}
}
3.三个数最大值
(1)题目地址:https://www.dotcpp.com/oj/problem1002.html
(2)算法解析: 设定一个中间变量,然后和三个数进行对比,最后把最大的数赋给中间变量即可。
(3)语法解析:
java代码可以用数组或者直接三个数进行比较,可以配合三元表达式;
python代码可以用max函数。
(4)python代码
a,b,c = map(int,input().strip().split())
print(max(a,b,c))
(5)java代码1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int max = 0;
if (a <= b) max = a;
else max = b;
if (max <= c) max = c;
System.out.println(max);
}
}
(5)java代码2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a[] = new int[3],max = 0;
a[0] = scanner.nextInt();
a[1] = scanner.nextInt();
a[2] = scanner.nextInt();
for(int i=0;i < 3;i++)
{
max=(a[i] < max?max: a[i]);
}
System.out.println(max);
}
}
4.密码破译
(1)题目地址:https://www.dotcpp.com/oj/problem1003.html
(2)算法解析: 字符串中的字符先转化为ASCII码,然后增加到需要的字符的ASCII码后,再转化为字符。这个就是有名的凯撒密码。
(3)语法解析:
java代码可以生成26个字母的数组,然后对比每个输入字符,向后移动,也可以先转化为ASCII码,然后再进行转化,这里用第一种。
python代码直接用ord函数和chr函数即可进行转换。
(4)python代码
a = input()
for i in a:
print(chr(ord(i) + 4),end="")
(5)java代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String kk = scanner.nextLine();
char [] s = new char[52];
for(int i = 0;i<=25;i++){
s[i] = (char)(96+i+1);
s[i+26] = (char)(64+i+1);
}
for(int j = 0;j < kk.length();j++)
{
for(int i = 0;i<52;i++)
{
if(kk.charAt(j) == s[i])
{
System.out.print(s[i+4]);
}
}
}
}
}
5.母牛的故事
(1)题目地址:https://www.dotcpp.com/oj/problem1004.html
(2)算法解析:


(3)语法解析:
利用上面的公式分段即可求解。
(4)python代码
n = eval(input())
k = []
while n != 0:
if n > 4:
s = [i for i in range(0,5)]
for i in range(5,n+1):
s.append(s[i-3] + s[i-1])
k.append(s[-1])
else:
k.append(n)
n = eval(input())
for i in k:
print(i,end="\n")
(5)java代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n;
while (scanner.hasNext()) {
n = scanner.nextInt();
if(n == 0)break;
System.out.println(result(n));
}
}
private static int result(int n) {
if(n<=4) {
return n;
}else {
return result(n-1)+result(n-3);
}
}
}
6.7.8.9.10
(1)题目地址:
第6题:https://www.dotcpp.com/oj/problem1005.html
第7题:https://www.dotcpp.com/oj/problem1006.html
第8题:https://www.dotcpp.com/oj/problem1007.html
第9题:https://www.dotcpp.com/oj/problem1008.html
第10题:https://www.dotcpp.com/oj/problem1009.html
(2)算法解析: 6.7.8.9都是基础语法题,这里就不进行分析了,是很简单的题目。直接上代码:
(4)python代码
第6题:
n = eval(input())
result = 5*(n-32)/9
print('c=%.2f' % result)
第7题:
a,b,c=map(int,input().strip().split())
print(max(a,b,c))
第8题:
x = eval(input())
y = x if x < 1 else 2 * x - 1 if x >= 1 and x < 10 else 3 * x - 11
print(y)
第9题:
x = eval(input())
s = {100:'A',90:'A',80:'B',70:'C',60:'D'}
if x < 60:
print('D')
else:
print(s[x // 10 * 10])
第10题:
n = input()
print(len(n))
print(' '.join(n))
print(n[::-1])
(5)java代码
第6题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
double result = 5 * (a-32)/9;
System.out.println(String.format("c=%.2f", result));
}
}
第7题:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a[] = new int[3],max = 0;
a[0] = scanner.nextInt();
a[1] = scanner.nextInt();
a[2] = scanner.nextInt();
for(int i=0;i < 3;i++)
{
max=(a[i] < max?max: a[i]);
}
System.out.println(max);
}
}
第8题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int b = x < 1?x:(x >= 1 && x < 10 ?2*x-1:3*x-11);
System.out.println(b);
}
}
第9题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
if (a >= 90)System.out.println('A');
else if (a >= 80 && a < 90)System.out.println('B');
else if (a >= 70 && a < 80)System.out.println('C');
else if (a >= 60 && a < 70)System.out.println('D');
else System.out.println('D');
}
}
第10题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
System.out.println(a.length());
for(int i = 0;i < a.length() - 1;i++)
{
char s = a.charAt(i);
System.out.print(s + " ");
}
System.out.print(a.charAt(a.length() - 1));
System.out.println();
for(int j = a.length() - 1;j >= 0;j--)
{
char s = a.charAt(j);
System.out.print(s);
}
}
}
python+java蓝桥杯ACM日常算法题训练(一)10基础题的更多相关文章
- Python解答蓝桥杯省赛真题之从入门到真题(二刷题目一直更新)
蓝桥刷题 原文链接: https://github.com/libo-sober/LanQiaoCup Python解答蓝桥杯省赛真题之从入门到真题 不同字串 """ 一 ...
- Java 蓝桥杯 算法训练(VIP) 最大体积
最大体积 问题描述 每个物品有一定的体积(废话),不同的物品组合,装入背包会战用一定的总体积. 假如每个物品有无限件可用,那么有些体积是永远也装不出来的. 为了尽量装满背包,附中的OIER想要研究一下 ...
- 第九届蓝桥杯JavaC组决(国)赛真题
1:年龄问题 s夫人一向很神秘.这会儿有人问起她的年龄,她想了想说: "20年前,我丈夫的年龄刚好是我的2倍,而现在他的年龄刚好是我的1.5倍". 你能算出s夫人现在的年龄吗? 这 ...
- Java蓝桥杯 算法提高 九宫格
算法提高 9-1九宫格 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 九宫格.输入1-9这9个数字的一种任意排序,构成3*3二维数组.如果每行.每列以及对角线之和都相等,打印1.否 ...
- java 蓝桥杯算法提高 出现次数最多的整数
思路:其实这道题不是太难,但是这个题太坑了,提交了好多次都不是100,后来才知道,一定一定要在输入数组数据之前先判断一下输进去的n的范围,一定一定要注意,否则就是跟我下面的图片一样的效果了,泪奔~ 问 ...
- Java 蓝桥杯 算法训练 貌似化学
** 貌似化学 ** 问题描述 现在有a,b,c三种原料,如果他们按x:y:z混合,就能产生一种神奇的物品d. 当然不一定只产生一份d,但a,b,c的最简比一定是x:y:z 现在给你3种可供选择的物品 ...
- Java 蓝桥杯 算法训练 字符串的展开 (JAVA语言实现)
** 算法训练 字符串的展开 ** 题目: 在初赛普及组的"阅读程序写结果"的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于"d-h" ...
- Java蓝桥杯 算法训练 复数归一化
算法提高 复数归一化 时间限制:1.0s 内存限制:512.0MB 编写函数Normalize,将复数归一化,即若复数为a+bi,归一化结果为a/sqrt(aa+bb) + ib/sqrt(aa+b* ...
- Java蓝桥杯——贪心算法
贪心算法 贪心算法:只顾眼前的苟且. 即在对问题求解时,总是做出在当前看来是最好的选择 如买苹果,专挑最大的买. 最优装载问题--加勒比海盗 货物重量:Wi={4,10,7,11,3,5,14,2} ...
随机推荐
- IT兄弟连 HTML5教程 HTML5表单 新增的表单属性3
9 novalidate novalidate是属性规定在提交表单时不应该验证form和input域.novalidate属性适用于的<input>类型有:text.search.url ...
- 深入浅出14个Java并发容器
前言 不考虑多线程并发的情况下,容器类一般使用ArrayList.HashMap等线程不安全的类,效率更高.在并发场景下,常会用到ConcurrentHashMap.ArrayBlockingQueu ...
- 【问题记录】 Linux分区磁盘占满,导致ssh登陆闪退
问题描述 今天要去后台看日志查个问题,通过ssh登陆到服务器后准备用平时非常熟悉的less命令打开日志查看,突然xshell客户端就闪退了.一时感觉很蒙,怎么回事??由于之前有同事遇到类似的问题,提醒 ...
- C#_.NetFramework_Web项目_NPOI_EXCEL数据导入
[推荐阅读我的最新的Core版文章,是最全的介绍:C#_.NetCore_Web项目_EXCEL数据导出] 项目需要引用NPOI的Nuget包: B-2--EXCEL数据导入--NPOI--C#获取数 ...
- 微信小程序跳转传参参数丢失?
垂死病中惊坐起,笑问 Bug 何处来?! 1.先是大写字母作祟 前两天发布了「柒留言」v2.0.0 新版本,结果...你懂的嘛,没有 Bug 的程序不是好程序,写不出 Bug 的程序员不是好程序员. ...
- flutter全栈开发学习资料大全 免费flutter学习视频 文字教程!
flutter今年特别火,google推出flutter就是为了一次开发全平台应用,包括PC端,手机wap端,android,ios直接生成APP应用,如果真的能像谷歌说的,那我们开发人员就真的好好学 ...
- IOS-dequeueReusableCellWithIdentifier的应用
这是个uitableviewcell重用的函数.当一个列表中的布局相同只是数据不同时,我们可以重用我们的cell,不需要再重复创建.上面代码的意思是,先根据identifier去重用列表中找有没有可以 ...
- 【Gradle】Android Gradle 多项目构建
Android Gradle 多项目构建 Android 项目区别 Android项目一般分为库项目,应用项目,测试项目,Android Gradle 根据这些项目分别对应3种插件:com.andro ...
- Linux Shell之监测程序
监测程序 一.探查进程 当程序运行在系统上时,我们称之为进程(process).想要监测这些进程,需要熟悉ps命令的用法.ps命令好比工具中的瑞士军刀,它能输出运行在系统上的所有程序的许多信息. 但是 ...
- Octave计算数据
设A=[1 2;3 4;5 6] B=[11 12;13 14;15 16] A.*B = :对A以及B中的对应的元素进行相乘 11 24 39 56 75 96 A.^2 :对A中的每一个元 ...