python+java Blue Bridge Cup ACM daily algorithm training 10 basic questions

Keywords: Python Java ascii

Catalog

@(Custom Catalog title is written here)
Algorithm training website: http://www.dotcpp.com

1. Simple a+b

(1) address: https://www.dotcpp.com/oj/problem1000.html
(2) algorithm analysis: first of all, it should be able to receive the data separated by horizontal spaces, and know where to stop when running.
(3) syntax analysis:
    when using java syntax, scanner.nextInt(); can recognize integers directly, and scanner.hasNext() with while loop can wait until the last integer entered;
                  .
(4)python code

while True:
    try:
        a,b=map(int,input().strip().split())
        print(a+b)
    except:
        break

(5)java code

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. The first HelloWorld program!

(1) address: https://www.dotcpp.com/oj/problem1001.html
(2) algorithm analysis: output directly
(3) syntax parsing: output directly
(4)python code

print('**************************')
print('Hello World!')
print('**************************')

(5)java code

public class Main {
    public static void main(String[] args) {
        System.out.println("**************************");
        System.out.println("Hello World!");
        System.out.println("**************************");
    }
}


3. Maximum of three numbers

(1) address: https://www.dotcpp.com/oj/problem1002.html
(2) algorithm analysis: set an intermediate variable, then compare it with three numbers, and finally assign the largest number to the intermediate variable.
(3) syntax analysis:
   java code can be compared with array or direct three numbers, and can cooperate with ternary expression;
   python code can use the max function.
(4)python code

a,b,c = map(int,input().strip().split())
print(max(a,b,c))

(5)java code 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 code 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. Password decoding

(1) address: https://www.dotcpp.com/oj/problem1003.html
(2) algorithm analysis: the characters in the string are first converted to ASCII code, then added to the ASCII code of the required characters, and then converted to characters. This is the famous Caesar code.
(3) syntax analysis:
java code can generate an array of 26 letters, then compare each input character, move backward, or convert it to ASCII code first, and then convert it. Here, the first one is used.
python code can be converted directly by ord function and chr function.
(4)python code

a = input()
for i in a:
    print(chr(ord(i) + 4),end="")

(5)java code

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. The story of the cow

(1) address: https://www.dotcpp.com/oj/problem1004.html
(2) algorithm analysis:


(3) syntax analysis:
The above formula can be used to solve the problem.
(4)python code

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 code

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) address:
Sixth question: https://www.dotcpp.com/oj/problem1005.html
Seventh question: https://www.dotcpp.com/oj/problem1006.html
Eighth question: https://www.dotcpp.com/oj/problem1007.html
Ninth question: https://www.dotcpp.com/oj/problem1008.html
Tenth question: https://www.dotcpp.com/oj/problem1009.html
(2) algorithm analysis: 6.7.8.9 is a basic grammar problem, so it will not be analyzed here. It is a very simple problem. Direct code:
(4)python code
Sixth question:

n = eval(input())
result = 5*(n-32)/9
print('c=%.2f' % result)

Seventh question:

a,b,c=map(int,input().strip().split())
print(max(a,b,c))

Eighth question:

x = eval(input())
y = x if x < 1 else 2 * x - 1 if x >= 1 and x < 10 else 3 * x - 11
print(y)

Ninth question:

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])

Tenth question:

n = input()
print(len(n))
print(' '.join(n))
print(n[::-1])

(5)java code
Sixth questions

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

Seventh question:

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

Eighth questions

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

Ninth questions

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

Tenth questions

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

Posted by fatmcgav on Wed, 11 Dec 2019 05:45:10 -0800