python + java Blue Bridge Cup ACM everyday problems training algorithm (a) 10 basic questions

@ (Here write custom directory title)
algorithm problems training website: http://www.dotcpp.com

1. Simple a + b

(1) Title Address: https://www.dotcpp.com/oj/problem1000.html
(2) parsing algorithm: first to be able to receive data transversely separated by a space, and know when to run when, in what can be stop.
(3) parsing:
  when scanner.nextInt with java syntax (); integers can be identified directly, scanner.hasNext () complexes while loop may wait until a last input integer;
  the need to use a map function syntax python.
(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) Title Address: https://www.dotcpp.com/oj/problem1001.html
(2) Algorithm Analysis: direct output to
(3) parsing: direct output to
(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. The maximum number of three

(1) Title Address: https://www.dotcpp.com/oj/problem1002.html
(2) parsing algorithm: setting an intermediate variable, and then to compare the number of three, the maximum number of the last middle variable It can be.
(3) parsing:
  Java code can be compared with an array of three numbers or directly, can be used with a triplet of expressions;
  Python code may be 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. cryptographer

(1) Title Address: https://www.dotcpp.com/oj/problem1003.html
(2) Algorithm Analysis: After a character ASCII code string converted to the ASCII code, then increases to a desired character, then into character. This is the famous Caesar cipher.
(3) parsing:
  Java code can generate an array of 26 letters, and then compare each input character, backward movement, may be first converted to ASCII code, and then transformed, here first.
  python code conversion can be carried out directly and ord function chr functions.
(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) Title Address: https://www.dotcpp.com/oj/problem1004.html
(2) parsing algorithm:
Here Insert Picture Description
Here Insert Picture Description
(3) parsing:
With the above equation can be solved segment.
(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) Title Address:
Problem 6: https://www.dotcpp.com/oj/problem1005.html
Question 7: https://www.dotcpp.com/oj/problem1006.html
Question 8: HTTPS: //www.dotcpp.com/oj/problem1007.html
question 9: https://www.dotcpp.com/oj/problem1008.html
question 10: https://www.dotcpp.com/oj/problem1009. HTML
(2) parsing algorithm: 6.7.8.9 all basic grammar question here is not to be analyzed, and is very simple subject. Direct code on:
(. 4) Code Python
Question 6:

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

Question 7:

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

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

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

Question 10:

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

(5) java codes
Question 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));
    }
}

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

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

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

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

Guess you like

Origin www.cnblogs.com/ITXiaoAng/p/12025095.html