One 、 graphic display

This question is simple , But what we need to pay attention to is , Every “*” There's a space behind it 】

Problem description
Write a program , First, enter an integer , for example 5, Then the following figure is displayed on the screen (5 Number of lines ):
* * * * *
* * * *
* * *
* *
*
 import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int n = mScanner.nextInt();
for (int i = n; i > 0; i--) {
if (i != n) {
System.out.println();
}
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
}
}
}

Two 、 Sort

【 Sort from small to large and output in reverse order 】
Problem description
Write a program , Input 3 It's an integer , Then the program will arrange these three integers from large to small .
Input format : There is only one line of input , Three integers , Space separates the middle .
Output format : There is only one line of output , That is, the result after sorting .
I/o sample
The sample input
9 2 30
Sample output
30 9 2

 import java.util.Arrays;
import java.util.Scanner; public class Main{ public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int[] array = new int[3];
for (int i = 0; i < array.length; i++) {
array[i] = mScanner.nextInt();
}
Arrays.sort(array);
for (int i = array.length - 1; i >= 0; i--) {
System.out.print(array[i] + " ");
}
} }

3、 ... and 、2 To the power of

【 This is an answer from the Internet , Pass the test 】
Problem description
Any positive integer can be used 2 Hexadecimal said , for example :137 Of 2 The base number is represented by 10001001.
Put this 2 The decimal representation is written as 2 The sum of the powers of , Let's put the power up front , We can get the following expression :137=2^7+2^3+2^0
Now the power of the Convention is expressed in parentheses , namely a^b Expressed as a(b)
here ,137 Can be expressed as :2(7)+2(3)+2(0)
further :7=2^2+2+2^0 (2^1 use 2 Express )
3=2+2^0 
So finally 137 Can be expressed as :2(2(2)+2+2(0))+2(2+2(0))+2(0)
And so on :1315=2^10+2^8+2^5+2+1
therefore 1315 Finally, it can be expressed as :
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
Input format
Positive integer (1<=n<=20000)
Output format
As agreed n Of 0,2 Express ( There can be no spaces in the representation )
The sample input
137
Sample output
2(2(2)+2+2(0))+2(2+2(0))+2(0)
The sample input
1315
Sample output
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
Tips
It's easier to implement recursively , You can output... Recursively
 import java.util.Scanner;
public class Main{
public void fun(double e) {
double d = e;
if (d == 1) {
System.out.print("2(0)");
} else {
if (d == 2) {
System.out.print("2");
} else {
int x = 1;
int m = 0;
while (m == 0) {
if ((Math.pow(2, x + 1) > d) && (d >= Math.pow(2, x))) {
m = 1;
if (d == Math.pow(2, x)) {
System.out.print("2");
if (x == 1) {
} else {
System.out.print("(");
fun(x);
System.out.print(")");
}
} else {
System.out.print("2");
if (x == 1) {
} else {
System.out.print("(");
fun(x);
System.out.print(")");
}
System.out.print("+");
fun(d - Math.pow(2, x));
}
} else {
x++;
}
}
}
}
} public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
double n = mScanner.nextInt();
Main cm = new Main();
cm.fun(n);
}
}

Four 、 Prefix expression

【 This question does not seem to design the corresponding function according to the requirements of the question , But also passed the test 】
Problem description
Write a program , Enter a prefix expression as a string , And then calculate its value . The input format is :“ Operator object 1 object 2”, among , The operator is “+”( Add )、“-”( Subtraction )、“*”( Multiplication ) or “/”( division ), The operation object is not more than 10 The integer of , They are separated by a space . requirement : For plus 、 reduce 、 ride 、 In addition to these four operations , The corresponding functions are designed to realize .
Input format : There is only one line of input , That is, a prefix expression string .
Output format : Output the corresponding calculation results ( If it's division , Direct adoption c Linguistic “/” Operator , The result is an integer ).
I/o sample
The sample input
+ 5 2
Sample output
7
 import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
String mm = mScanner.next();
int a = mScanner.nextInt();
int b = mScanner.nextInt();
if (mm.equals("+")) {
System.out.println(a+b);
}else if (mm.equals("-")) {
System.out.println(a-b);
}else if (mm.equals("*")) {
System.out.println(a*b);
}else if (mm.equals("/")) {
System.out.println(a/b);
}
}
}

5、 ... and 、Anagrams problem

