20240407蓝桥杯备赛 1、学习蓝桥云课省赛冲刺课 【3-搜索算法】【4-枚举与尺度法】 2、学习蓝桥云课Java省赛无忧班 【1-语言基础】 3、代码练习 数字反转 数字反转优化算法 sort排序相关 String字符串相关 StringBuilder字符串相关 HashSet相关
1、学习蓝桥云课省赛冲刺课 【3-搜索算法】【4-枚举与尺度法】
2、学习蓝桥云课Java省赛无忧班 【1-语言基础】
3、代码练习
数字反转
package THL_0407 ; import java. util. Scanner ; public class Demo01 { public static void main ( String [ ] args) { Scanner sc = new Scanner ( System . in) ; int num = sc. nextInt ( ) ; if ( num > 0 ) { StringBuilder string = new StringBuilder ( ) ; string. append ( num) ; StringBuilder string1 = new StringBuilder ( ) ; string1 = string. reverse ( ) ; String string2 = string1. toString ( ) ; int num1 = Integer . valueOf ( string2) ; System . out. println ( num1) ; } else { num = num * ( - 1 ) ; StringBuilder string = new StringBuilder ( ) ; string. append ( num) ; StringBuilder string1 = new StringBuilder ( ) ; string1 = string. reverse ( ) ; String string2 = string1. toString ( ) ; int num1 = Integer . valueOf ( string2) ; num1 = num1 * ( - 1 ) ; System . out. println ( num1) ; } }
}
数字反转优化算法
package THL_0407 ; import java. util. Scanner ;
import java. lang. Math ; public class Demo02 { public static void main ( String [ ] args) { Scanner sc = new Scanner ( System . in) ; int num = sc. nextInt ( ) ; StringBuilder string = new StringBuilder ( ) ; string. append ( Math . abs ( num) ) ; StringBuilder reversedString = string. reverse ( ) ; int reversedNum = Integer . parseInt ( reversedString. toString ( ) ) ; if ( num < 0 ) { reversedNum *= - 1 ; } System . out. println ( reversedNum) ; }
}
sort排序相关
package THL_0407 ; import java. util. Arrays ;
import java. util. * ;
import java. util. List ; import com. sun. jdi. connect. spi. Connection ; public class Demo_sort { public static void main ( String [ ] args) { List < Integer > list = new ArrayList < > ( ) ; list. add ( 8 ) ; list. add ( 4 ) ; list. add ( 5 ) ; list. add ( 6 ) ; list. add ( 9 ) ; list. add ( 3 ) ; list. add ( 1 ) ; Collections . sort ( list) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; Collections . sort ( list, new Comparator < Integer > ( ) { @Override public int compare ( Integer o1, Integer o2) { return o2- o1; } } ) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; Collections . sort ( list, ( o1, o2) -> o2- o1) ; System . out. print ( "实现降序排序:" ) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; Collections . sort ( list, ( o1, o2) -> o1- o2) ; System . out. print ( "实现升序排序:" ) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ;
} }
String字符串相关
package THL_0407 ; import java. util. Scanner ; public class Demo_String { public static void main ( String [ ] args) { Scanner sc = new Scanner ( System . in) ; String string0 = "hahaha" ; String string = sc. nextLine ( ) ; System . out. println ( "string = " + string) ; System . out. println ( "string的第三个字符为 = " + string. charAt ( 2 ) ) ; System . out. println ( "string0和string1作比较:" + ( string0. compareTo ( string) == 0 ? "相同" : "不同" ) ) ; String string1 = "fighting" ; System . out. println ( "连接字符串string0+string1 = " + string0. concat ( string1) ) ; String string2 = "hahahafighting" ; System . out. println ( "string2与连接的字符串作比较:" + string2. equals ( string0. concat ( string1) ) ) ; String string3 = "helloworldhello" ; System . out. println ( "world在字符串string3中第一次出现的索引下标为:" + string3. indexOf ( "world" ) ) ; System . out. println ( "hahah在字符串string3中第一次出现的索引下标为:" + string3. indexOf ( "hahaha" ) ) ; String string4 = "hello world zoe haha" ; System . out. println ( "string4的索引区间[12,15):" + string4. substring ( 12 , 15 ) ) ; String string5 = "helloworld" ; char [ ] ch = string5. toCharArray ( ) ; for ( int i = 0 ; i < ch. length; i++ ) { System . out. print ( ch[ i] ) ; } System . out. println ( ) ; String string6 = "" ; System . out. println ( "string5是否是空字符串:" + string5. isEmpty ( ) ) ; System . out. println ( "string6是否是空字符串:" + string6. isEmpty ( ) ) ; String str = sc. nextLine ( ) ; String str1 = sc. next ( ) ; System . out. println ( str) ; System . out. println ( str1) ; String [ ] str2 = str. split ( " " ) ; System . out. println ( "str2.length = " + str2. length) ; for ( int i = 0 ; i < str2. length; i++ ) { System . out. print ( str2[ i] + " " ) ; } System . out. println ( ) ; String [ ] str3 = str. split ( " " , 3 ) ; System . out. println ( "str3.length = " + str3. length) ; for ( int i = 0 ; i < str3. length; i++ ) { System . out. print ( str3[ i] + " " ) ; } System . out. println ( ) ; sc. close ( ) ; }
}
StringBuilder字符串相关
package THL_0407 ; import java. util. Scanner ; public class Demo_StringBuilder { public static void main ( String [ ] args) { Scanner sc = new Scanner ( System . in) ; String string = sc. nextLine ( ) ; StringBuilder str = new StringBuilder ( ) ; str. append ( string) ; StringBuilder str1 = new StringBuilder ( ) ; str1 = str. reverse ( ) ; System . out. println ( "string = " + string) ; System . out. println ( "string反转后为 = " + str1) ; int num = 265512000 ; String string1 = num + "" ; StringBuilder str2 = new StringBuilder ( ) ; str2. append ( string1) ; str2 = str2. reverse ( ) ; String string2 = str2. toString ( ) ; int num1 = Integer . valueOf ( string2) ; System . out. println ( "num反转输出为:" + num1) ; StringBuilder string3 = new StringBuilder ( ) ; string3. append ( "zoe,helloworld" ) ; System . out. println ( "hello字符串第一次出现的位置为:" + string3. indexOf ( "hello" ) ) ; StringBuilder string4 = new StringBuilder ( ) ; string4. append ( "" ) ; System . out. println ( "string3是空字符串吗?" + string3. isEmpty ( ) ) ; System . out. println ( "string4是空字符串吗?" + string4. isEmpty ( ) ) ; System . out. println ( "string3索引为3的字符为:" + string3. charAt ( 3 ) ) ; sc. close ( ) ; }
}
HashSet相关
package THL_0407 ; import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. Collections ;
import java. util. HashSet ;
import java. util. Set ; public class Demo_set { public static void main ( String [ ] args) { Set < Integer > set = new HashSet < > ( ) ; set. add ( 8 ) ; set. add ( 2 ) ; set. add ( 8 ) ; set. add ( 2 ) ; set. add ( 7 ) ; set. add ( 2 ) ; set. add ( 5 ) ; set. add ( 2 ) ; for ( Integer integer : set) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; ArrayList < Integer > list = new ArrayList < > ( set) ; Collections . sort ( list) ; System . out. print ( "升序输出:" ) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; Collections . sort ( list, ( o1, o2) -> o2- o1) ; System . out. print ( "降序输出:" ) ; for ( Integer integer : list) { System . out. print ( integer+ " " ) ; } System . out. println ( ) ; } }
package THL_0407 ; import java. util. HashSet ;
import java. util. Scanner ;
import java. util. Set ; public class Demo_set02 { public static void main ( String [ ] args) { Set < String > set1 = new HashSet < > ( ) ; set1. add ( "h" ) ; set1. add ( "e" ) ; set1. add ( "l" ) ; set1. add ( "l" ) ; set1. add ( "o" ) ; Scanner sc = new Scanner ( System . in) ; String str = sc. nextLine ( ) ; Set < String > set = new HashSet < > ( ) ; for ( int i = 0 ; i < str. length ( ) ; i++ ) { set. add ( String . valueOf ( str. charAt ( i) ) ) ; } int count = 0 ; for ( String string : set) { if ( set1. contains ( string) ) { count++ ; } } System . out. println ( count) ; set1. remove ( "l" ) ; System . out. println ( set. contains ( "l" ) ) ; set. retainAll ( set1) ; System . out. println ( set. size ( ) ) ; set. clear ( ) ; System . out. println ( set. size ( ) ) ; }
}