另类加法
import java. util. *;
public class UnusualAdd {
public int addAB ( int A, int B) {
if ( B!= 0 ) {
int a= A^ B;
int b= ( A& B) << 1 ;
return addAB ( a, b) ;
} else {
return A;
}
}
}
运行时间:82 ms
占用内存:8912 k
饥饿的小易
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int x= in. nextInt ( ) ;
int count= 0 ;
while ( x!= 0 && count<= 300000 ) {
count++ ;
x= ( 2 * x+ 1 ) % 1000000007 ;
}
if ( ( count+ 2 ) / 3 <= 100000 ) {
System. out. println ( ( count+ 2 ) / 3 ) ;
}
else {
System. out. println ( - 1 ) ;
}
}
}
类方法是属于整个类的,而实例方法是属于类的某个对象的。
由于类方法是属于整个类的,并不属于类的哪个对象,所以类方法的方法体中不能有与类的对象有关的内容。即类方法体有如下限制:
( 1 ) 类方法中不能引用对象变量;
( 2 ) 类方法中不能调用类的对象方法;
( 3 ) 在类方法中不能使用super 、this 关键字。
( 4 ) 类方法不能被覆盖。
如果违反这些限制,就会导致程序编译错误。在这里插入代码片
与类方法相比,对象方法几乎没有什么限制:
( 1 ) 对象方法中可以引用对象变量,也可以引用类变量;
( 2 ) 对象方法中可以调用类方法;
( 3 ) 对象方法中可以使用super 、this 关键字。