【 My approach is to use the input string first toLowerCase() All functions are converted to lowercase letters , Then use the sort function to sort the character array , Finally, compare the size of each character in turn !】
Problem description
Anagrams It refers to two words with the following characteristics : In these two words , Every letter of English ( Case insensitive ) It's the same number of times . for example ,“Unclear” and “Nuclear”、“Rimon” and “MinOR” All are Anagrams. Write a program , Enter two words , And then judge , Are these two words Anagrams. Each word is no longer than 80 Characters , And it's case independent .
Input format : There are two lines of input , It's two words .
Output format : The output has only one letter Y or N, respectively Yes and No.
I/o sample
The sample input
Unclear
Nuclear
Sample output
Y
 import java.util.Arrays;
import java.util.Scanner; public class Main{ public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
String str1 = mScanner.next().toLowerCase();
String str2 = mScanner.next().toLowerCase();
if (str1.length() == str2.length()) {
char[] arr1 = fun(str1);
char[] arr2 = fun(str2);
boolean tag = true;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
tag = false;
}
}
if (tag) {
System.out.println("Y");
}else{
System.out.println("N");
}
} else {
System.out.println("N");
}
} public static char[] fun(String str) {
char[] array = str.toCharArray();
Arrays.sort(array);
return array;
} }

6、 ... and 、 The most frequent integer

【 This problem is not too difficult , But if the consideration is not comprehensive, I think 100 It's a bit difficult to score , The problem is that the test value given in his test data is less than or equal to 1, Greater than 20…… This is a pit ...】
Problem description
Write a program , Read in a set of integers , This group of integers is arranged from small to large , The number of them N It's also entered by the user , Not more than 20. Then the program will count this array , Print out the value of the most frequent array element . If two element values appear the same number of times , That is to tie for the first place , Then print only the smaller value .
Input format : The first line is an integer N,N   £  20; Next there is N That's ok , Each line represents an integer , And in order from small to large .
Output format : There is only one line of output , That is, the element value with the most occurrences .
I/o sample
The sample input
5
100
150
150
200
250
Sample output
150
 import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int m = mScanner.nextInt();
if (m < 1 || m > 20) {
return;
}
int[] array = new int[m];
for (int i = 0; i < m; i++) {
array[i] = mScanner.nextInt();
} int max = 0;
int tag = 0;
int num = 0;
for (int i = 1; i < array.length; i++) {
if (array[i - 1] == array[i]) {
tag++;
} else {
tag = 0;
}
if (tag > max) {
num = array[i];
max = tag;
}
}
if (num==0) {
num = array[0];
}
System.out.println(num);
} }

7、 ... and 、 String statistics

Problem description
Given a length of n String S, There's another number L, The statistical length is greater than or equal to L The most frequent substring ( Different appearances can intersect ), If there are more than one , Longest output , If there are still more than one , The first time the output appears .
Input format
One number in the first line L.
The second line is the string S.
L Greater than 0, And no more than S The length of .
Output format
a line , The string required by the title .

sample input 1:
4
bbaabbaaaaa

sample output 1:
bbaa

sample input 2:
2
bbaabbaaaaa

sample output 2:
aa

Data size and engagement
n<=60
S All characters in are lowercase letters .

Tips
Enumerate all possible substrings , Count the number of times , Find the one who meets the criteria

 
 

8、 ... and 、 Matrix multiplication

【 This question examines the multiplication of matrices , If you can't multiply matrices , You can take a good look at this question , And the operation of two-dimensional arrays 】
Problem description
Enter two matrices , Namely m*s,s*n size . Output the result of multiplication of two matrices .
Input format
first line , Three positive integers separated by spaces m,s,n( Not more than 200).
Next m That's ok , Each row s An integer separated by spaces , According to matrix A(i,j).
Next s That's ok , Each row n An integer separated by spaces , According to matrix B(i,j).
Output format
m That's ok , Each row n An integer separated by spaces , Output the multiplied matrix C(i,j) Value .
The sample input
2 3 2
1 0 -1
1 1 -3
0 3
1 2
3 1
Sample output
-3 2
-8 2

Tips
matrix C Should be m That's ok n Column , among C(i,j) Equal matrix A The first i Row vectors and matrices B The first j The inner product of a column vector .
For example, in the example C(1,1)=(1,0,-1)*(0,1,3) = 1 * 0 +0*1+(-1)*3=-3

 import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int m = mScanner.nextInt();
int s = mScanner.nextInt();
int n = mScanner.nextInt();
int[][] a = new int[m][s];
int[][] b = new int[s][n];
int[][] c = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < s; j++) {
a[i][j] = mScanner.nextInt();
}
}
for (int i = 0; i < s; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = mScanner.nextInt();
}
} getMatrix(a, b); } public static void getMatrix(int[][] a, int[][] b) {
int[][] c = new int[a.length][b[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[0].length; j++) {
int sum = 0;
for (int k = 0; k < b.length; k++) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
}
} for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[0].length; j++) {
System.out.print(c[i][j] + "\t");
}
System.out.println();
} } }

