Blue bridge Cup Java 10th C group
Slowly organize some notes for later review.
The first question:
A: Sum up in this question
[Problem Description]
Xiaoming is interested in the numbers 2, 0, 1 and 9 in the number 1 to 40, including 1, 2, 9, 10 to 32, 39 and 40, a total of 28, and their sum is 574.
What is the sum of all these numbers between 1 and 2019?
Go through the numbers, screen out the numbers that meet the conditions, add up to get the answer.
Public static void main(String[] args) {public static void main(String[] args) { // TODO auto-generated method stub int ans=0; // TODO auto-generated method stub int ans=0; for (int i=0; i<=2019; I++) // traversal. This equals don't leave out! { ans+=getNum(i); } system.out.println (ans);} system.out.println (ans); } public static int getNum(int num) {// define method. String STR = string.valueof (num); // Valueof converts various types to strings. Only strings can count the length. char[] a = new char[str.length()]; for(int i=0; i<a.length; i++) { a[i] = str.charAt(i); For (int I =0; charAt =0; charAt =0; charAt =0; charAt =0; charAt =0; charAt =0; charAt =0; charAt =0; i<a.length; I++) {if (a [I] = = '2' | | a [I] = = '0' | | a [I] = = '1' | | a [I] = = '9') / / 'give a number into a string return num; } return 0; // return false;}Copy the code
Public static void main(String[] args) {// TODO auto-generated method stub ans=0; for(int i=1; i<=2019; i++){ int a=i; // I changes in the loop, and enters a separate judgment loop while(a! =0){ int b=a%10; if(b==2 ||b==0 || b==1 || b==9){ ans+=i; // Add the original number I. You can't still use a, because a/10 breaks when you loop through the digits; } a/=10; }} system.out.println (ans); }Copy the code
The second question:
B: Rectangle cutting
Xiaoming has some rectangular materials, and he wants to cut some squares out of these rectangular materials.
When he was faced with a rectangular piece of material, he always cut one cut in the middle to make the largest square, leaving one rectangular piece, and then cutting the remaining rectangular material until they were all square. For example, for a piece of material with two sides of 5 and 3 respectively (denoted as 5×3), Ming will cut out 4 squares of 3×3, 2×2, 1×1 and 1×1 in turn.
Now Xiaoming has a rectangular piece of material with two sides of length 2019 and 324 respectively. Will Xiao Ming finally. How many squares are cut out?
Idea: Use the number, the remainder of the division until 0
Public static void main(String[] args) {// TODO auto-generated method stub int A =2019; / / long int b = 324; / / wide int sum = 0; While (a! =0){// write a loop. If (a>b) // Divide the big side by the small side {sum+=a/b; //sum =a%b; //sum =a%b; } else {sum+=b/a; b=b%a; } the if (b = = 0 | | a = = 0) / / define a exit dead loop condition {break; } } System.out.println("sum = " + sum); }Copy the code
The third question:
C) The number of people in the room is 10
[Problem Description]
A non-empty substring of a string is a contiguous string of at least 1 characters. The string. For example, the string aaab has seven non-empty substrings A, B, aa, ab, aaa, aab, aaab. Notice that when you calculate, you only count the number of essentially different strings.
How many different non-empty substrings are there for the string 0100110001010001?
Use hashSet to remove duplicate values and substring cutting method
// TODO Auto-generated method stub String a="0100110001010001"; Set<String> b=new HashSet<String>(); For (int I =0; i<a.length(); i++){ for(int j=i+1; j<=a.length(); j++){ String c = a.substring(i,j); //substring cut out b.dd (c); } } System.out.println(b.size()); }Copy the code
[hashSet]
HashSet is an implementation class of the set interface, and the most common set that we use is a collection of unordered, unique objects. Because it is unordered, there is no index for each group of data, and he does not have many methods available for lists. Its main feature is that it can remove duplicate data. HashSet is used to compare objects using equals to ensure that the data is unique. If the objects of two pieces of data are consistent, then a HashSet will merge them and store only one space.
Details stamp: blog.csdn.net/jinqianwang…
Link: [substring related methods]
Stamp for details: www.w3school.com.cn/js/jsref_su…