笔记
常用数值转化
Integer . parseInt ( str)
Integer . valueOf ( str) . intValue ( )
String s = String . valueOf ( i) ;
String s = Integer . toString ( i) ;
常用的系统自带类
Math. Pow ( )
返回第一个参数的第二个参数次方
Math . sqrt ( )
求参数的算术平方根
Math . log ( )
返回参数的自然数底数的对数值。
Math . ound ( )
它表示四舍五入,算法为 Math . floor ( x+ 0.5 ) ,即将原来的数字加上 0.5 后再向下取整,所以,Math . round ( 11.5 ) 的结果为12 ,Math . round ( - 11.5 ) 的结果为- 11 。
char charAt ( int index)
返回指定索引处的 char 值。
boolean contentEquals ( StringBuffer sb)
当且仅当字符串与指定的StringBuffer 有相同顺序的字符时候返回真。
StringBuilder / buffer
public class RunoobTest {
public static void main ( String args[ ] ) {
StringBuilder sb = new StringBuilder ( 10 ) ;
sb. append ( "Runoob.." ) ;
System . out. println ( sb) ;
sb. append ( "!" ) ;
System . out. println ( sb) ;
sb. insert ( 8 , "Java" ) ;
System . out. println ( sb) ;
sb. delete ( 5 , 8 ) ;
System . out. println ( sb) ;
}
}
数组类 Arrays
String [ ] [ ] str = new String [ 3 ] [ 4 ] ;
public static int binarySearch ( Object [ ] a, Object key)
用二分查找算法在给定数组中搜索给定值的对象( Byte , Int , double 等) 。数组在调用前必须排序好的。如果查找值包含在数组中,则返回搜索键的索引;否则返回 ( - ( 插入点) - 1 ) 。
public static boolean equals ( long [ ] a, long [ ] a2)
如果两个指定的 long 型数组彼此相等,则返回 true 。如果两个数组包含相同数量的元素,并且两个数组中的所有相应元素对都是相等的,则认为这两个数组是相等的。换句话说,如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。同样的方法适用于所有的其他基本数据类型(Byte ,short ,Int 等)。
public static void sort ( Object [ ] a)
对指定对象数组根据其元素的自然顺序进行升序排列。同样的方法适用于所有的其他基本数据类型(Byte ,short ,Int 等)。
Scanner输入相关
小心这种情况
Scanner in = new Scanner ( System . in) ;
int cur = in. nextInt ( ) ;
System . out. println ( cur) ;
String s = in. nextLine ( ) ;
System . out. println ( s) ;
输入:
123 回车
后无法继续输入 因为s为nextInt队列中的回车符
最好的处理方式是用Integer . parseInt ( ) 转成int 就不会有回车符在队列了
基础知识
if ( ( year% 4 == 0 && year% 100 != 0 ) || ( year% 400 == 0 ) )
1. 任何一个正整数都可以由两个质数相乘得到
2. 寻找n的因数时,可以用n/ i> i作为条件 这是因为如果n有大于sqrt ( n)的因数,那么它必然还有一个小于或等于sqrt ( n) 的因数配对。因此,检查到sqrt ( n) 就足够了。 (注意完全平方数的情况)
买股票总是想在最高点卖出 最低点买入
这里贪心的点是隔日有正利润的日期 使其最大化就行
转换的原理是:
int [ ] price= { 7 , 1 , 5 , 10 , 3 , 6 , 4 }
p[ 3 ] - p[ 1 ] = p[ 3 ] - p[ 2 ] + p[ 2 ] - p[ 1 ]
常用模版
计算日期
bool isLeaf ( int x) {
return ( x % 400 == 0 ) || ( x % 4 == 0 && x % 100 != 0 ) ;
}
int end_year , end_month , end_day ;
int count_day ( int year , int month , int day) {
int ans = 0 ;
int mon[ 13 ] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
while ( 1 ) {
if ( end_year== year && end_month == month && end_day == day) {
break ;
}
day++ ;
if ( isLeaf ( year) && month== 2 ) {
if ( day> mon[ month] + 1 ) {
month++ ;
day= 1 ;
}
} else {
if ( day> mon[ month] ) {
month++ ;
day= 1 ;
}
}
if ( month> 12 ) {
month= 1 ;
year++ ;
}
ans++ ;
}
return ans;
}
最大公约数
int gcd ( int a, int b) {
return b== 0 ? a: gcd ( b, a% b) ;
}
最小公倍数
public static long gcd ( long a, long b)
{
return ( b== 0 ? a: gcd ( b, a% b) ) ;
}
public static void main ( String args[ ] )
{
Scanner cin= new Scanner ( System . in) ;
long a= cin. nextLong ( ) ;
long b= cin. nextLong ( ) ;
long c= gcd ( a, b) ;
long d= a* b/ c;
System . out. println ( c) ;
System . out. println ( d) ;
}
埃氏筛素数
核心思想是利用一个布尔数组记录是否为素数,不断地筛掉n以内素数的倍数( 即合数) ,这样留下的全是素数
public static void main ( String args[ ] )
{
Scanner cin= new Scanner ( System . in) ;
long num= cin. nextLong ( ) ;
boolean [ ] isprime= new boolean [ ( int ) num+ 1 ] ;
for ( int i= 2 ; i<= num; i++ )
isprime[ i] = true ;
int i;
for ( i= 2 ; i<= num; i++ )
{
if ( isprime[ i] )
{
System . out. println ( i) ;
for ( int j= 2 * i; j<= num; j+= i)
isprime[ j] = false ;
}
}
}
欧拉筛素数
核心思想是避免埃氏筛的重复标记 所有的合数都是由最小质因子合成筛掉的
利用的数论定理是唯一分解定理的改版形式,即任何一个数= 最小质因子数* 任意一个数
public static void main ( String args[ ] ) {
Scanner cin = new Scanner ( System . in) ;
long num = cin. nextLong ( ) ;
boolean [ ] isprime = new boolean [ ( int ) num + 1 ] ;
long [ ] prime = new long [ ( int ) num + 1 ] ;
int i, j, count = 0 ;
for ( i = 2 ; i <= num; i++ )
isprime[ i] = true ;
for ( i = 2 ; i <= num; i++ ) {
if ( isprime[ i] ) {
prime[ count++ ] = i;
System . out. println ( i) ;
}
for ( j = 0 ; j < count; j++ ) {
if ( prime[ j] > num / i)
break ;
isprime[ ( int ) ( prime[ j] * i) ] = false ;
if ( i % prime[ j] == 0 )
break ;
}
}
}
进制转换
public static void add ( int [ ] num, int incre, int index)
{
if ( index == 0 )
return ;
num[ index- 1 ] += incre;
if ( num[ index- 1 ] >= 4 )
{
num[ index- 1 ] = 0 ;
add ( num, 1 , index- 1 ) ;
}
}
public static void main ( String args[ ] )
{
String er = "0010" ;
String eight= "0077" ;
int ten1 = Integer . parseInt ( er, 2 ) ;
System . out. println ( ten1) ;
int ten2 = Integer . parseInt ( eight, 8 ) ;
System . out. println ( ten2) ;
String sixteen1= Integer . toHexString ( ten1) ;
System . out. println ( sixteen1) ;
String sixteen2= Integer . toHexString ( ten2) ;
System . out. println ( sixteen2) ;
int [ ] num = { - 1 , 0 , 0 , 0 } ;
while ( num[ 0 ] != 0 )
{
add ( num, 1 , num. length) ;
for ( int i= 1 ; i< num. length; i++ )
{
System . out. print ( num[ i] ) ;
}
System . out. println ( ) ;
}
}
```java