Nine 、 toggle case

【 Case conversion function , String to character array conversion function 】
Problem description
Write a program , Enter a string ( Length not exceeding 20), And then case each character in the string , I'm going to change capital letters to lowercase , Lowercase letters become uppercase , And then output this new string .
Input format : Enter a string , And this string contains only English letters , Does not contain other types of characters , There are no spaces .
Output format : Output converted String .
I/o sample
The sample input
AeDb
Sample output
aEdB
 import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
String str = mScanner.next();
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (Character.isLowerCase(arr[i])) {
str = String.valueOf(arr[i]).toUpperCase();
} else {
str = String.valueOf(arr[i]).toLowerCase();
}
System.out.print(str);
}
} }

Ten 、 Dynamic arrays use

【 This problem can be considered arrayList Dynamic list !】
Read in... From the keyboard n It's an integer , Use dynamic arrays to store integers read in , The sum and average of them are calculated and output respectively . It is required to use functions to implement program code as much as possible . If the average value is a decimal, only the integral part of it will be retained .
The sample input
5
3 4 0 0 2
Sample output
9 1
The sample input
7
3 2 7 5 2 9 1
Sample output
29 4
 import java.util.ArrayList;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int length = mScanner.nextInt(); int sum = 0;
int average = 0;
for (int i = 0; i < length; i++) {
sum = sum + mScanner.nextInt();
}
System.out.println(sum + " " + sum / length); } }

11、 ... and 、 Delete array zeros

【 This problem may be troublesome if you use array , But use  ArrayList If you can get twice the result with half the effort 】
Read in... From the keyboard n Put an integer in an array , Write function CompactIntegers, Delete all values in the array as 0 The elements of , Then the element moves to the head of the array . Be careful ,CompactIntegers The function takes an array and the number of its elements as arguments , The return value of the function should be the number of new elements in the array after the deletion operation . Output the number of elements in the deleted array and output the array elements in turn . The sample input : ( Input format description :5 Is the number of input data ,3 4 0 0 2 It's separated by spaces 5 It's an integer )
5
3 4 0 0 2
Sample output :( Output format description :3 Is the number of nonzero data ,3 4 2 It's separated by spaces 3 It's a nonzero integer )
3
3 4 2
The sample input
7
0 0 7 0 0 9 0
Sample output
2
7 9
The sample input
3
0 0 0
Sample output
0
 import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner mScanner = new Scanner(System.in);
int count = mScanner.nextInt(); List<Integer> list = new ArrayList<Integer>(); // int[] arr = new int[count];
for (int i = 0; i < count; i++) {
// arr[i] = mScanner.nextInt();
list.add(mScanner.nextInt());
}
int length = CompactIntegers(list, count);
System.out.println(length); for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
} } public static int CompactIntegers(List<Integer> list, int count) {
int ss = count;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 0) {
list.remove(i);
ss--;
i--;
}
}
return ss;
}
}

