进制转换
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String str = sc. nextLine ( ) ;
String num = str. substring ( 2 , str. length ( ) ) ;
int result = 0 ;
int power = 1 ;
for ( int i = num. length ( ) - 1 ; i >= 0 ; i-- ) {
char c = num. charAt ( i) ;
if ( c >= '0' && c <= '9' ) {
result += ( c - '0' ) * power;
} else if ( c >= 'A' && c <= 'F' ) {
result += ( c - 'A' + 10 ) * power;
}
power *= 16 ;
}
System . out. println ( result) ;
}
sc. close ( ) ;
}
}
质数因子
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
long num = scanner. nextLong ( ) ;
long k = ( long ) Math . sqrt ( num) ;
for ( long i = 2 ; i <= k; ++ i) {
while ( num % i == 0 ) {
System . out. print ( i + " " ) ;
num /= i;
}
}
System . out. println ( num == 1 ? "" : num+ " " ) ;
}
}
合并表记录
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
HashMap < Integer , Integer > map= new HashMap < > ( ) ;
int quantity= in. nextInt ( ) ;
while ( in. hasNextInt ( ) ) {
int key= in. nextInt ( ) ;
int value= in. nextInt ( ) ;
map. put ( key, map. getOrDefault ( key, 0 ) + value) ;
}
ArrayList < Map. Entry < Integer , Integer > > list= new ArrayList < Map. Entry < Integer , Integer > > ( map. entrySet ( ) ) ;
Collections . sort ( list, new Comparator < Map. Entry < Integer , Integer > > ( ) {
@Override
public int compare ( Map. Entry < Integer , Integer > o1, Map. Entry < Integer , Integer > o2) {
return o1. getKey ( ) - o2. getKey ( ) ;
}
} ) ;
for ( int i= 0 ; i< list. size ( ) ; i++ ) {
System . out. println ( list. get ( i) . getKey ( ) + " " + list. get ( i) . getValue ( ) ) ;
}
}
}
提取不重复的整数
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
int num = scanner. nextInt ( ) ;
char [ ] chars= ( num+ "" ) . toCharArray ( ) ;
String str = "" ;
for ( int i= chars. length- 1 ; i>= 0 ; i-- ) {
if ( ! str. contains ( chars[ i] + "" ) ) {
str += chars[ i] ;
}
}
System . out. println ( Integer . valueOf ( str) ) ;
}
}
字符个数统计
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
String line = scanner. next ( ) ;
BitSet bitSet = new BitSet ( 128 ) ;
for ( char c : line. toCharArray ( ) ) {
if ( ! bitSet. get ( c) ) {
bitSet. set ( c) ;
}
}
System . out. println ( bitSet. cardinality ( ) ) ;
}
}
字符串排序
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
int n = sc. nextInt ( ) ;
String [ ] arr = new String [ n] ;
for ( int i= 0 ; i< n; i++ ) {
String str = sc. next ( ) ;
arr[ i] = str;
}
Arrays . sort ( arr) ;
for ( int i= 0 ; i< arr. length; i++ ) {
System . out. println ( arr[ i] ) ;
}
}
sc. close ( ) ;
}
}
简单密码
import java. util. * ;
public class Main {
public static void main ( String [ ] arg) {
HashMap < String , Integer > map= new HashMap < > ( ) ;
map. put ( "abc" , 2 ) ;
map. put ( "def" , 3 ) ;
map. put ( "ghi" , 4 ) ;
map. put ( "jkl" , 5 ) ;
map. put ( "mno" , 6 ) ;
map. put ( "pqrs" , 7 ) ;
map. put ( "tuv" , 8 ) ;
map. put ( "wxyz" , 9 ) ;
Scanner sc= new Scanner ( System . in) ;
String line= sc. next ( ) ;
for ( char c: line. toCharArray ( ) ) {
if ( c>= 'A' && c<= 'Z' ) {
if ( ( c+ 32 ) == 'z' ) {
System . out. print ( 'a' ) ;
} else {
System . out. print ( ( char ) ( c+ 33 ) ) ;
}
} else if ( c>= 'a' && c<= 'z' ) {
final Character cF= c;
String key= map. keySet ( ) . stream ( ) . filter ( o-> o. contains ( cF. toString ( ) ) ) . findAny ( ) . get ( ) ;
System . out. print ( map. get ( key) ) ;
} else {
System . out. print ( c) ;
}
}
}
}
字符串排序
import java. util. * ;
public class Main {
public static String sort ( String str) {
List < Character > letters = new ArrayList < > ( ) ;
for ( char ch : str. toCharArray ( ) ) {
if ( Character . isLetter ( ch) ) {
letters. add ( ch) ;
}
}
letters. sort ( new Comparator < Character > ( ) {
public int compare ( Character o1, Character o2) {
return Character . toLowerCase ( o1) - Character . toLowerCase ( o2) ;
}
} ) ;
StringBuilder result = new StringBuilder ( ) ;
for ( int i = 0 , j = 0 ; i < str. length ( ) ; i++ ) {
if ( Character . isLetter ( str. charAt ( i) ) ) {
result. append ( letters. get ( j++ ) ) ;
}
else {
result. append ( str. charAt ( i) ) ;
}
}
return result. toString ( ) ;
}
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
while ( in. hasNextLine ( ) ) {
String str = in. nextLine ( ) ;
String res = sort ( str) ;
System . out. println ( res) ;
}
}
}
图片整理
import java. util. Arrays ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
while ( scanner. hasNext ( ) ) {
String str= scanner. nextLine ( ) ;
char [ ] chars= str. toCharArray ( ) ;
Arrays . sort ( chars) ;
for ( char c : chars ) {
System . out. print ( c) ;
}
System . out. println ( ) ;
}
}
}
蛇形矩阵
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
int n = sc. nextInt ( ) ;
StringBuffer str = new StringBuffer ( ) ;
int a = 1 ;
int b = 0 ;
for ( int i= 0 ; i< n; i++ ) {
a = a + i;
b = a;
for ( int j= 0 ; j< n- i; j++ ) {
str. append ( b+ " " ) ;
b = b+ j+ i+ 2 ;
}
System . out. println ( str. toString ( ) . trim ( ) ) ;
str = new StringBuffer ( ) ;
}
}
}
}
字符串加密
import java. util. Scanner ;
import java. util. LinkedHashSet ;
import java. util. ArrayList ;
public class Main {
public static void main ( String args[ ] ) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String s1 = sc. nextLine ( ) . toUpperCase ( ) ;
String s2 = sc. nextLine ( ) ;
char [ ] chars1 = s1. toCharArray ( ) ;
char [ ] chars2 = s2. toCharArray ( ) ;
LinkedHashSet < Character > set = new LinkedHashSet ( ) ;
for ( int i = 0 ; i < chars1. length; i++ ) {
set. add ( chars1[ i] ) ;
}
int k = 0 ;
while ( set. size ( ) < 26 ) {
char a = ( char ) ( 'A' + k) ;
set. add ( a) ;
k++ ;
}
ArrayList < Character > list = new ArrayList < > ( set) ;
StringBuffer sb = new StringBuffer ( ) ;
for ( int i = 0 ; i < chars2. length; i++ ) {
if ( chars2[ i] == ' ' ) {
sb. append ( chars2[ i] ) ;
} else if ( chars2[ i] < 'a' ) {
int n = ( int ) ( chars2[ i] - 'A' ) ;
char c = list. get ( n) ;
sb. append ( c) ;
} else {
int n = ( int ) ( chars2[ i] - 'a' ) ;
char c = ( char ) ( list. get ( n) + 'a' - 'A' ) ;
sb. append ( c) ;
}
}
System . out. println ( sb. toString ( ) ) ;
}
}
}
求小球落地5次后所经过的路程和第五次反弹的高度
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
while ( scanner. hasNext ( ) ) {
int height = scanner. nextInt ( ) ;
System . out. println ( 2.875 * height) ;
System . out. println ( 0.03125 * height) ;
}
}
统计字符( 输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数)
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
int isLetter= 0 , isDigit= 0 , isWhitespace= 0 , isQt= 0 ;
char [ ] str = sc. nextLine ( ) . toCharArray ( ) ;
for ( int i= 0 ; i< str. length; i++ ) {
char a = str[ i] ;
if ( Character . isLetter ( a) )
isLetter++ ;
else if ( Character . isDigit ( a) )
isDigit++ ;
else if ( Character . isWhitespace ( a) )
isWhitespace++ ;
else isQt++ ;
}
System . out. println ( isLetter) ;
System . out. println ( isWhitespace) ;
System . out. println ( isDigit) ;
System . out. println ( isQt) ;
}
}
}
迷宫问题
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
int n = sc. nextInt ( ) ;
int m = sc. nextInt ( ) ;
int [ ] [ ] num = new int [ n+ 2 ] [ m+ 2 ] ;
for ( int i= 0 ; i< m+ 2 ; i++ ) {
num[ 0 ] [ i] = 1 ;
num[ n+ 1 ] [ i] = 1 ;
}
for ( int j= 0 ; j< n+ 2 ; j++ ) {
num[ j] [ 0 ] = 1 ;
num[ j] [ m+ 1 ] = 1 ;
}
for ( int i= 1 ; i< n+ 1 ; i++ ) {
for ( int j= 1 ; j< m+ 1 ; j++ ) {
num[ i] [ j] = sc. nextInt ( ) ;
}
}
setWay ( num, 1 , 1 , n, m) ;
for ( int i= 1 ; i< n+ 1 ; i++ ) {
for ( int j= 1 ; j< m+ 1 ; j++ ) {
if ( num[ i] [ j] == 2 )
System . out. println ( "(" + ( i- 1 ) + "," + ( j- 1 ) + ")" ) ;
}
}
}
}
public static boolean setWay ( int [ ] [ ] num, int i , int j, int a, int b) {
if ( num[ a] [ b] == 2 ) {
return true ;
} else {
if ( num[ i] [ j] == 0 ) {
num[ i] [ j] = 2 ;
if ( setWay ( num, i+ 1 , j, a, b) ) {
return true ;
} else if ( setWay ( num, i, j+ 1 , a, b) ) {
return true ;
} else if ( setWay ( num, i- 1 , j, a, b) ) {
return true ;
} else if ( setWay ( num, i, j- 1 , a, b) ) {
return true ;
} else {
num[ i] [ j] = 3 ;
return false ;
}
} else {
return false ;
}
}
}
}
名字的漂亮度
import java. util. Arrays ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner in= new Scanner ( System . in) ;
while ( in. hasNext ( ) ) {
int n= in. nextInt ( ) ;
for ( int i= 0 ; i< n; i++ ) {
String str= in. next ( ) ;
int s[ ] = new int [ 128 ] ;
for ( int j= 0 ; j< str. length ( ) ; j++ ) {
s[ str. charAt ( j) ] ++ ;
}
Arrays . sort ( s) ;
int mul= 26 , sum= 0 ;
for ( int j= s. length- 1 ; j>= 0 && s[ j] > 0 ; j-- ) {
sum+= s[ j] * mul;
mul-- ;
}
System . out. println ( sum) ;
}
}
}
}
截取字符串
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc= new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String str= sc. next ( ) ;
int n= sc. nextInt ( ) ;
char [ ] ch= str. toCharArray ( ) ;
int i= 0 ;
int count= 0 ;
for ( int j= 0 ; j< ch. length; j++ ) {
if ( ch[ j] > 128 ) {
count+= 2 ;
if ( count< n) {
i++ ;
}
}
else {
count++ ;
if ( count<= n) {
i++ ;
}
}
}
System . out. println ( str. substring ( 0 , i) ) ;
}
}
}
从单向链表中删除指定值的节点
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc= new Scanner ( System . in) ;
int count= sc. nextInt ( ) ;
int head= sc. nextInt ( ) ;
LinkedList < Integer > lm= new LinkedList < Integer > ( ) ;
lm. add ( head) ;
int insert= sc. nextInt ( ) ;
while ( sc. hasNext ( ) ) {
int index= lm. indexOf ( sc. nextInt ( ) ) ;
lm. add ( index+ 1 , insert) ;
insert= sc. nextInt ( ) ;
}
int index= lm. indexOf ( insert) ;
lm. remove ( index) ;
Iterator it= lm. iterator ( ) ;
while ( it. hasNext ( ) ) {
System . out. print ( it. next ( ) + " " ) ;
}
}
}
计算字符串的距离
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String s1 = sc. next ( ) ;
String s2 = sc. next ( ) ;
int dp[ ] [ ] = new int [ s1. length ( ) + 1 ] [ s2. length ( ) + 1 ] ;
dp[ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ; i< dp. length; i++ ) {
dp[ i] [ 0 ] = i;
}
for ( int i = 1 ; i< dp[ 0 ] . length; i++ ) {
dp[ 0 ] [ i] = i;
}
for ( int i = 1 ; i< dp. length; i++ ) {
for ( int j = 1 ; j< dp[ 0 ] . length; j++ ) {
if ( s1. charAt ( i- 1 ) == s2. charAt ( j- 1 ) )
{
dp[ i] [ j] = dp[ i- 1 ] [ j- 1 ] ;
}
else {
dp[ i] [ j] = Math . min ( dp[ i- 1 ] [ j- 1 ] + 1 , Math . min ( dp[ i] [ j- 1 ] + 1 , dp[ i- 1 ] [ j] + 1 ) ) ;
}
}
}
System . out. println ( dp[ s1. length ( ) ] [ s2. length ( ) ] ) ;
}
}
}
挑7
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
int n = sc. nextInt ( ) ;
String str;
int count = 0 ;
for ( int i = 1 ; i <= n; i++ ) {
str = String . valueOf ( i) ;
if ( str. contains ( "7" ) || i % 7 == 0 ) {
count++ ;
}
}
System . out. println ( count) ;
}
sc. close ( ) ;
}
}
高精度整数加法
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String n1 = sc. next ( ) ;
String n2 = sc. next ( ) ;
int i = n1. length ( ) - 1 ;
int j = n2. length ( ) - 1 ;
boolean needCarry = false ;
Stack < Integer > stack = new Stack < > ( ) ;
while ( i >= 0 || j >= 0 ) {
char c1 = '0' ;
char c2 = '0' ;
int result = 0 ;
if ( i >= 0 ) {
c1 = n1. charAt ( i) ;
}
if ( j >= 0 ) {
c2 = n2. charAt ( j) ;
}
result = ( c1 - 48 ) + ( c2 - 48 ) + ( needCarry ? 1 : 0 ) ;
needCarry = false ;
if ( result > 9 ) {
result %= 10 ;
needCarry = true ;
}
stack. push ( result) ;
i-- ;
j-- ;
}
if ( needCarry) {
stack. push ( 1 ) ;
}
while ( ! stack. empty ( ) ) {
System . out. print ( stack. pop ( ) ) ;
}
System . out. println ( ) ;
}
}
}
找出字符串中第一个只出现一次的字符
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
while ( in. hasNext ( ) ) {
String str = in. nextLine ( ) ;
int signal = 0 ;
for ( int i= 0 ; i< str. length ( ) ; i++ ) {
if ( str. indexOf ( str. charAt ( i) ) == str. lastIndexOf ( str. charAt ( i) ) ) {
System . out. print ( str. charAt ( i) ) ;
signal = 1 ;
break ;
}
}
if ( signal == 0 ) {
System . out. print ( - 1 ) ;
}
System . out. println ( ) ;
}
}
}
查找组成一个偶数最接近的两个素数
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc= new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
int n= sc. nextInt ( ) ;
int [ ] res= new int [ 2 ] ;
for ( int i= 1 ; i<= n/ 2 ; i++ ) {
if ( isPrime ( i) && isPrime ( n- i) ) {
res[ 0 ] = i;
res[ 1 ] = n- i;
}
}
System . out. println ( res[ 0 ] ) ;
System . out. println ( res[ 1 ] ) ;
}
sc. close ( ) ;
}
private static boolean isPrime ( int num) {
for ( int i = 2 ; i < num; i++ ) {
if ( num% i== 0 )
return false ;
}
return true ;
}
}
DNA序列
import java. util. * ;
import java. io. * ;
public class Main {
public static void main ( String [ ] args) throws IOException {
BufferedReader br= new BufferedReader ( new InputStreamReader ( System . in) ) ;
String in;
while ( ( in = br. readLine ( ) ) != null ) {
int w= Integer . parseInt ( br. readLine ( ) ) ;
System . out. println ( getString ( in, w) ) ;
}
}
public static String getString ( String in, int w) {
int maxLen= 0 ;
String res= "" ;
for ( int i= 0 ; i< in. length ( ) - w+ 1 ; i++ ) {
int tmp= countSubString ( in. substring ( i, i+ w) ) ;
if ( tmp> maxLen) {
maxLen= tmp;
res= in. substring ( i, i+ w) ;
}
}
return res;
}
public static int countSubString ( String in) {
int count= 0 ;
for ( int i= 0 ; i< in. length ( ) ; i++ ) {
if ( in. charAt ( i) == 'C' || in. charAt ( i) == 'G' )
count++ ;
}
return count;
}
}
查找两个字符串a,b中的最长公共子串
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String stra = sc. next ( ) ;
String strb = sc. next ( ) ;
String longStr = ( stra. length ( ) > strb. length ( ) ) ? stra: strb;
String shortStr = ( stra. length ( ) < strb. length ( ) ) ? stra: strb;
List list = new ArrayList ( ) ;
Map map = new HashMap ( ) ;
List list2 = new ArrayList ( ) ;
List list3 = new ArrayList ( ) ;
for ( int i= 0 ; i< shortStr. length ( ) ; i++ ) {
String s = "" ;
s = s + shortStr. charAt ( i) ;
for ( int j = i+ 1 ; j< shortStr. length ( ) ; j++ ) {
s = s + shortStr. charAt ( j) ;
if ( longStr. contains ( s) ) {
list. add ( s) ;
list3. add ( s. length ( ) ) ;
}
}
}
Collections . sort ( list3) ;
int numbera = ( int ) list3. get ( list3. size ( ) - 1 ) ;
for ( int j = 0 ; j< list. size ( ) ; j++ ) {
if ( String . valueOf ( list. get ( j) ) . length ( ) == numbera) {
System . out. println ( list. get ( j) ) ;
break ;
}
}
}
}
}
24点游戏
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner input= new Scanner ( System . in) ;
double result= 0.0 ;
int [ ] num= new int [ 4 ] ;
while ( input. hasNext ( ) ) {
int [ ] temp= new int [ 4 ] ;
for ( int i= 0 ; i< 4 ; i++ ) {
num[ i] = input. nextInt ( ) ;
}
System . out. println ( check ( num, temp, result) ) ;
}
input. close ( ) ;
}
private static boolean check ( int [ ] num, int [ ] temp, double result) {
for ( int i= 0 ; i< num. length; i++ ) {
if ( temp[ i] == 0 ) {
temp[ i] = 1 ;
if ( check ( num, temp, result+ num[ i] )
|| check ( num, temp, result- num[ i] )
|| check ( num, temp, result* num[ i] )
|| check ( num, temp, result/ num[ i] ) ) {
return true ;
}
temp[ i] = 0 ;
}
}
if ( result== 24 ) {
return true ;
} else {
return false ;
}
}
}
矩阵乘法
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
Integer x = Integer . parseInt ( sc. nextLine ( ) ) ;
Integer yx = Integer . parseInt ( sc. nextLine ( ) ) ;
Integer y = Integer . parseInt ( sc. nextLine ( ) ) ;
int [ ] [ ] left= new int [ x] [ yx] ;
int [ ] [ ] right= new int [ yx] [ y] ;
int [ ] [ ] result = new int [ x] [ y] ;
for ( int i= 0 ; i< x; i++ ) {
String sb = sc. nextLine ( ) ;
String [ ] array= sb. split ( " " ) ;
for ( int j = 0 ; j < array. length; j++ ) {
left[ i] [ j] = Integer . parseInt ( array[ j] ) ;
}
}
for ( int i= 0 ; i< yx; i++ ) {
String sb = sc. nextLine ( ) ;
String [ ] array= sb. split ( " " ) ;
for ( int j = 0 ; j < array. length; j++ ) {
right[ i] [ j] = Integer . parseInt ( array[ j] ) ;
}
}
for ( int i = 0 ; i < x; i++ ) {
for ( int j = 0 ; j < y; j++ ) {
int sum= 0 ;
for ( int k = 0 ; k < yx; k++ ) {
sum = sum + left[ i] [ k] * right[ k] [ j] ;
}
result[ i] [ j] = sum;
}
}
for ( int i = 0 ; i < x; i++ ) {
for ( int j = 0 ; j < y; j++ ) {
System . out. print ( result[ i] [ j] + " " ) ;
}
System . out. println ( ) ;
}
}
}
}
字符串通配符
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
while ( scanner. hasNextLine ( ) ) {
String regx = scanner. nextLine ( ) ;
String string = scanner. nextLine ( ) ;
regx = regx. replaceAll ( "\\?" , "[0-9A-Za-z]{1}" ) ;
regx = regx. replaceAll ( "\\*" , "[0-9A-Za-z]{0,}" ) ;
regx = regx. replaceAll ( "\\." , "\\\\." ) ;
boolean result = string. matches ( regx) ;
System . out. println ( result) ;
}
}
}
扑克牌大小
import java. util. HashMap ;
import java. util. Map ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Map < String , Integer > map= new HashMap < > ( ) ;
String [ ] strings= { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" , "joker" , "JOKER" } ;
int i= 0 ;
for ( String s: strings) {
map. put ( s, i) ;
i++ ;
}
Scanner scan= new Scanner ( System . in) ;
while ( scan. hasNext ( ) ) {
String str= scan. nextLine ( ) ;
String [ ] strs= str. split ( "\\-" ) ;
String [ ] strs1= strs[ 0 ] . split ( "\\s" ) ;
String [ ] strs2= strs[ 1 ] . split ( "\\s" ) ;
if ( strs1. length== strs2. length) {
if ( map. get ( strs1[ 0 ] ) > map. get ( strs2[ 0 ] ) ) {
System . out. println ( strs[ 0 ] ) ;
} else {
System . out. println ( strs[ 1 ] ) ;
}
} else {
if ( strs1[ 0 ] . contains ( "joker" ) ) {
System . out. println ( strs[ 0 ] ) ;
} else if ( strs2[ 0 ] . contains ( "joker" ) ) {
System . out. println ( strs[ 1 ] ) ;
} else if ( strs1. length== 4 ) {
System . out. println ( strs[ 0 ] ) ;
} else if ( strs2. length== 4 ) {
System . out. println ( strs[ 1 ] ) ;
} else {
System . out. println ( "ERROR" ) ;
}
}
}
scan. close ( ) ;
}
}
合法ip
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc= new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
String str= sc. nextLine ( ) ;
String [ ] str2= str. split ( "\\." ) ;
int [ ] in= new int [ str2. length] ;
for ( int i= 0 ; i< str2. length; i++ ) {
in[ i] = Integer . parseInt ( str2[ i] ) ;
}
if ( in[ 0 ] >= 0 && in[ 0 ] <= 255 && in[ 1 ] >= 0 && in[ 1 ] <= 255 && in[ 2 ] >= 0 && in[ 2 ] <= 255 && in[ 3 ] >= 0 && in[ 3 ] <= 255 ) {
System . out. println ( "YES" ) ;
}
else {
System . out. println ( "NO" ) ;
}
}
}
}
表示数字
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
while ( scanner. hasNext ( ) ) {
String input = scanner. next ( ) ;
System . out. println ( input. replaceAll ( "([0-9]+)" , "*$1*" ) ) ;
}
}
}
自守数
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
while ( scanner. hasNext ( ) ) {
int n = scanner. nextInt ( ) ;
int selfHoldNumber = 0 ;
for ( int i = 0 ; i <= n; i++ ) {
if ( ( ( int ) Math . pow ( i, 2 ) + "" ) . matches ( "[0-9]*" + i + "$" ) ) {
selfHoldNumber++ ;
}
}
System . out. println ( selfHoldNumber) ;
}
}
}
字符统计
字符统计
import java. io. BufferedReader ;
import java. io. InputStreamReader ;
import java. io. IOException ;
public class Main {
public static void main ( String [ ] args)
{
BufferedReader br= new BufferedReader ( new InputStreamReader ( System . in) ) ;
String str;
try {
while ( ( str= br. readLine ( ) ) != null )
{
System . out. println ( count ( str) . toString ( ) ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static StringBuilder count ( String str)
{
char [ ] strArray= str. toCharArray ( ) ;
int [ ] chArray= new int [ 129 ] ;
for ( char i: strArray)
chArray[ ( int ) i] ++ ;
int max= 0 ;
for ( int i= 0 ; i< chArray. length; i++ )
if ( max< chArray[ i] )
max= chArray[ i] ;
StringBuilder sb= new StringBuilder ( ) ;
while ( max!= 0 )
{
for ( int i= 0 ; i< chArray. length; i++ )
if ( chArray[ i] == max)
sb. append ( ( char ) i) ;
max-- ;
}
return sb;
}
}
求解立方根
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNext ( ) ) {
double input = sc. nextDouble ( ) ;
double n = getCubeRoot ( input) ;
System . out. printf ( "%.1f" , n) ;
}
}
public static double getCubeRoot ( double input) {
if ( input == 0 ) {
return 0 ;
}
double x0 = input;
double x1 = ( 2 * x0 + input/ x0/ x0) / 3 ;
while ( Math . abs ( x1 - x0) > 0.001 ) {
x0 = x1;
x1 = ( 2 * x0 + input/ x0/ x0) / 3 ;
}
return x1;
}
}