import java. util. Arrays ;
import java. util. Scanner ;
public class Test8_36 {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
System . out. print ( "Enter a number n: " ) ;
int n = input. nextInt ( ) ;
int [ ] [ ] arr = new int [ n] [ n] ;
int limit_max = 65 + n;
System . out. println ( "Enter " + n + " rows of letters separated by spaces: " ) ;
char temp_str;
int temp_int = 0 ;
for ( int i = 0 ; i < n ; i++ ) {
for ( int j = 0 ; j < n ; j++ ) {
temp_str = input. next ( ) . charAt ( 0 ) ;
temp_int = ( int ) temp_str;
if ( temp_int >= 65 && temp_int < limit_max) {
arr[ i] [ j] = temp_int;
} else {
System . out. println ( "Wrong input: the letters must be from A to " + ( char ) ( limit_max - 1 ) ) ;
return ;
}
}
}
int [ ] count = new int [ 65 + n] ;
for ( int i = 0 ; i < n ; i++ ) {
Arrays . fill ( count, 0 ) ;
for ( int j = 0 ; j < n ; j++ ) {
if ( count[ arr[ i] [ j] ] == 0 ) {
++ count[ arr[ i] [ j] ] ;
} else {
System . out. println ( "The input array is not Latin square" ) ;
return ;
}
}
}
for ( int j = 0 ; j < n ; j++ ) {
Arrays . fill ( count, 0 ) ;
for ( int i = 0 ; i < n ; i++ ) {
if ( count[ arr[ i] [ j] ] == 0 ) {
++ count[ arr[ i] [ j] ] ;
} else {
System . out. println ( "The input array is not Latin square" ) ;
return ;
}
}
}
System . out. println ( "The input array is Latin square" ) ;
}
}
public class Test8_18 {
public static void main ( String [ ] args) {
int [ ] [ ] m = { { 1 , 2 } , { 3 , 4 } , { 5 , 6 } , { 7 , 8 } , { 9 , 10 } } ;
shuffle ( m) ;
}
public static void shuffle ( int [ ] [ ] m) {
int [ ] temp = new int [ 2 ] ;
for ( int a = 0 ; a < m. length ; a++ ) {
for ( int b = 0 ; b < m. length ; b++ ) {
if ( ( int ) ( Math . random ( ) * 2 ) == 0 ) {
temp[ 0 ] = m[ a] [ 0 ] ;
temp[ 1 ] = m[ a] [ 1 ] ;
m[ a] [ 0 ] = m[ b] [ 0 ] ;
m[ a] [ 1 ] = m[ b] [ 1 ] ;
m[ b] [ 0 ] = temp[ 0 ] ;
m[ b] [ 1 ] = temp[ 1 ] ;
}
}
}
}
}
import java. util. Arrays ;
import java. util. Random ;
public class Test6 {
public static void main ( String [ ] args) {
int [ ] arr = new int [ 100000 ] ;
Random rd = new Random ( ) ;
for ( int i = 0 ; i < arr. length ; i++ ) {
arr[ i] = rd. nextInt ( 100000 ) ;
}
Test6_StopWatch sw = new Test6_StopWatch ( ) ;
Arrays . sort ( arr) ;
sw. stop ( ) ;
System . out. println ( "执行了" + sw. getElaspsedTime ( ) + "毫秒" ) ;
}
}
class Test6_StopWatch {
private long startTime, endTime;
public Test6_StopWatch ( ) {
startTime = System . currentTimeMillis ( ) ;
}
public void stop ( ) {
this . endTime = System . currentTimeMillis ( ) ;
}
public long getElaspsedTime ( ) {
return this . endTime - this . startTime;
}
}
public class Test8 {
public static void main ( String [ ] args) {
Test8_Fan fan0 = new Test8_Fan ( ) ;
fan0. setSpeed ( Test8_Fan . FAST) ;
fan0. setradius ( 10 ) ;
fan0. setColor ( "yellow" ) ;
fan0. setOn ( true ) ;
Test8_Fan fan1 = new Test8_Fan ( ) ;
fan1. setSpeed ( Test8_Fan . MEDIUM) ;
fan1. setradius ( 5 ) ;
fan1. setColor ( "blue" ) ;
fan1. setOn ( false ) ;
System . out. println ( fan0. toString ( ) ) ;
System . out. println ( fan1. toString ( ) ) ;
}
}
class Test8_Fan {
static final int SLOW = 1 , MEDIUM = 2 , FAST = 3 ;
private int speed = SLOW;
private boolean on = false ;
private double radius = 5 ;
String color = "blue" ;
public int getSpeed ( ) {
return speed;
}
public void setSpeed ( int speed) {
this . speed = speed;
}
public boolean isOn ( ) {
return on;
}
public void setOn ( boolean on) {
this . on = on;
}
public double getradius ( ) {
return radius;
}
public void setradius ( double radius) {
this . radius = radius;
}
public String getColor ( ) {
return color;
}
public void setColor ( String color) {
this . color = color;
}
public Test8_Fan ( ) { }
public String toString ( ) {
if ( on) {
return speed + " " + color + " " + radius;
} else {
return "fan is off" ;
}
}
}
public class Test9 {
public static void main ( String [ ] args) {
Test9_RegularPolygon rp0 = new Test9_RegularPolygon ( ) ;
Test9_RegularPolygon rp1 = new Test9_RegularPolygon ( 6 , 4 ) ;
Test9_RegularPolygon rp2 = new Test9_RegularPolygon ( 10 , 4 , 5.6 , 7.8 ) ;
System . out. printf ( "第一个参数周长、面积:%.2f %.2f\n" , rp0. getPerimeter ( ) , rp0. getArea ( ) ) ;
System . out. printf ( "第二个参数周长、面积:%.2f %.2f\n" , rp1. getPerimeter ( ) , rp1. getArea ( ) ) ;
System . out. printf ( "第三个参数周长、面积:%.2f %.2f\n" , rp2. getPerimeter ( ) , rp2. getArea ( ) ) ;
}
}
class Test9_RegularPolygon {
private int n = 3 ;
private double side = 1.0 ;
private double x = 0.0 ;
private double y = 0.0 ;
public Test9_RegularPolygon ( ) { }
public Test9_RegularPolygon ( int n, double side) {
this . n = n;
this . side = side;
}
public Test9_RegularPolygon ( int n, double side, double x, double y) {
this . n = n;
this . side = side;
this . x = x;
this . y = y;
}
public int getN ( ) {
return n;
}
public void setN ( int n) {
this . n = n;
}
public double getSide ( ) {
return side;
}
public void setSide ( double side) {
this . side = side;
}
public double getX ( ) {
return x;
}
public void setX ( double x) {
this . x = x;
}
public double getY ( ) {
return y;
}
public void setY ( double y) {
this . y = y;
}
public double getPerimeter ( ) {
return n * side;
}
public double getArea ( ) {
return ( n * side * side) / ( 4 * Math . tan ( Math . PI / n) ) ;
}
}
import java. util. Scanner ;
public class Test10 {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
System . out. println ( "Enter a, b, c: " ) ;
int a = input. nextInt ( ) , b = input. nextInt ( ) , c = input. nextInt ( ) ;
Test10_QuadraticEquation qe = new Test10_QuadraticEquation ( a, b, c) ;
double judge = qe. getDiscriminant ( ) ;
if ( judge > 1 ) {
System . out. printf ( "%.3f %.3f\n" , qe. getRoot1 ( ) , qe. getRoot2 ( ) ) ;
} else if ( judge < 0.0001 && judge > - 0.0001 ) {
System . out. printf ( "%.3f\n" , qe. getRoot1 ( ) ) ;
} else {
System . out. printf ( "The equation has no roots" ) ;
}
}
}
class Test10_QuadraticEquation {
private double a, b, c;
public Test10_QuadraticEquation ( int a, int b, int c) {
this . a = a;
this . b = b;
this . c = c;
}
public double getA ( ) {
return a;
}
public double getB ( ) {
return b;
}
public double getC ( ) {
return c;
}
public double getDiscriminant ( ) {
return b * b - 4 * a * c;
}
public double getRoot1 ( ) {
return ( - b + Math . sqrt ( getDiscriminant ( ) ) ) / ( 2 * a) ;
}
public double getRoot2 ( ) {
return ( - b - Math . sqrt ( getDiscriminant ( ) ) ) / ( 2 * a) ;
}
}
import java. util. Scanner ;
public class Test13 {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
System . out. print ( "Enter the number of rows and columns in the array:" ) ;
int rows = input. nextInt ( ) , cols = input. nextInt ( ) ;
double [ ] [ ] arr = new double [ rows] [ cols] ;
System . out. println ( "Enter the array:" ) ;
for ( int a = 0 ; a < rows ; a++ ) {
for ( int b = 0 ; b < cols ; b++ ) {
arr[ a] [ b] = input. nextDouble ( ) ;
}
}
Test13_Location l = new Test13_Location ( ) ;
l = locateLargest ( arr) ;
System . out. println ( "The location of the largest elements is " + Test13_Location . maxValue +
" at (" + Test13_Location . row + ", " + Test13_Location . column + ")" ) ;
}
public static Test13_Location locateLargest ( double [ ] [ ] a) {
Test13_Location l = new Test13_Location ( ) ;
for ( int m = 0 ; m < a. length ; m++ ) {
for ( int n = 0 ; n < a[ 0 ] . length ; n++ ) {
if ( Test13_Location . maxValue < a[ m] [ n] ) {
Test13_Location . maxValue = a[ m] [ n] ;
Test13_Location . row = m;
Test13_Location . column = n;
}
}
}
return l;
}
}
class Test13_Location {
public static int row = 0 , column = 0 ;
public static double maxValue = 0.0 ;
}
public class Test1 {
public static void main ( String [ ] args) {
Test1_Time t1 = new Test1_Time ( ) ;
System . out. printf ( "%d小时%d分%d秒\n" , t1. getHour ( ) , t1. getMinute ( ) , t1. getSecond ( ) ) ;
Test1_Time t2 = new Test1_Time ( 555550000L ) ;
System . out. printf ( "%d小时%d分%d秒" , t2. getHour ( ) , t2. getMinute ( ) , t2. getSecond ( ) ) ;
}
}
class Test1_Time {
long hour, minute, second;
public Test1_Time ( ) {
long [ ] arr = millis_to_hms ( System . currentTimeMillis ( ) ) ;
hour = arr[ 0 ] ;
minute = arr[ 1 ] ;
second = arr[ 2 ] ;
}
public Test1_Time ( Long l) {
long [ ] arr = millis_to_hms ( l) ;
hour = arr[ 0 ] ;
minute = arr[ 1 ] ;
second = arr[ 2 ] ;
}
public Test1_Time ( long hour, long minute, long second) {
this . hour = hour;
this . minute = minute;
this . second = second;
}
public long getHour ( ) {
return hour;
}
public long getMinute ( ) {
return minute;
}
public long getSecond ( ) {
return second;
}
public void setTime ( long elapseTime) {
long [ ] arr = new long [ 3 ] ;
arr = millis_to_hms ( elapseTime) ;
hour += arr[ 0 ] ;
minute += arr[ 1 ] ;
second += arr[ 2 ] ;
}
public long [ ] millis_to_hms ( long l) {
long [ ] feedback = new long [ 3 ] ;
feedback[ 2 ] = l / 1000 % 60 ;
feedback[ 1 ] = l / 1000 / 60 % 60 ;
feedback[ 0 ] = l / 1000 / 60 / 60 % 24 ;
return feedback;
}
}
public class Test3 {
public static void main ( String [ ] args) {
Test3_MyInteger mi = new Test3_MyInteger ( 1 ) ;
System . out. println ( mi. getValue ( ) ) ;
System . out. println ( mi. isEven ( ) + " " + mi. isOdd ( ) + " " + mi. isPrime ( ) ) ;
System . out. println ( Test3_MyInteger . isEven ( 2 ) + " " + Test3_MyInteger . isOdd ( 2 ) +
" " + Test3_MyInteger . isPrime ( 2 ) ) ;
Test3_MyInteger mi_pro = new Test3_MyInteger ( 2 ) ;
System . out. println ( Test3_MyInteger . isEven ( mi_pro) + " " + Test3_MyInteger . isOdd ( mi_pro) +
" " + Test3_MyInteger . isPrime ( mi_pro) ) ;
char [ ] ch = { '1' , '2' , '3' } ;
System . out. println ( Test3_MyInteger . parseInt ( ch) ) ;
String str = "123" ;
System . out. print ( Test3_MyInteger . parseInt ( str) ) ;
}
}
class Test3_MyInteger {
static int value;
public Test3_MyInteger ( int num) {
value = num;
}
public int getValue ( ) {
return value;
}
public boolean isEven ( ) {
return value % 2 == 0 ;
}
public boolean isOdd ( ) {
return value % 2 == 1 ;
}
public boolean isPrime ( ) {
return havePrimeNumber ( value) ;
}
public static boolean isEven ( int num) {
return num % 2 == 0 ;
}
public static boolean isOdd ( int num) {
return num % 2 == 1 ;
}
public static boolean isPrime ( int num) {
return havePrimeNumber ( value) ;
}
public static boolean isEven ( Test3_MyInteger mi) {
return mi. isEven ( mi. value) ;
}
public static boolean isOdd ( Test3_MyInteger mi) {
return mi. isOdd ( mi. value) ;
}
public static boolean isPrime ( Test3_MyInteger mi) {
return havePrimeNumber ( mi. value) ;
}
public static boolean havePrimeNumber ( int num) {
boolean bool = true ;
for ( int i = 2 ; i <= num / 2 ; i++ ) {
if ( num % i != 0 ) {
bool = false ;
}
}
return bool;
}
public boolean equals ( int num) {
return value == num;
}
public boolean equals ( Test3_MyInteger mi) {
return this . value == mi. value;
}
public static int parseInt ( char [ ] arr) {
int length = arr. length;
String str = "" ;
for ( int i = 0 ; i < length ; i++ ) {
str += arr[ i] ;
}
return parseInt ( str) ;
}
public static int parseInt ( String str) {
int length = str. length ( ) , result = 0 ;
char temp;
for ( int i = 0 ; i < length ; i++ ) {
temp = str. charAt ( i) ;
result = result * 10 + temp - 48 ;
}
return result;
}
}
public class Test4 {
public static void main ( String [ ] args) {
Test4_MyPoint mp = new Test4_MyPoint ( ) ;
System . out. println ( mp. distance ( 10 , 30.5 ) ) ;
System . out. printf ( "%.4f" , mp. distance ( 10 , 30.5 ) ) ;
}
}
class Test4_MyPoint {
double x, y;
public Test4_MyPoint ( ) {
x = 0 ;
y = 0 ;
}
public Test4_MyPoint ( double x, double y) {
this . x = x;
this . y = y;
}
public double distance ( Test4_MyPoint mp) {
return Math . sqrt ( ( mp. x - this . x) * ( mp. x - this . x) + ( mp. y - this . y) * ( mp. y - this . y) ) ;
}
public double distance ( double x, double y) {
return Math . sqrt ( ( this . x - x) * ( this . x - x) + ( this . y - y) * ( this . y - y) ) ;
}
}
public class Test10 {
public static void main ( String [ ] args) {
Test10_Queue queue = new Test10_Queue ( ) ;
for ( int i = 1 ; i <= 20 ; i++ ) {
queue. enqueue ( i) ;
}
for ( int i = 1 ; i <= 20 ; i++ ) {
System . out. print ( queue. dequeue ( ) + " " ) ;
}
}
}
class Test10_Queue {
private int [ ] element;
private int size = 0 ;
public Test10_Queue ( ) {
element = new int [ 8 ] ;
}
public void enqueue ( int v) {
if ( size == element. length) {
element = enlarge ( element) ;
}
element[ size] = v;
size++ ;
}
public int [ ] enlarge ( int [ ] arr) {
int [ ] temp = new int [ size * 2 ] ;
for ( int i = 0 ; i < size ; i++ ) {
temp[ i] = arr[ i] ;
}
arr = temp;
return arr;
}
public int dequeue ( ) {
int temp = element[ 0 ] ;
System . arraycopy ( element, 1 , element, 0 , size) ;
if ( size == element. length) {
element[ size-- ] = 0 ;
}
return temp;
}
public boolean empty ( ) {
boolean bool = false ;
if ( element[ 0 ] == 0 ) {
bool = true ;
}
return bool;
}
public int getSize ( ) {
return size;
}
}
public class Test11 {
public static void main ( String [ ] args) {
Test11_Circle2D c1 = new Test11_Circle2D ( 2 , 2 , 5.5 ) ;
System . out. printf ( "面积:%.2f\n" , c1. getArea ( ) ) ;
System . out. printf ( "周长:%.2f\n" , c1. getPerimeter ( ) ) ;
System . out. println ( "====================" ) ;
System . out. println ( c1. contains ( 3 , 3 ) ) ;
System . out. println ( c1. contains ( new Test11_Circle2D ( 4 , 5 , 10.5 ) ) ) ;
System . out. println ( c1. overlaps ( new Test11_Circle2D ( 3 , 5 , 2.3 ) ) ) ;
}
}
class Test11_Circle2D {
double x, y;
public double getX ( ) {
return x;
}
public double getY ( ) {
return y;
}
double radius;
public double getRadius ( ) {
return radius;
}
public Test11_Circle2D ( ) {
x = 0 ;
y = 0 ;
radius = 1 ;
}
public Test11_Circle2D ( double x, double y, double radius) {
this . x = x;
this . y = y;
this . radius = radius;
}
public double getArea ( ) {
return Math . PI * radius * radius;
}
public double getPerimeter ( ) {
return 2 * Math . PI * radius;
}
public boolean contains ( double x, double y) {
double distance = Math . sqrt ( ( this . x - x) * ( this . x - x) + ( this . y - y) * ( this . y - y) ) ;
boolean b;
if ( distance >= radius)
b = false ;
else
b = true ;
return b;
}
public boolean contains ( Test11_Circle2D circle) {
boolean judgeStepOne, judgeStepTwo;
double distance = Math . sqrt ( ( x - circle. x) * ( x - circle. x) + ( y - circle. y) * ( y - circle. y) ) ;
judgeStepOne = contains ( circle. x, circle. y) ;
judgeStepTwo = ( distance + circle. radius <= radius) ;
return judgeStepOne && judgeStepTwo;
}
public boolean overlaps ( Test11_Circle2D circle) {
double distance = Math . sqrt ( ( x - circle. x) * ( x - circle. x) + ( y - circle. y) * ( y - circle. y) ) ;
return distance < radius + circle. radius;
}
}
public class Test22_MyString1 {
char [ ] ch;
public Test22_MyString1 ( char [ ] chars) {
ch = chars;
}
public char charAt ( int index) {
return ch[ index] ;
}
public int length ( ) {
return ch. length;
}
public Test22_MyString1 substring ( int begin, int end) {
char [ ] feedback = new char [ end - begin] ;
for ( int i = 0 ; begin < end ; i++ ) {
feedback[ i] = ch[ begin] ;
begin++ ;
}
return new Test22_MyString1 ( feedback) ;
}
public Test22_MyString1 toLowerCase ( ) {
int temp = 0 ;
for ( int i = 0 ; i < ch. length ; i++ ) {
temp = ch[ i] ;
if ( temp >= 65 && temp <= 90 ) {
temp += 32 ;
ch[ i] = ( char ) temp;
}
}
return new Test22_MyString1 ( ch) ;
}
public boolean equals ( Test22_MyString1 s) {
boolean result = true ;
int thisLength = ch. length;
int sLength = s. ch. length;
if ( thisLength != sLength)
return false ;
for ( int i = 0 ; i < thisLength ; i++ ) {
if ( ch[ i] != s. ch[ i] ) {
result = false ;
}
}
return result;
}
public static Test22_MyString1 valueOf ( int i) {
char [ ] feedback = { ( char ) i} ;
return new Test22_MyString1 ( feedback) ;
}
}
public class Test23_MyString2 {
char [ ] ch;
public Test23_MyString2 ( String s) {
ch = new char [ s. length ( ) ] ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
ch[ i] = s. charAt ( i) ;
}
}
public int compare ( String s) {
int count = 0 ;
int chLen = ch. length;
int sLen = s. length ( ) ;
if ( chLen != sLen) {
return 0 ;
}
for ( int i = 0 ; i < chLen ; i++ ) {
if ( ch[ i] == s. charAt ( i) ) {
count++ ;
}
}
return count;
}
public Test23_MyString2 substring ( int begin) {
String feedback = "" ;
for ( ; begin < ch. length ; begin++ ) {
feedback += ch[ begin] ;
}
return new Test23_MyString2 ( feedback) ;
}
public Test23_MyString2 toUpperCase ( ) {
int temp = 0 ;
for ( int i = 0 ; i < ch. length ; i++ ) {
temp = ch[ i] ;
if ( temp >= 98 && temp <= 133 ) {
temp -= 33 ;
ch[ i] = ( char ) temp;
}
}
String feedback = "" ;
for ( int i = 0 ; i < ch. length ; i++ ) {
feedback += ch[ i] ;
}
return new Test23_MyString2 ( feedback) ;
}
public char [ ] toChars ( ) {
return ch;
}
public static Test23_MyString2 valueOf ( boolean b) {
return new Test23_MyString2 ( ( b) ? "1" : "0" ) ;
}
}
public class Test02 {
public static void main ( String [ ] args) {
Test02_Person p = new Test02_Person ( ) ;
System . out. println ( p. toString ( ) ) ;
Test02_Student stu = new Test02_Student ( ) ;
System . out. println ( stu. toString ( ) ) ;
Test02_Employee e = new Test02_Employee ( ) ;
System . out. println ( e. toString ( ) ) ;
Test02_Faculty f = new Test02_Faculty ( ) ;
System . out. println ( f. toString ( ) ) ;
Test02_Staff staff = new Test02_Staff ( ) ;
System . out. println ( staff. toString ( ) ) ;
}
}
import java. util. GregorianCalendar ;
class Test02_MyDate {
int year, month, day;
public Test02_MyDate ( ) {
GregorianCalendar gc = new GregorianCalendar ( ) ;
year = gc. get ( GregorianCalendar . YEAR) ;
month = gc. get ( GregorianCalendar . MONTH) ;
day = gc. get ( GregorianCalendar . DAY_OF_MONTH) ;
}
public Test02_MyDate ( long l) {
GregorianCalendar gc = new GregorianCalendar ( ) ;
gc. setTimeInMillis ( l) ;
year = gc. get ( GregorianCalendar . YEAR) ;
month = gc. get ( GregorianCalendar . MONTH) ;
day = gc. get ( GregorianCalendar . DAY_OF_MONTH) ;
}
public Test02_MyDate ( int year, int month, int day) {
this . year = year;
this . month = month;
this . day = day;
}
public void setDate ( long elapsedTime) {
GregorianCalendar gc = new GregorianCalendar ( ) ;
gc. setTimeInMillis ( elapsedTime + toCalender ( year, month, day) ) ;
year = gc. get ( GregorianCalendar . YEAR) ;
month = gc. get ( GregorianCalendar . MONTH) ;
day = gc. get ( GregorianCalendar . DAY_OF_MONTH) ;
}
public long toCalender ( int year, int month, int day) {
day += year * 365 ;
switch ( month) {
case 11 : day += 31 ;
case 10 : day += 30 ;
case 9 : day += 31 ;
case 8 : day += 30 ;
case 7 : day += 31 ;
case 6 : day += 31 ;
case 5 : day += 30 ;
case 4 : day += 31 ;
case 3 : day += 30 ;
case 2 : day += 31 ;
case 1 : day += 28 ;
}
return ( long ) day * 24 * 60 * 60 * 1000 ;
}
}
class Test02_Person {
int name, address, phoneNumber, email;
@Override
public String toString ( ) {
return "Test02_Person -- " + name;
}
}
class Test02_Student extends Test02_Person {
static String dayi = "freshman" ;
static String daer = "sophomore" ;
static String dasan = "junior" ;
static String dasi = "senior" ;
@Override
public String toString ( ) {
return "Test02_Student -- " + name;
}
}
import java. util. Date ;
class Test02_Employee extends Test02_Person {
String office;
int salary;
Date dateOfEmploy;
public static void main ( String [ ] args) {
Test02_MyDate my = new Test02_MyDate ( ) ;
}
@Override
public String toString ( ) {
return "Test02_Employee -- " + name;
}
}
import java. util. Date ;
class Test02_Faculty extends Test02_Employee {
Date workTime;
int level;
@Override
public String toString ( ) {
return "Test02_Faculty -- " + name;
}
}
class Test02_Staff extends Test02_Employee {
String title;
@Override
public String toString ( ) {
return "Test02_Staff -- " + name;
}
}
import java. util. Arrays ;
import java. util. Scanner ;
import java. util. Arrays ;
import java. util. Date ;
import java. util. Scanner ;
public class Test01 {
public static void main ( String [ ] args) {
Scanner input = new Scanner ( System . in) ;
System . out. print ( "输入三条边的值:" ) ;
int side1 = input. nextInt ( ) , side2 = input. nextInt ( ) , side3 = input. nextInt ( ) ;
System . out. print ( "输入颜色:" ) ;
String color = input. next ( ) ;
System . out. print ( "是否填充(boolean值):" ) ;
boolean filled = input. nextBoolean ( ) ;
if ( ! isTriangle ( side1, side2, side3) ) {
System . out. println ( "输入的三条边不符合要求" ) ;
System . exit ( 0 ) ;
}
Test01_Triangle myTri = new Test01_Triangle ( side1, side2, side3) ;
myTri. setColor ( color) ;
myTri. setFilled ( filled) ;
System . out. println ( myTri. getArea ( ) + "\n" + myTri. getPerimeter ( ) + "\n"
+ myTri. getColor ( ) + "\n" + myTri. isFilled ( ) ) ;
Test01_GeometricObject myGO = new Test01_Triangle ( side1, side2, side3) ;
myGO. setColor ( color) ;
myGO. setFilled ( filled) ;
}
public static boolean isTriangle ( double s1, double s2, double s3) {
double [ ] arr = { s1, s2, s3} ;
Arrays . sort ( arr) ;
boolean re1 = arr[ 0 ] + arr[ 1 ] > arr[ 2 ] ;
boolean re2 = ( s1 > 0 ) && ( s2 > 0 ) && ( s3 > 0 ) ;
return re1 && re2;
}
}
class Test01_GeometricObject {
String color;
boolean filled;
Date dateCreated;
public Test01_GeometricObject ( ) {
dateCreated = new Date ( ) ;
}
public Test01_GeometricObject ( String color, boolean filled) {
this . color = color;
this . filled = filled;
dateCreated = new Date ( ) ;
}
public String getColor ( ) {
return color;
}
public void setColor ( String color) {
this . color = color;
}
public boolean isFilled ( ) {
return filled;
}
public void setFilled ( boolean filled) {
this . filled = filled;
}
public Date getDateCreated ( ) {
return dateCreated;
}
@Override
public String toString ( ) {
return "Test01_GeometricObject{" +
"color='" + color + '\'' +
", filled=" + filled +
", dateCreated=" + dateCreated +
'}' ;
}
}
class Test01_Triangle extends Test01_GeometricObject {
public double side1 = 1 ;
public double side2 = 1 ;
public double side3 = 1 ;
public Test01_Triangle ( ) {
}
public Test01_Triangle ( double side1, double side2, double side3) {
this . side1 = side1;
this . side2 = side2;
this . side3 = side3;
}
public double getSide1 ( ) {
return side1;
}
public double getSide2 ( ) {
return side2;
}
public double getSide3 ( ) {
return side3;
}
public double getArea ( ) {
double s = ( side1 + side2 + side3) / 2 ;
return Math . pow ( s * ( s - side1) * ( s - side2) * ( s - side3) , 0.5 ) ;
}
public double getPerimeter ( ) {
return side1 + side2 + side3;
}
@Override
public String toString ( ) {
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side = " + side3;
}
}
public int arrayPairSum ( int [ ] nums) {
int min = 0 ;
Arrays . sort ( nums) ;
for ( int i = 0 ; i< nums. length; i += 2 ) min += nums[ i] ;
return min;
}
class Solution {
public int [ ] sortArrayByParity ( int [ ] A ) {
int i= 0 ;
int j= A . length- 1 ;
while ( i< j) {
if ( A [ i] % 2 == 0 ) {
i++ ;
} else if ( A [ j] % 2 == 1 ) {
j-- ;
} else {
int temp= 0 ;
temp= A [ i] ;
A [ i] = A [ j] ;
A [ j] = temp;
i++ ;
j-- ;
}
}
return A ;
}
}
import java. util. ArrayList ;
import java. util. List ;
import java. util. Scanner ;
public class TestDemo {
public static List < List < Integer > > generate ( int numRows) {
List < List < Integer > > ret = new ArrayList < > ( ) ;
if ( numRows == 0 ) {
return ret;
}
List < Integer > oneRow = new ArrayList < > ( ) ;
ret. add ( oneRow) ;
ret. get ( 0 ) . add ( 1 ) ;
for ( int i = 1 ; i < numRows; i++ ) {
List < Integer > curRow = new ArrayList < > ( ) ;
curRow. add ( 1 ) ;
List < Integer > prevRow = ret. get ( i- 1 ) ;
for ( int j = 1 ; j < i; j++ ) {
int x = prevRow. get ( j) ;
int y = prevRow. get ( j- 1 ) ;
curRow. add ( x+ y) ;
}
curRow. add ( 1 ) ;
ret. add ( curRow) ;
}
return ret;
}
public static void main ( String [ ] args) {
List < List < Integer > > lists = generate ( 3 ) ;
for ( List < Integer > tmp: lists) {
System . out. println ( tmp) ;
}
}
}
public boolean isToeplitzMatrix ( int [ ] [ ] matrix) {
for ( int i= 0 ; i< matrix. length- 1 ; i++ ) {
for ( int j= 0 ; j< matrix[ 0 ] . length- 1 ; j++ ) {
if ( matrix[ i] [ j] != matrix[ i+ 1 ] [ j+ 1 ] ) {
return false ;
}
}
}
return true ;
}
private static int [ ] f3 ( int [ ] nums, int target)
{
HashMap < Integer , Integer > map = new HashMap < > ( ) ;
for ( int i = 0 ; i < nums. length; i++ )
{
int a = target - nums[ i] ;
map. put ( nums[ i] , i) ;
if ( map. containsKey ( a) && map. get ( a) != i)
return new int [ ] { map. get ( a) , i} ;
}
throw new IllegalArgumentException ( "No two sum solution" ) ;
}
class Solution {
public int findMaxConsecutiveOnes ( int [ ] nums) {
int result= 0 ;
int count= 0 ;
for ( int i= 0 ; i< nums. length; i++ )
{
if ( nums[ i] == 1 )
{
count++ ;
} else
{
if ( count> result)
{
result= count;
}
count= 0 ;
}
}
return result> count ? result: count;
}
}
var plusOne = function ( digits) {
let len = digits. length;
for ( let i = len- 1 ; i>= 0 ; i-- ) {
if ( digits[ i] == 9 ) {
digits[ i] = 0 ;
} else {
digits[ i] ++ ;
return digits;
}
}
let newDigits = [ 1 ] . concat ( digits) ;
return newDigits;
} ;
package array ;
public class array1 {
public static void main ( String [ ] args) {
int [ ] ary = { 0 , 2 , 1 , - 6 , 6 , - 7 , 9 , 1 , 2 , 0 , 1 } ;
int [ ] ary1 = { 0 , - 6 , 1 , 2 , 6 , - 7 , 9 , 1 , 2 , 0 , 1 } ;
int sum = 0 ;
for ( int i = 0 ; i < ary. length; i++ ) {
sum += ary[ i] ;
}
int avg = sum / 3 ;
if ( avg * 3 != sum) {
System . out. println ( "false" ) ;
}
int temp = 0 ;
int step = 0 ;
for ( int j = 0 ; j < ary. length; j++ ) {
temp += ary[ j] ;
System . out. print ( ary[ j] + " " ) ;
if ( temp == avg) {
temp = 0 ;
System . out. println ( " " ) ;
step++ ;
}
}
if ( step == 3 ) {
System . out. println ( "true" ) ;
}
}
}
import java. util. Arrays ;
public class Test13 {
public static void main ( String [ ] args) {
int [ ] [ ] arr = new int [ ] [ ] { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } } ;
System . out. println ( Arrays . toString ( spiralOrder ( arr) ) ) ;
}
public static int [ ] spiralOrder ( int [ ] [ ] arr) {
if ( arr. length == 0 )
return new int [ ] { } ;
int top = 0 ;
int bottom = arr. length - 1 ;
int left = 0 ;
int right = arr[ 0 ] . length - 1 ;
int [ ] move = new int [ ] { 0 , 1 } ;
int index = 0 ;
int count = arr. length* arr[ 0 ] . length;
int [ ] result = new int [ arr. length* arr[ 0 ] . length] ;
int row = 0 ;
int column = 0 ;
while ( count > 0 ) {
result[ index++ ] = arr[ row] [ column] ;
count-- ;
if ( row == top && column == right && move[ 1 ] == 1 ) {
move[ 0 ] = 1 ;
move[ 1 ] = 0 ;
top++ ;
} else if ( row == bottom && column == right && move[ 0 ] == 1 ) {
move[ 0 ] = 0 ;
move[ 1 ] = - 1 ;
right-- ;
} else if ( row == bottom && column == left && move[ 1 ] == - 1 ) {
move[ 0 ] = - 1 ;
move[ 1 ] = 0 ;
bottom-- ;
} else if ( row == top && column == left && move[ 0 ] == - 1 ) {
move[ 0 ] = 0 ;
move[ 1 ] = 1 ;
left++ ;
}
row += move[ 0 ] ;
column += move[ 1 ] ;
}
return result;
}
}
class RecentCounter {
int count;
public RecentCounter ( ) {
this . count= 0 ;
}
Queue < Integer > queue= new LinkedList < > ( ) ;
public int ping ( int t) {
int c= 0 ;
queue. offer ( t) ;
Queue < Integer > myqueue= new LinkedList < > ( queue) ;
while ( ! myqueue. isEmpty ( ) ) {
int x= myqueue. poll ( ) ;
if ( x>= t- 3000 && x<= t) {
c++ ;
} else {
queue. poll ( ) ;
}
}
return c;
}
}
public static int [ ] maxSlidingWindow1 ( int [ ] nums, int k) {
int length = nums. length;
if ( length<= 0 || length< k) {
return new int [ ] { } ;
}
int [ ] res= new int [ length- k+ 1 ] ;
int first= 0 ;
int second= k- 1 ;
while ( second<= length- 1 ) {
int max= nums[ first] ;
for ( int i = first+ 1 ; i < first+ k; i++ ) {
if ( nums[ i] > max) {
max= nums[ i] ;
}
}
res[ first] = max;
first++ ;
second++ ;
}
return res;
}
class KthNumber {
public :
int findKth ( int k) {
vector< int > res ( k+ 1 ) ;
int i= 0 , j= 0 , t= 0 ;
res[ 0 ] = 1 ;
int num= 0 ;
for ( int h= 1 ; h<= k; h++ )
{
num= min ( res[ i] * 3 , min ( res[ j] * 5 , res[ t] * 7 ) ) ;
res[ h] = num;
if ( num== res[ i] * 3 ) i++ ;
if ( num== res[ j] * 5 ) j++ ;
if ( num== res[ t] * 7 ) t++ ;
}
return res[ k] ;
}
} ;
class MyStack {
private LinkedList < Integer > queue1;
private LinkedList < Integer > queue2;
public MyStack ( ) {
queue1= new LinkedList < Integer > ( ) ;
queue2= new LinkedList < Integer > ( ) ;
}
public void push ( int x) {
if ( queue1. isEmpty ( ) && queue2. isEmpty ( ) ) {
queue1. addLast ( x) ;
} else if ( queue1. isEmpty ( ) ) {
queue2. addLast ( x) ;
} else {
queue1. addLast ( x) ;
}
}
public int pop ( ) {
if ( queue1. isEmpty ( ) ) {
int len= queue2. size ( ) ;
for ( int i= 0 ; i< len- 1 ; i++ ) {
queue1. addLast ( queue2. removeFirst ( ) ) ;
}
return queue2. removeFirst ( ) ;
} else {
int len= queue1. size ( ) ;
for ( int i= 0 ; i< len- 1 ; i++ ) {
queue2. addLast ( queue1. removeFirst ( ) ) ;
}
return queue1. removeFirst ( ) ;
}
}
public int top ( ) {
if ( queue1. isEmpty ( ) ) {
return queue2. getLast ( ) ;
} else {
return queue1. getLast ( ) ;
}
}
public boolean empty ( ) {
return queue1. isEmpty ( ) && queue2. isEmpty ( ) ;
}
}
public class MyQueue {
private Stack < Integer > stack1;
private Stack < Integer > stack2;
public MyQueue ( ) {
stack1 = new Stack ( ) ;
stack2 = new Stack ( ) ;
}
public void push ( int x) {
stack1. push ( x) ;
}
public int pop ( ) {
if ( stack2. isEmpty ( ) ) {
while ( ! stack1. isEmpty ( ) ) {
stack2. push ( stack1. pop ( ) ) ;
}
}
return stack2. pop ( ) ;
}
public int peek ( ) {
if ( stack2. isEmpty ( ) ) {
while ( ! stack1. isEmpty ( ) ) {
stack2. push ( stack1. pop ( ) ) ;
}
}
return stack2. peek ( ) ;
}
public boolean empty ( ) {
return stack1. isEmpty ( ) && stack2. isEmpty ( ) ;
}
}
public boolean isValid ( String s) {
Stack < Character > stack = new Stack < > ( ) ;
Map < Character , Character > map= new HashMap < > ( ) ;
map. put ( ')' , '(' ) ;
map. put ( '}' , '{' ) ;
map. put ( ']' , '[' ) ;
char [ ] chars= s. toCharArray ( ) ;
for ( char ch: chars) {
if ( stack. isEmpty ( ) ) {
stack. push ( ch) ;
} else if ( stack. peek ( ) == map. get ( ch) ) {
stack. pop ( ) ;
} else {
stack. push ( ch) ;
}
}
return stack. isEmpty ( ) ;
}
class Solution {
public int evalRPN ( String [ ] tokens) {
Stack < Integer > stack = new Stack < > ( ) ;
for ( String s: tokens) {
switch ( s) {
case "+" :
stack. push ( stack. pop ( ) + stack. pop ( ) ) ;
break ;
case "-" :
stack. push ( - stack. pop ( ) + stack. pop ( ) ) ;
break ;
case "*" :
stack. push ( stack. pop ( ) * stack. pop ( ) ) ;
break ;
case "/" :
int num = stack. pop ( ) ;
stack. push ( stack. pop ( ) / num) ;
break ;
default :
stack. push ( Integer . valueOf ( s) ) ;
break ;
}
}
return stack. pop ( ) ;
}
}
public List < Integer > topKFrequent2 ( int [ ] nums, int k) {
HashMap < Integer , Integer > map = new HashMap < > ( ) ;
for ( int num : nums) {
if ( map. containsKey ( num) ) {
map. put ( num, map. get ( num) + 1 ) ;
} else {
map. put ( num, 1 ) ;
}
}
List < Integer > [ ] bucket = new List [ nums. length + 1 ] ;
for ( Map. Entry < Integer , Integer > e : map. entrySet ( ) ) {
Integer value = e. getValue ( ) ;
if ( bucket[ value] == null ) {
bucket[ value] = new ArrayList < > ( ) ;
}
bucket[ value] . add ( e. getKey ( ) ) ;
}
List < Integer > freList = new ArrayList < > ( ) ;
for ( int j = bucket. length - 1 ; j > - 1 && freList. size ( ) < k; j-- ) {
if ( bucket[ j] != null )
freList. addAll ( bucket[ j] ) ;
}
return freList;
}
public static void add ( int n) {
int count = 0 ;
for ( int i = 2 ; i < n; i++ ) {
boolean sign = true ;
for ( int j = 2 ; j < i; j++ ) {
if ( i % j == 0 ) {
sign = false ;
break ;
}
}
if ( sign) {
count++ ;
}
}
System . out. println ( count) ;
}
bool isIn ( int * a, int c, int b)
{
int i;
for ( i = 0 ; i < c; i++ )
if ( a[ i] == b)
return true ;
return false ;
}
int * powerfulIntegers ( int x, int y, int bound, int * returnSize) {
int i, j, k;
int * ret = malloc ( sizeof ( int ) * bound) ;
int buf;
int c = 0 ;
for ( j = 0 ; pow ( y, j) < bound && j < 21 ; j++ ) {
for ( i= 0 ; pow ( x, i) < bound && i < 21 ; i++ ) {
buf = pow ( y, j) + pow ( x, i) ;
if ( buf <= bound) {
if ( ! isIn ( ret, c, buf) )
ret[ c++ ] = buf;
}
}
}
* returnSize = c;
return ret;
}
class Solution {
public :
vector< int > intersection ( vector< int > & nums1, vector< int > & nums2) {
unordered_set< int > s1;
for ( auto e : nums1)
s1. insert ( e) ;
unordered_set< int > s2;
for ( auto e : nums2)
s2. insert ( e) ;
vector< int > vRet;
for ( auto e : s1)
{
if ( s2. find ( e) != s2. end ( ) )
vRet. push_back ( e) ;
}
return vRet;
}
} ;
public class Solution {
public static void main ( String [ ] args) {
System . out. println ( searchIndex ( "yayah" ) ) ;
System . out. println ( searchIndex ( "hyaya" ) ) ;
}
public static int searchIndex ( String s) {
for ( int i= 0 ; i< s. length ( ) ; i++ ) {
char ch = s. charAt ( i) ;
if ( s. indexOf ( ch) == s. lastIndexOf ( ch) ) {
return i;
}
}
return - 1 ;
}
}
class Solution ( object) :
def isAlienSorted ( self, words, order) :
"""
:type words: List[str]
:type order: str
:rtype: bool
"""
length = len ( words)
for i in range ( length- 1 ) :
a = words[ i]
b = words[ i+ 1 ]
minlen = min ( len ( a) , len ( b) )
for j in range ( minlen) :
if order. index ( a[ j] ) < order. index ( b[ j] ) :
break
elif order. index ( a[ j] ) > order. index ( b[ j] ) :
return False
elif j == minlen - 1 and len ( b) < len ( a) :
return False
return True