notes : This article is original , First in blog Park , Reprint please indicate the source .[http://www.cnblogs.com/zhjsll/p/4412250.html]

 

Blue Bridge Cup algorithm training < One > More articles about

  1. Java Realization Blue Bridge Cup Algorithm training Monkeys eat steamed buns ( violence )

    test questions Algorithm training Monkeys eat steamed buns Problem description Once upon a time , There is a monkey who is very good at eating steamed buns , It can eat countless steamed buns , however , It eats different buns at different speeds : Meat buns are eaten every second x individual : Leek bag every second y individual : Steamed buns without stuffing are eaten every second z individual : Now there is x1 A meat ...

  2. Java Realize the Blue Bridge Cup Algorithm training Equal to n The least complete square of

    test questions Algorithm training Equal to n The least complete square of Resource constraints The time limit :1.0s Memory limit :256.0MB Problem description The output is equal to n The smallest complete square of . If a number can be expressed as the square of a natural number , This number is called completely flat ...

  3. Blue Bridge Cup algorithm training java Algorithm Expression evaluation

    Problem description Enter a legal expression that contains only addition, subtraction, division and parentheses , Find the value of the expression . Where division means integral division . Input format The input line , Contains an expression . Output format Output the value of this expression . The sample input 1-2+3*(4-5) Sample output - ...

  4. java Realization Blue Bridge Cup Algorithm training Password Suspects

    Problem description When I was young , The hero of our story -- King Copa-- His personal data is not completely secure . For him it was , It's unacceptable . therefore , He invented a code , It's easy to remember and hard to crack . later , He just knew that this kind of password is an odd number in length ...

  5. Blue Bridge Cup Algorithm training Torry Perplexity of ( Basic type )( Water problem , Sieve to find prime number )

    Algorithm training Torry Perplexity of ( Basic type ) The time limit :1.0s   Memory limit :512.0MB      Problem description Torry I love math since I was a child . One day , The teacher told him , image 2.3.5.7…… Such numbers are called prime numbers .Torry A sudden ...

  6. Blue Bridge Cup Algorithm training Section k Large number query ( Water problem )

    Algorithm training Section k Large number query The time limit :1.0s   Memory limit :256.0MB Problem description Given a sequence , The second in the sequence of each inquiry l The number to the first r The number of K Which is the big number . Input format The first line contains a number n, Represents sequence length . ...

  7. Blue Bridge Cup -- Algorithm training Section k Large number query

                                                                                  Algorithm training Section k Large number query   The time limit :1.0 ...

  8. Blue Bridge Cup Algorithm training ALGO-116 The biggest formula

    Algorithm training The biggest formula   The time limit :1.0s   Memory limit :256.0MB Problem description The subject is very simple , give N A digital , Don't change their relative position , Add... In the middle K A multiplication sign and N-K-1 Plus sign ,( Put in parentheses ) Make the final result as much as possible ...

  9. Blue Bridge Cup algorithm training Section k Large number query

    Algorithm training Section k Large number query   Problem description Given a sequence , The second in the sequence of each inquiry l The number to the first r The number of K Which is the big number . Input format The first line contains a number n, Represents sequence length . The second line contains n A positive integer , Represents a given sequence . Third ...

  10. Blue Bridge Cup Algorithm training ALGO-15 Traveler's budget

    Algorithm training Traveler's budget   The time limit :1.0s   Memory limit :256.0MB Problem description A traveler wants to drive a car from one city to another at the least cost ( Suppose the fuel tank is empty when you start ). Given the distance between two cities D1. automobile ...

Random recommendation

  1. fasicon.js ( No QR code version , If needed js To create a QR code version, please contact me :770959294@qq.com)

    /*** Created by Zhangyunshan on 2016/12/9.*/(function(wins,doct){wins.fasicon = function(){this.name = " ...

  2. [Data Structure] Bit-map Space compression and quick sort to remove duplication

    Bit-map It's a very clever data storage structure . So-called Bit-map Just use one bit Bit to mark the corresponding Value, and Key That's the element . Because of the adoption of Bit Store data in units , Can greatly save storage space .Bit-ma ...

  3. How to use it Github+Appveyor+Nuget Build your own .net core Open source library

    The following tutorial is based on the fact that you have a hosting in Github Upper .net core project , If you don't have it, you can do it yourself fork You can create a default project by yourself . We need to generate nuget In the package project project.json file , of ...

  4. android Battery management system from the top java Calls to the underlying driver ( Reprint )

    1. summary With the rapid development of mobile intelligent devices , The battery's endurance ability has induced the purchase choice of the mass consumers in a large extent ,android Whether the power management of the system is reasonable or not directly affects the battery's endurance , And the battery system is part of it , It's mainly used for battery like ...

  5. mysql High performance

    Chapter 1 : Shared lock ( Read the lock ), Exclusive lock ( Write lock ) The storage engine used to query data tables :show table status like ' Table name ' \G Storage engine for converting data tables :alter table Table name engine= lead ...

  6. java cannot find the tag library descriptor for &quot;http://java.sun.com/jsp/jstl/core&quot;

    appear java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" The main wish is ...

  7. MYSQL5.6 database ZIP Installation and VS Note for use in the project

    Hang up resources first , Download address : http://download.softagency.net/MySQL/Downloads/. find MYSQL5.6, The two versions depend on the system. What I ordered is 64 Bit :http://down ...

  8. stay MVC Add exception to add log

    MVC It's a great structure , Basically, you can find a place where you can think of it , for example IActionFilter,IResultFilter,IAuthorizationFilter as well as IExceptionFilter following ...

  9. Entity Framework Code First Appointment

    Code First So that you can pass C# perhaps Visual Basic .NET To describe the model , The basic rules of the model are checked by using conventions , And conventions are a set of built-in rules . stay Code First Class based definitions in are defined through a ...

  10. FTP Users can't log in to debug details

    FTP As a simple and convenient file sharing technology , It is used in many enterprises . If enabled FTP Verification control of , Administrators can set different access rights for different users , Control user access to specific content .IIS Medium FTP There is only one way to verify a site , That is, the basic test ...