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);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/ITXiaoAng/p/12025095.html