1、古典问题
import java. util. Scanner;
public class Test01 {
@SuppressWarnings ( "resource" )
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入月份:" ) ;
int month= in. nextInt ( ) ;
System. out. println ( "第" + month+ "月免子总数为" + fun ( month) * 2 ) ;
}
public static int fun ( int n) {
if ( n== 1 || n== 2 ) {
return 1 ;
} else {
return fun ( n- 1 ) + fun ( n- 2 ) ;
}
}
}
2、求素数
public class Test02 {
public static void main ( String[ ] args) {
int m = 101 ;
int n = 200 ;
int count = 0 ;
for ( int i= m; i< n; i++ ) {
if ( isPrime ( i) ) {
count++ ;
System. out. print ( i+ " " ) ;
if ( count% 10 == 0 ) {
System. out. println ( ) ;
}
}
}
System. out. println ( ) ;
System. out. println ( "在" + m+ "和" + n+ "之间共有" + count+ "个素数" ) ;
}
private static boolean isPrime ( int n) {
boolean flag = true ;
if ( n== 1 ) {
flag = false ;
} else {
for ( int i= 2 ; i<= Math. sqrt ( n) ; i++ ) {
if ( ( n% i) == 0 || n== 1 ) {
flag = false ;
break ;
} else {
flag = true ;
}
}
}
return flag;
}
}
3、水仙花数
public class Test03 {
public static void main ( String[ ] args) {
int sum= 0 , one= 0 , two= 0 , three= 0 ;
for ( int i= 100 ; i<= 999 ; i++ ) {
one= i/ 100 ;
two= ( i% 100 ) / 10 ;
three= i% 10 ;
sum= one* one* one+ two* two* two+ three* three* three;
if ( i== sum) {
System. out. println ( i+ "是水仙花数" ) ;
}
}
}
}
4、分解质因数
import java. util. Scanner;
public class Test04 {
public static void main ( String[ ] args) {
System. out. println ( "请输入一个正整数:" ) ;
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
int n= in. nextInt ( ) ;
int k= 2 ;
System. out. print ( n+ "=" ) ;
while ( k<= n) {
if ( k== n) {
System. out. print ( n) ;
break ;
} else if ( n% k== 0 ) {
System. out. print ( k+ "*" ) ;
n= n/ k;
} else {
k++ ;
}
}
}
5、成绩问题
import java. util. Scanner;
public class Test05 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个成绩:" ) ;
int score= in. nextInt ( ) ;
char grade;
grade= score>= 90 ? 'A'
: score>= 60 ? 'B'
: 'C' ;
System. out. println ( "成绩为:" + grade) ;
}
@SuppressWarnings ( "resource" )
public static void getResults ( ) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请您输入分数:0-100" ) ;
int num= sc. nextInt ( ) ;
int i= ( num>= 0 && num<= 100 ) ? num/ 10 : - 1 ;
String s= null;
switch ( i) {
case 10 :
case 9 : s= "A" ; break ;
case 8 :
case 7 :
case 6 : s= "B" ; break ;
case - 1 : s= "输入的分数有问题" ; break ;
default : / / 当上门的case 没有符合的值,就执行 default
s= "C" ; break ;
}
System. out. println ( s) ;
}
}
6、最大公约数和最小公倍数
import java. util. Scanner;
public class Test06 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个正整数m:" ) ;
int m= in. nextInt ( ) ;
System. out. println ( "请输入另一个正整数n:" ) ;
int n= in. nextInt ( ) ;
int max = 0 ;
int min = 0 ;
if ( m> n) {
max= m;
min= n;
} else {
max= n;
min= m;
}
if ( max% min== 0 ) {
System. out. println ( "最大公约数是:" + min) ;
System. out. println ( "最小公倍数是:" + max) ;
} else {
int k = 0 ;
for ( int i = 1 ; i < min; i++ ) {
if ( max % i == 0 && min % i == 0 ) {
k++ ;
}
}
int [ ] maxArrays= new int [ k] ;
for ( int i = 1 ; i < min; i++ ) {
if ( max % i == 0 && min % i == 0 ) {
maxArrays[ k- 1 ] = i;
k-- ;
}
}
System. out. println ( "最大公约数是:" + maxArrays[ 0 ] ) ;
System. out. println ( "最小公倍数是:" + max* min/ maxArrays[ 0 ] ) ;
}
int a, b, m2;
@SuppressWarnings ( "resource" )
Scanner s = new Scanner ( System. in) ;
System. out. print ( "请输入一个整数: " ) ;
a = s. nextInt ( ) ;
System. out. print ( "再输入一个整数: " ) ;
b = s. nextInt ( ) ;
max cd = new max ( ) ;
m2 = cd. maxNum ( a, b) ;
int n2 = a * b / m2;
System. out. println ( "最大公约数: " + m2) ;
System. out. println ( "最小公倍数: " + n2) ;
}
}
class max {
public int maxNum ( int x, int y) {
int t;
if ( x < y) {
t = x;
x = y;
y = t;
}
while ( y != 0 ) {
if ( x == y)
return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
7、统计字符个数
import java. util. Scanner;
public class Test07 {
public static void main ( String[ ] args) {
System. out. print ( "请输入一串字符:" ) ;
Scanner in = new Scanner ( System. in) ;
String str = in. nextLine ( ) ;
in. close ( ) ;
count ( str) ;
}
private static void count ( String str) {
String E1 = "[a-zA-Z]" ;
String E2 = "[0-9]" ;
String E3 = "\\s" ;
int countLetter = 0 ;
int countNumber = 0 ;
int countSpace = 0 ;
int countOther = 0 ;
char [ ] array_Char = str. toCharArray ( ) ;
String[ ] array_String = new String [ array_Char. length] ;
for ( int i= 0 ; i< array_Char. length; i++ )
array_String[ i] = String. valueOf ( array_Char[ i] ) ;
for ( String s: array_String) {
if ( s. matches ( E1) ) {
countLetter++ ;
} else if ( s. matches ( E2) ) {
countNumber++ ;
} else if ( s. matches ( E3) ) {
countSpace++ ;
} else {
countOther++ ;
}
}
System. out. println ( "输入的字母个数:" + countLetter) ;
System. out. println ( "输入的数字个数:" + countNumber) ;
System. out. println ( "输入的空格个数:" + countSpace) ;
System. out. println ( "输入的其它字符个数:" + countOther) ;
}
}
8、a+aa+aaa+…
public class Test08 {
public static void main ( String[ ] args) {
int b= 0 , s= 0 , i= 1 ;
String str= null;
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入a的值:" ) ;
int a= in. nextInt ( ) ;
System. out. println ( "请输入相加的个数n:" ) ;
int n= in. nextInt ( ) ;
if ( n== 1 ) {
str= "s=" + a;
} else if ( n== 2 ) {
str= "s=" + a+ "+" + a+ a;
} else if ( n== 3 ) {
str= "s=" + a+ "+" + a+ a+ "+" + a+ a+ a;
} else if ( n== 4 ) {
str= "s=" + a+ "+" + a+ a+ "+" + a+ a+ a+ "+" + a+ a+ a+ a;
} else {
str= "s=" + a+ "+" + a+ a+ "+" + a+ a+ a+ "+...+" + a+ a+ "..." + a;
}
while ( i<= n) {
b+= a;
s+= b;
a= a* 10 ;
i++ ;
}
System. out. println ( str+ "=" + s) ;
}
}
9、完数
public class Test09 {
public static void main ( String[ ] args) {
int n= 1 ;
System. out. println ( "1到1000的数中" ) ;
while ( n< 1000 ) {
int t= 0 ;
int k= 0 ;
for ( int i= 1 ; i<= n/ 2 ; i++ ) {
if ( n% i== 0 ) {
k++ ;
}
}
int [ ] arr= new int [ k] ;
int m= 0 ;
for ( int i= 1 ; i<= n/ 2 ; i++ ) {
if ( n% i== 0 ) {
t+= i;
arr[ m] = i;
m++ ;
}
}
String str= "" ;
for ( int i= 0 ; i< arr. length; i++ ) {
str+= arr[ i] + "+" ;
}
if ( n== t) {
str= str. substring ( 0 , str. length ( ) - 1 ) ;
System. out. println ( n+ "是完数," + n+ "=" + str) ;
}
n++ ;
}
}
}
10、自由落体
public class Test10 {
public static void main ( String[ ] args) {
int k= 0 ;
double n= 100 ;
double sum= 0 ;
while ( k< 10 ) {
sum+= n;
n= n/ 2 ;
k++ ;
}
System. out. println ( "第10次落地时,共经过" + sum+ "米" ) ;
System. out. println ( "第10次反弹" + n+ "米" ) ;
}
}
11、无重复数
public class Test11 {
public static void main ( String[ ] args) {
int count= 0 ;
for ( int x= 1 ; x< 5 ; x++ ) {
for ( int y= 1 ; y< 5 ; y++ ) {
for ( int z= 1 ; z< 5 ; z++ ) {
if ( x!= y&& x!= z&& y!= z) {
count++ ;
System. out. println ( x* 100 + y* 10 + z) ;
}
}
}
}
System. out. println ( "能组成" + count+ "个不同的数" ) ;
}
}
12、利润问题一
import java. util. Scanner;
public class Test12 {
public static void main ( String[ ] args) {
System. out. println ( "请输入当月利润(元):" ) ;
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
double profit= in. nextInt ( ) ;
double bonus= 0 ;
if ( profit<= 100000 ) {
bonus= profit* 10 / 100 ;
} else if ( profit> 100000 && profit<= 200000 ) {
bonus= 10000 + ( profit- 100000 ) * 75 / 1000 ;
} else if ( profit> 200000 && profit<= 400000 ) {
bonus= 10000 + 7500 + ( profit- 200000 ) * 5 / 100 ;
} else if ( profit> 400000 && profit<= 600000 ) {
bonus= 10000 + 7500 + 10000 + ( profit- 400000 ) * 3 / 100 ;
} else if ( profit> 600000 && profit<= 1000000 ) {
bonus= 10000 + 7500 + 10000 + 6000 + ( profit- 600000 ) * 15 / 1000 ;
} else {
bonus= 10000 + 7500 + 10000 + 6000 + 6000 + ( profit- 1000000 ) * 1 / 100 ;
}
System. out. println ( "应发放奖金:" + bonus+ "元" ) ;
double x = 0 , y = 0 ;
System. out. print ( "输入当月利润(万):" ) ;
x = in. nextInt ( ) ;
if ( x > 0 && x <= 10 ) {
y = x * 0.1 ;
} else if ( x > 10 && x <= 20 ) {
y = 10 * 0.1 + ( x - 10 ) * 0.075 ;
} else if ( x > 20 && x <= 40 ) {
y = 10 * 0.1 + 10 * 0.075 + ( x - 20 ) * 0.05 ;
} else if ( x > 40 && x <= 60 ) {
y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + ( x - 40 ) * 0.03 ;
} else if ( x > 60 && x <= 100 ) {
y = 10 * 0.175 + 20 * 0.05 + 20 * 0.03 + ( x - 60 ) * 0.015 ;
} else if ( x > 100 ) {
y = 10 * 0.175 + 20 * 0.08 + 40 * 0.015 + ( x - 100 ) * 0.01 ;
}
System. out. println ( "应该提取的奖金是 " + y + "万" ) ;
}
}
13、利润问题二
import java. util. Scanner;
public class Test54 {
@SuppressWarnings ( "resource" )
public static void main ( String[ ] args) {
Scanner sc= new Scanner ( System. in) ;
System. out. println ( "请输入您的薪水:" ) ;
double dnum= sc. nextDouble ( ) ;
double num= dnum- 2000 ;
int types= getTypes ( num) ;
double ynum= getYnum ( types, num) ;
System. out. println ( "您的收入:" + dnum) ;
System. out. println ( "您应该纳税部分:" + num) ;
System. out. println ( "您的税收为:" + ynum) ;
if ( ynum<= 0 ) {
System. out. println ( "纳税光荣,请加油" ) ;
}
}
private static double getYnum ( int types, double num) {
double nums= 0 ;
switch ( types) {
case 9 : nums= ( num- 100000 ) * 0.45 ;
num= 100000 ;
case 8 : nums+= ( num- 80000 ) * 0.4 ;
num= 80000 ;
case 7 : nums+= ( num- 60000 ) * 0.35 ;
num= 60000 ;
case 6 : nums+= ( num- 40000 ) * 0.3 ;
num= 40000 ;
case 5 : nums+= ( num- 20000 ) * 0.25 ;
num= 20000 ;
case 4 : nums+= ( num- 5000 ) * 0.2 ;
num= 5000 ;
case 3 : nums+= ( num- 2000 ) * 0.15 ;
num= 2000 ;
case 2 : nums+= ( num- 500 ) * 0.1 ;
num= 500 ;
case 1 : nums+= ( num) * 0.05 ;
break ;
}
return nums;
}
private static int getTypes ( double num) {
int types= 0 ;
if ( num> 100000 ) {
types= 9 ;
} else if ( num> 80000 ) {
types= 8 ;
} else if ( num> 60000 ) {
types= 7 ;
} else if ( num> 40000 ) {
types= 6 ;
} else if ( num> 20000 ) {
types= 5 ;
} else if ( num> 5000 ) {
types= 4 ;
} else if ( num> 2000 ) {
types= 3 ;
} else if ( num> 500 ) {
types= 2 ;
} else if ( num> 0 ) {
types= 1 ;
}
return types;
}
}
14、判断这这一天是这一年的第几天
public class Test14 {
public static void main ( String[ ] args) {
int year, month, day;
int days = 0 ;
int d = 0 ;
int e;
input fymd = new input ( ) ;
do {
e = 0 ;
System. out. print ( "输入年:" ) ;
year = fymd. in ( ) ;
System. out. print ( "输入月:" ) ;
month = fymd. in ( ) ;
System. out. print ( "输入天:" ) ;
day = fymd. in ( ) ;
if ( year< 0 || month< 0 || month> 12 || day< 0 || day> 31 ) {
System. out. println ( "输入错误,请重新输入!" ) ;
e = 1 ;
}
} while ( e == 1 ) ;
for ( int i = 1 ; i < month; i++ ) {
switch ( i) {
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
days = 31 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
days = 30 ;
break ;
case 2 :
if ( ( year% 400 == 0 ) || ( year% 4 == 0 && year% 100 != 0 ) ) {
days = 29 ;
} else {
days = 28 ;
}
break ;
}
d += days;
}
System. out. println ( year+ "-" + month+ "-" + day+ "是这年的第" + ( d+ day) + "天。" ) ;
}
}
class input {
public int in ( ) {
int value = 0 ;
@SuppressWarnings ( "resource" )
Scanner s = new Scanner ( System. in) ;
value = s. nextInt ( ) ;
return value;
}
}
15、升序输出
import java. util. Scanner;
public class Test15 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入x的值:" ) ;
int x= in. nextInt ( ) ;
System. out. println ( "请输入y的值:" ) ;
int y= in. nextInt ( ) ;
System. out. println ( "请输入z的值:" ) ;
int z= in. nextInt ( ) ;
System. out. println ( "原数值:" + x+ "," + y+ "," + z) ;
if ( x> y) {
int t= x;
x= y;
y= t;
}
if ( x> z) {
int t= x;
x= z;
z= t;
}
if ( y> z) {
int t= y;
y= z;
z= t;
}
System. out. println ( "排序后:" + x+ "," + y+ "," + z) ;
}
}
16、九九乘法表
public class Test16 {
public static void main ( String[ ] args) {
for ( int i= 1 ; i< 10 ; i++ ) {
for ( int j= 1 ; j< i+ 1 ; j++ ) {
System. out. print ( j+ "*" + i+ "=" + j* i+ " " ) ;
if ( j* i< 10 ) {
System. out. print ( " " ) ;
}
}
System. out. println ( "" ) ;
}
}
}
17、猴子问题一
public class Test17 {
public static void main ( String[ ] args) {
int x= 1 ;
for ( int i= 0 ; i< 9 ; i++ ) {
x= ( x+ 1 ) * 2 ;
}
System. out. println ( "第一天共摘了" + x+ "个" ) ;
}
}
18、猴子问题二
public class Test18 {
public static void main ( String[ ] args) {
int t, count, x= 0 ;
for ( int i= 4 ; i< 10000 ; i++ ) {
count= 1 ;
t= i;
for ( int j= 0 ; j< 5 ; j++ ) {
x= i/ 4 * 5 + 1 ;
i= x;
if ( x% 4 == 0 ) {
count++ ;
} else {
break ;
}
}
i= t;
if ( count== 5 ) {
System. out. println ( "原有桃子:" + x) ;
break ;
}
}
}
}
19、输出棱形
import java. util. Scanner;
public class Test19 {
public static void main ( String[ ] args) {
int h= 7 , w= 7 ;
for ( int i= 0 ; i< ( h+ 1 ) / 2 ; i++ ) {
for ( int j= 0 ; j< w/ 2 - i; j++ ) {
System. out. print ( " " ) ;
}
for ( int k= 1 ; k< ( i+ 1 ) * 2 ; k++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( "" ) ;
}
for ( int i= 0 ; i< h/ 2 ; i++ ) {
for ( int j= 0 ; j< i+ 1 ; j++ ) {
System. out. print ( " " ) ;
}
for ( int k= 1 ; k< ( h/ 2 - i) * 2 ; k++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( "" ) ;
}
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入高:" ) ;
int h2= in. nextInt ( ) ;
for ( int i= 1 ; i<= h2; i++ ) {
int m= ( i<= ( h2+ 1 ) / 2 ) ? i: ( h2+ 1 - i) ;
for ( int k= ( h2+ 1 ) / 2 - m; k> 0 ; k-- ) {
System. out. print ( " " ) ;
}
for ( int j= 1 ; j< m* 2 ; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
}
20、输出三角形
public class Test56 {
public static void main ( String[ ] args) {
for ( int i= 1 ; i<= 5 ; i++ ) {
for ( int j= 1 ; j< 2 * i; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
}
21、输出三角形二
import java. util. Scanner;
public class Test57 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. print ( "请输入三角形的高度:" ) ;
int h = in. nextInt ( ) ;
for ( int i= 1 ; i<= h; i++ ) {
for ( int j= h- i; j> 0 ; j-- ) {
System. out. print ( " " ) ;
}
for ( int j= 1 ; j< 2 * i; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
}
22、阶乘相加
public class Test21 {
public static void main ( String[ ] args) {
new sum ( ) ;
long sum= 0 ;
long flg= 1 ;
for ( int i= 1 ; i<= 20 ; i++ ) {
flg= flg* i;
sum+= flg;
}
System. out. println ( "1+2!+3!+...+20!=" + sum) ;
}
}
class sum {
sum ( ) {
long sum= 0 ;
for ( int i= 1 ; i<= 20 ; i++ ) {
sum+= fun ( i) ;
}
System. out. println ( "1+2!+3!+...+20!=" + sum) ;
}
long fun ( int n) {
if ( n> 1 )
return n* fun ( n- 1 ) ;
return 1 ;
}
}
23、递归求阶乘
public class Test22 {
public static void main ( String[ ] args) {
int n= 5 ;
Res res= new Res ( ) ;
System. out. println ( n+ "!=" + res. res ( n) ) ;
}
}
class Res {
public long res ( int n) {
long value= 0 ;
if ( n== 1 ) {
value= 1 ;
} else {
value= n* res ( n- 1 ) ;
}
return value;
}
}
24、逆序输出一
import java. util. Scanner;
public class Test24 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个正整数" ) ;
long n= in. nextLong ( ) ;
String str= Long. toString ( n) ;
char [ ] ch= str. toCharArray ( ) ;
int k= ch. length;
System. out. println ( "它是一个" + k+ "位数" ) ;
for ( int i= k- 1 ; i>= 0 ; i-- ) {
System. out. print ( ch[ i] ) ;
}
}
}
25、逆序输出二
import java. util. Scanner;
public class Test53 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner sc= new Scanner ( System. in) ;
System. out. println ( "请输入1个数字:" ) ;
int n= sc. nextInt ( ) ;
int num= 0 ;
for ( int i= n; i!= 0 ; ) {
int x= i% 10 ;
num= num* 10 + x;
System. out. println ( "num=" + num+
" i=" + i+ " x=" + x) ;
i/= 10 ;
}
System. out. println ( n+ "::::" + num) ;
}
}
26、逆序输出三
import java. util. Scanner;
public class Test31 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "输入多个整数(输入-1结束)" ) ;
int [ ] a= new int [ 20 ] ;
int i= 0 , j;
do {
a[ i] = in. nextInt ( ) ;
i++ ;
} while ( a[ i- 1 ] != - 1 ) ;
System. out. println ( "正序输出:" ) ;
for ( j= 0 ; j< i- 1 ; j++ ) {
System. out. print ( a[ j] + " " ) ;
}
System. out. println ( ) ;
System. out. println ( "逆序输出:" ) ;
for ( j= i- 2 ; j>= 0 ; j-- ) {
System. out. print ( a[ j] + " " ) ;
}
}
}
27、求素数
public class Test27 {
public static void main ( String[ ] args) {
boolean b= false ;
System. out. println ( "100以内的素数有:" ) ;
System. out. print ( 2 + " " ) ;
System. out. print ( 3 + " " ) ;
for ( int i= 3 ; i<= 100 ; i+= 2 ) {
for ( int j= 2 ; j<= Math. sqrt ( i) ; j++ ) {
if ( i% j== 0 ) {
b= false ;
break ;
} else {
b= true ;
}
}
if ( b== true ) {
System. out. print ( i+ " " ) ;
}
}
System. out. println ( "" ) ;
int [ ] a= new int [ ] { 2 , 3 , 5 , 7 } ;
for ( int i= 0 ; i< 4 ; i++ ) {
System. out. print ( a[ i] + " " ) ;
}
boolean b2= false ;
for ( int i= 11 ; i< 100 ; i+= 2 ) {
for ( int j= 0 ; j< 4 ; j++ ) {
if ( i% a[ j] == 0 ) {
b2= false ;
break ;
} else {
b2= true ;
}
}
if ( b2== true ) {
System. out. print ( i+ " " ) ;
}
}
}
}
28、排序
import java. util. Scanner;
public class Test28 {
public static int [ ] BarnarySort ( int [ ] data) {
int [ ] temp= new int [ data. length] ;
for ( int i= 0 ; i< temp. length; i++ ) {
if ( i== 0 ) {
temp[ i] = data[ i] ;
} else {
for ( int j= 0 , k= i- 1 ; j< i&& k>= 0 ; ) {
if ( temp[ ( j+ k) / 2 ] >= data[ i] ) {
if ( ( j+ k) / 2 == 0 ) {
for ( int iter= i; iter> 0 ; iter-- ) {
temp[ iter] = temp[ iter- 1 ] ;
}
temp[ 0 ] = data[ i] ;
break ;
} else if ( temp[ ( j+ k) / 2 - 1 ] <= data[ i] ) {
for ( int iter= i; iter> ( j+ k) / 2 ; iter-- ) {
temp[ iter] = temp[ iter- 1 ] ;
}
temp[ ( j+ k) / 2 ] = data[ i] ;
break ;
} else {
k= ( k+ j) / 2 - 1 ;
}
} else if ( temp[ ( j+ k) / 2 ] < data[ i] ) {
if ( ( j+ k) / 2 == i- 1 ) {
temp[ i] = data[ i] ;
break ;
} else {
j= ( k+ j) / 2 + 1 ;
}
}
}
}
}
return temp;
}
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入10个数:" ) ;
int [ ] num= new int [ 10 ] ;
for ( int i= 0 ; i< num. length; i++ ) {
num[ i] = in. nextInt ( ) ;
}
int [ ] arr= BarnarySort ( num) ;
for ( int i= 0 ; i< num. length; i++ ) {
System. out. print ( arr[ i] + " " ) ;
}
}
}
29、矩阵对角线和
import java. util. Scanner;
public class Test29 {
public static void main ( String[ ] args) {
double [ ] [ ] a= new double [ 3 ] [ 3 ] ;
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
for ( int i= 0 ; i< 3 ; i++ ) {
System. out. println ( "请输入第" + ( i+ 1 ) + "行元素" ) ;
for ( int j= 0 ; j< 3 ; j++ ) {
a[ i] [ j] = in. nextDouble ( ) ;
}
}
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ ) {
System. out. print ( a[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
double sum= 0 ;
sum= a[ 0 ] [ 0 ] + a[ 1 ] [ 1 ] + a[ 2 ] [ 2 ] + a[ 0 ] [ 2 ] + a[ 2 ] [ 0 ] ;
System. out. println ( "对角线之和等于:" + sum) ;
}
}
30、数组的插入
import java. util. Scanner;
public class Test30 {
public static void main ( String[ ] args) {
int [ ] a= { 2 , 3 , 4 , 5 , 16 , 17 , 28 , 29 } ;
int [ ] b= new int [ a. length+ 1 ] ;
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个数:" ) ;
int n= in. nextInt ( ) ;
if ( n> a[ a. length- 1 ] ) {
b[ b. length- 1 ] = n;
for ( int i= 0 ; i< b. length- 1 ; i++ ) {
b[ i] = a[ i] ;
}
} else if ( n< a[ 0 ] ) {
b[ 0 ] = n;
for ( int i= 0 ; i< a. length; i++ ) {
b[ i+ 1 ] = a[ i] ;
}
} else {
int k;
for ( k= 0 ; k< a. length; k++ ) {
if ( n> a[ k] ) {
b[ k] = a[ k] ;
} else {
b[ k] = n;
break ;
}
}
for ( int j= k+ 1 ; j< b. length; j++ ) {
b[ j] = a[ j- 1 ] ;
}
}
for ( int i= 0 ; i< b. length; i++ ) {
System. out. print ( b[ i] + " " ) ;
}
}
public static void insert ( int [ ] arr, int [ ] arrCopy, int lastIndex, int targetIndex, int val) {
for ( int i= lastIndex; i> targetIndex; i-- ) {
arrCopy[ i] = arr[ i- 1 ] ;
}
arrCopy[ targetIndex] = val;
for ( int i= 0 ; i< targetIndex; i++ ) {
arrCopy[ i] = arr[ i] ;
}
}
}
31、输入字母判断星期
import java. util. Scanner;
public class Test26 {
public static void main ( String[ ] args) {
getChar tw = new getChar ( ) ;
System. out. println ( "请输入星期的第一个大写字母:" ) ;
char ch = tw. get ( ) ;
switch ( ch) {
case 'M' :
System. out. println ( "Monday" ) ;
break ;
case 'W' :
System. out. println ( "Wednesday" ) ;
break ;
case 'F' :
System. out. println ( "Friday" ) ;
break ;
case 'T' :
System. out. println ( "请输入星期的第二个字母:" ) ;
char ch2 = tw. get ( ) ;
if ( ch2== 'U' ) {
System. out. println ( "Tuesday" ) ;
} else if ( ch2== 'H' ) {
System. out. println ( "Thursday" ) ;
} else {
System. out. println ( "无此写法!" ) ;
}
break ;
case 'S' :
System. out. println ( "请输入星期的第二个字母:" ) ;
char ch3 = tw. get ( ) ;
if ( ch3== 'A' ) {
System. out. println ( "Saturday" ) ;
} else if ( ch3== 'U' ) {
System. out. println ( "Sunday" ) ;
} else {
System. out. println ( "无此写法!" ) ;
}
break ;
default : System. out. println ( "无此写法!" ) ;
}
}
}
class getChar {
public char get ( ) {
@SuppressWarnings ( "resource" )
Scanner s = new Scanner ( System. in) ;
String str = s. nextLine ( ) ;
char ch = str. charAt ( 0 ) ;
if ( ch< 'A' || ch> 'Z' ) {
System. out. println ( "输入错误,请重新输入" ) ;
ch= get ( ) ;
}
return ch;
}
}
32、取位数
import java. util. Scanner;
public class Test32 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个七位以上的整数:" ) ;
long num= in. nextLong ( ) ;
String str= Long. toString ( num) ;
char [ ] ch= str. toCharArray ( ) ;
int k= 4 ;
int n= ch. length;
if ( n< 7 ) {
System. out. println ( "输入错误" ) ;
} else {
for ( int i= n- 1 ; i> n- 4 - 1 ; i-- ) {
System. out. println ( "第" + k+ "位" + ch[ i- 3 ] ) ;
k++ ;
}
System. out. println ( "截取从右端开始的4~7位是:" + ch[ n- 7 ] + ch[ n- 6 ] + ch[ n- 5 ] + ch[ n- 4 ] ) ;
}
}
}
33、杨辉三角形
public class Test33 {
public static void main ( String[ ] args) {
int [ ] [ ] a= new int [ 10 ] [ 10 ] ;
for ( int i= 0 ; i< 10 ; i++ ) {
a[ i] [ i] = 1 ;
a[ i] [ 0 ] = 1 ;
}
for ( int i= 2 ; i< 10 ; i++ ) {
for ( int j= 1 ; j< i; j++ ) {
a[ i] [ j] = a[ i- 1 ] [ j- 1 ] + a[ i- 1 ] [ j] ;
}
}
for ( int i= 0 ; i< 10 ; i++ ) {
for ( int j= 0 ; j< 2 * ( 10 - i) - 1 ; j++ ) {
System. out. print ( " " ) ;
}
for ( int j= 0 ; j<= i; j++ ) {
System. out. print ( a[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
}
}
34、顺序输出
import java. util. Scanner;
public class Test34 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入a的值:" ) ;
int a= in. nextInt ( ) ;
System. out. println ( "请输入b的值:" ) ;
int b= in. nextInt ( ) ;
System. out. println ( "请输入c的值:" ) ;
int c= in. nextInt ( ) ;
int max, min, mid = 0 ;
if ( a> b) {
int t= a;
a= b;
b= t;
}
if ( a> c) {
int t= a;
a= c;
c= t;
}
if ( b> c) {
int t= b;
b= c;
c= t;
}
min= a;
mid= b;
max= c;
System. out. println ( "从小到大输出:" + min+ " " + mid+ " " + max) ;
System. out. println ( "从大到小输出:" + max+ " " + mid+ " 1" + min) ;
}
}
35、数组交换
public class Test35 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入数组的长度" ) ;
int len= in. nextInt ( ) ;
int [ ] arr= new int [ len] ;
for ( int i= 0 ; i< len; i++ ) {
System. out. println ( "请输入第" + ( i+ 1 ) + "个数" ) ;
arr[ i] = in. nextInt ( ) ;
}
System. out. println ( "原数组:" ) ;
for ( int i= 0 ; i< len; i++ ) {
System. out. print ( arr[ i] + " " ) ;
}
int max= 0 , min= 999999 ;
for ( int i= 0 ; i< len; i++ ) {
max= max> arr[ i] ? max: arr[ i] ;
}
for ( int i= 0 ; i< len; i++ ) {
min= min< arr[ i] ? min: arr[ i] ;
}
for ( int i= 0 ; i< len; i++ ) {
if ( max== arr[ i] ) {
arr[ i] = arr[ 0 ] ;
arr[ 0 ] = max;
}
if ( min== arr[ i] ) {
arr[ i] = arr[ len- 1 ] ;
arr[ len- 1 ] = min;
}
}
System. out. println ( ) ;
System. out. println ( "交换后数组:" ) ;
for ( int i= 0 ; i< len; i++ ) {
System. out. print ( arr[ i] + " " ) ;
}
}
}
36、数组移位
import java. util. Scanner;
public class Test36 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入n:" ) ;
int n= in. nextInt ( ) ;
int m;
do {
System. out. println ( "请输入m(m<n):" ) ;
m= in. nextInt ( ) ;
} while ( n< m) ;
int [ ] arr= new int [ n] ;
int [ ] arrCopy= new int [ n] ;
for ( int i= 0 ; i< n; i++ ) {
System. out. println ( "请输入第" + ( i+ 1 ) + "个数" ) ;
arr[ i] = in. nextInt ( ) ;
}
System. out. println ( "原数为:" ) ;
for ( int i= 0 ; i< n; i++ ) {
System. out. print ( arr[ i] + " " ) ;
}
System. out. println ( ) ;
for ( int i= 0 ; i< n- m; i++ ) {
arrCopy[ i+ m] = arr[ i] ;
}
int k= 0 ;
for ( int i= m- 1 ; i>= 0 ; i-- ) {
arrCopy[ i] = arr[ n- 1 - k] ;
k++ ;
}
System. out. println ( "移动后的数为:" ) ;
for ( int i= 0 ; i< n; i++ ) {
System. out. print ( arrCopy[ i] + " " ) ;
}
}
}
37、n人围成圈问题
public class Test37 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入围成圈的人数,最少2人" ) ;
int n= in. nextInt ( ) ;
boolean [ ] arr= new boolean [ n] ;
for ( int i= 0 ; i< arr. length; i++ ) {
arr[ i] = true ;
}
int leftNum= n;
int num= 0 ;
int index= 0 ;
while ( leftNum> 1 ) {
if ( arr[ index] == true ) {
num++ ;
}
if ( num== 3 ) {
arr[ index] = false ;
num= 0 ;
leftNum-- ;
}
index++ ;
if ( index== n) {
index= 0 ;
}
}
for ( int i= 0 ; i< arr. length; i++ ) {
if ( arr[ i] == true ) {
System. out. println ( "最后留下的是第" + ( i+ 1 ) + "号" ) ;
}
}
}
}
38、求字符串长度
import java. util. Scanner;
public class Test38 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个字符串:" ) ;
String str= in. nextLine ( ) ;
int l= len ( str) ;
System. out. println ( "字符串的长度为:" + l) ;
System. out. println ( "字符串的长度为:" + str. length ( ) ) ;
}
static int len ( String str) {
char [ ] ch= str. toCharArray ( ) ;
int k= 0 ;
for ( int i= 0 ; i< ch. length; i++ ) {
k++ ;
}
return k;
}
}
39、偶数和、奇数和
import java. util. Scanner;
public class Test39 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in = new Scanner ( System. in) ;
System. out. println ( "请输入一个数" ) ;
int n = in. nextInt ( ) ;
System. out. println ( sum ( n) ) ;
}
static double sum ( int n) {
double sum = 0 ;
if ( n % 2 == 0 ) {
for ( int i = 2 ; i <= n; i += 2 ) {
sum += ( double ) 1 / i;
}
} else {
for ( int i = 1 ; i <= n; i += 2 ) {
sum += ( double ) 1 / i;
}
}
return sum;
}
}
40、字符串排序
import java. util. Arrays;
import java. util. Scanner;
public class Test40 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in = new Scanner ( System. in) ;
System. out. println ( "请输入一个字符串" ) ;
String str= in. nextLine ( ) ;
char [ ] ch= str. toCharArray ( ) ;
Arrays. sort ( ch) ;
for ( int i= 0 ; i< ch. length; i++ ) {
System. out. println ( ch[ i] ) ;
}
}
}
41、比赛排名单问题
public class Test41 {
public static void main ( String[ ] args) {
char [ ] m= { 'a' , 'b' , 'c' } ;
char [ ] n= { 'x' , 'y' , 'z' } ;
for ( int i= 0 ; i< m. length; i++ ) {
for ( int j= 0 ; j< n. length; j++ ) {
if ( m[ i] == 'a' && n[ j] == 'x' || m[ i] == 'a' && n[ j] == 'y' ) {
continue ;
} else if ( m[ i] == 'c' && n[ j] == 'x' || m[ i] == 'c' && n[ j] == 'z' ) {
continue ;
} else if ( m[ i] == 'b' && n[ j] == 'y' || m[ i] == 'b' && n[ j] == 'z' ) {
continue ;
} else {
System. out. println ( m[ i] + " VS " + n[ j] ) ;
}
}
}
}
}
42、恒等式
public class Test42 {
public static void main ( String[ ] args) {
int x= 0 ;
for ( int i= 10 ; i< 13 ; i++ ) {
x= ( 8 * i< 100 && 9 * i> 99 ) ? i: x;
if ( x!= 0 ) {
String str= 800 + "*" + x+ "=" + 800 + "*" + x+ "+" + 9 + "*" + x;
System. out. println ( str+ "=" + 809 * x) ;
}
}
}
}
43、求n个数组成奇数个数
public class Test43 {
public static void main ( String[ ] args) {
int x= 4 , sum= 0 ;
System. out. println ( "组成1位数是:" + x) ;
sum+= x;
x*= 7 ;
sum+= x;
System. out. println ( "组成2位数是:" + x) ;
for ( int i= 3 ; i<= 7 ; i++ ) {
System. out. println ( "组成" + i+ "位数是:" + ( x*= 8 ) ) ;
sum+= x;
}
System. out. println ( "0—7所能组成的奇数个数为:" + sum) ;
}
}
44、两个素数和表示偶数
import java. util. Scanner;
public class Test44 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
int x;
do {
System. out. println ( "请输入一个大于或等于6偶数" ) ;
x= in. nextInt ( ) ;
} while ( x< 6 || x% 2 != 0 ) ;
for ( int i= 2 ; i< 10000 ; i++ ) {
if ( getSu ( i) == true && getSu ( x- i) == true ) {
System. out. println ( x+ "=" + i+ "+" + ( x- i) ) ;
}
}
}
public static boolean getSu ( int num) {
boolean flag = false ;
if ( num== 2 || num== 3 )
flag = true ;
for ( int i= 2 ; i<= Math. sqrt ( num) ; i++ ) {
if ( num% i== 0 ) {
flag= false ;
break ;
} else {
flag= true ;
}
}
return flag;
}
}
45、判断某个数能被几个某数整除
import java. util. Scanner;
public class Test45 {
@SuppressWarnings ( "unused" )
public static void main ( String[ ] args) {
int t, count= 0 ;
boolean bl= true ;
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个数字" ) ;
int x= in. nextInt ( ) ;
t= x;
do {
if ( x% 9 == 0 ) {
x/= 9 ;
count++ ;
} else {
bl= false ;
}
} while ( bl) ;
x= t;
System. out. println ( x+ "能被" + count+ "个9整除" ) ;
count= 0 ;
for ( int i= 0 ; x% 9 == 0 ; ) {
x = x/ 9 ;
count ++ ;
}
x= t;
System. out. println ( x+ "能被" + count+ "个9整除" ) ;
}
}
46、字符串连接
import java. util. Scanner;
public class Test46 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入第一个字符串:" ) ;
String str1= in. nextLine ( ) ;
System. out. println ( "请输入第二个字符串:" ) ;
String str2= in. nextLine ( ) ;
String str= str1+ str2;
System. out. println ( "第一个字符串为:" + str1) ;
System. out. println ( "第二个字符串为:" + str2) ;
System. out. println ( "连接后的字符串为:" + str) ;
}
}
47、打印随机数
public class Test47 {
public static void main ( String[ ] args) {
for ( int i= 0 ; i< 7 ; i++ ) {
int num= ( int ) ( Math. random ( ) * 50 + 1 ) ;
System. out. println ( num) ;
for ( int j= 0 ; j< num; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
@SuppressWarnings ( "resource" )
Scanner s = new Scanner ( System. in) ;
int n = 1 , num;
while ( n <= 7 ) {
do {
System. out. print ( "请输入一个1--50之间的整数:" ) ;
num = s. nextInt ( ) ;
} while ( num < 1 || num > 50 ) ;
for ( int i = 1 ; i <= num; i++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
n++ ;
}
}
}
48、加密问题
import java. util. Scanner;
public class Test48 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner s = new Scanner ( System. in) ;
int num2 = 0 , temp;
do {
System. out. print ( "请输入一个4位正整数:" ) ;
num2 = s. nextInt ( ) ;
} while ( num2 < 1000 || num2 > 9999 ) ;
int a[ ] = new int [ 4 ] ;
a[ 0 ] = num2 / 1000 ;
a[ 1 ] = ( num2 / 100 ) % 10 ;
a[ 2 ] = ( num2 / 10 ) % 10 ;
a[ 3 ] = num2 % 10 ;
for ( int j = 0 ; j < 4 ; j++ ) {
a[ j] += 5 ;
a[ j] %= 10 ;
}
for ( int j = 0 ; j <= 1 ; j++ ) {
temp = a[ j] ;
a[ j] = a[ 3 - j] ;
a[ 3 - j] = temp;
}
System. out. print ( "加密后的数字为:" ) ;
for ( int j = 0 ; j < 4 ; j++ )
System. out. print ( a[ j] ) ;
}
}
49、计算字符串中子串数
import java. util. Scanner;
public class Test49 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个字符串:" ) ;
String str= in. nextLine ( ) ;
System. out. println ( "请输入子串:" ) ;
String strZ= in. nextLine ( ) ;
int count= 0 ;
if ( str. equals ( "" ) || str. equals ( "" ) ) {
System. out. println ( "字符串或子串为空,无法进行比较" ) ;
System. exit ( 0 ) ;
} else {
for ( int i= 0 ; i<= str. length ( ) - strZ. length ( ) ; i++ ) {
if ( strZ. equals ( str. substring ( i, strZ. length ( ) + i) ) ) {
count++ ;
}
}
System. out. println ( "子串在母串中出现的次数为:" + count) ;
}
}
}
50、信息输入与写入
import java. io. BufferedWriter;
import java. io. File;
import java. io. FileWriter;
import java. util. Scanner;
public class Test50 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
String [ ] [ ] strArr= new String [ 5 ] [ 6 ] ;
double sum= 0 ;
for ( int i= 1 ; i<= 5 ; i++ ) {
System. out. println ( "请输入第" + i+ "个学生的学号" ) ;
strArr[ i- 1 ] [ 0 ] = in. nextLine ( ) ;
System. out. println ( "请输入第" + i+ "个学生的姓名" ) ;
strArr[ i- 1 ] [ 1 ] = in. nextLine ( ) ;
for ( int j= 1 ; j<= 3 ; j++ ) {
System. out. println ( "请输入第" + i+ "个学生的第" + j+ "门成绩" ) ;
strArr[ i- 1 ] [ j+ 1 ] = in. nextLine ( ) ;
double d= Double. valueOf ( strArr[ i- 1 ] [ j+ 1 ] ) . doubleValue ( ) ;
sum+= d;
}
sum= sum/ 3 ;
strArr[ i- 1 ] [ 5 ] = "" + sum;
sum= 0 ;
System. out. println ( ) ;
}
String str= "学号\t" + "姓名\t" + "成绩一\t" + "成绩二\t" + "成绩三\t" + "平均分\t" ;
System. out. println ( str) ;
for ( int i= 0 ; i< 5 ; i++ ) {
for ( int j= 0 ; j< 6 ; j++ ) {
System. out. print ( strArr[ i] [ j] + "\t" ) ;
}
System. out. println ( ) ;
}
String s1;
try {
File f = new File ( "E:\\stud.txt" ) ;
if ( f. exists ( ) ) {
System. out. println ( "文件存在" ) ;
} else {
System. out. println ( "文件不存在,正在创建文件" ) ;
f. createNewFile ( ) ;
}
BufferedWriter output = new BufferedWriter ( new FileWriter ( f) ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
for ( int j = 0 ; j < 6 ; j++ ) {
s1 = strArr[ i] [ j] + "\r\n" ;
output. write ( s1) ;
}
}
output. close ( ) ;
System. out. println ( "数据已写入c盘文件stud中!" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
51、猜价格获取商品程序
import java. util. Random;
import java. util. Scanner;
public class Test51 {
@SuppressWarnings ( "resource" )
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "猜价格游戏准备开始" ) ;
Random random= new Random ( ) ;
int num= random. nextInt ( 1001 ) ;
int i= 1 ;
for ( ; i<= 10 ; i++ ) {
System. out. println ( "请猜第" + i+ "次:" ) ;
int x= sc. nextInt ( ) ;
if ( x== num) {
System. out. println ( "猜对啦,商品拿回去" ) ;
break ;
} else if ( x< num) {
System. out. println ( "小啦,请大点。" ) ;
} else {
System. out. println ( "大啦,请小点." ) ;
}
}
if ( i> 10 )
System. out. println ( "你out啦" ) ;
System. out. println ( "商品的价格为:" + num) ;
}
}
52、请输出1到m内n的倍数和含有n的数字
import java. util. Scanner;
public class Test52 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. print ( "请输入n:" ) ;
int n = in. nextInt ( ) ;
System. out. print ( "请输入m:" ) ;
int m = in. nextInt ( ) ;
for ( int i= 1 ; i<= m ; i++ ) {
if ( i% n== 0 || i% 10 == n || i/ 10 == n) {
System. out. println ( i) ;
}
}
}
}
53、判断回文
import java. util. Scanner;
public class Test25 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner in= new Scanner ( System. in) ;
System. out. println ( "请输入一个5位数:" ) ;
int n= in. nextInt ( ) ;
int one, two, four, five;
one= n% 10 ;
two= ( n/ 10 ) % 10 ;
four= ( n/ 1000 ) % 10 ;
five= n/ 10000 ;
if ( one== five&& two== four) {
System. out. println ( "这是一个回文数" ) ;
} else {
System. out. println ( "这不是一个回文数" ) ;
}
int n2;
do {
System. out. println ( "请输入一个5位数:" ) ;
n2= in. nextInt ( ) ;
} while ( n2> 99999 || n2< 10000 ) ;
String str= String. valueOf ( n2) ;
char [ ] ch2= str. toCharArray ( ) ;
if ( ch2[ 0 ] == ch2[ 4 ] && ch2[ 1 ] == ch2[ 3 ] ) {
System. out. println ( "这是一个回文数" ) ;
} else {
System. out. println ( "这不是一个回文数" ) ;
}
boolean is= true ;
System. out. println ( "请输入一个正整数:" ) ;
long a= in. nextLong ( ) ;
String s= Long. toString ( a) ;
char [ ] c= s. toCharArray ( ) ;
int k= c. length;
for ( int i= 0 ; i< k/ 2 ; i++ ) {
if ( c[ i] != c[ k- i- 1 ] ) {
is= false ;
}
}
if ( is== true ) {
System. out. println ( "这是一个回文数" ) ;
} else {
System. out. println ( "这不是一个回文数" ) ;
}
}
}
54、完全平方数
public class Test13 {
public static void main ( String[ ] args) {
for ( int i= 1 ; i< 10000 ; i++ ) {
if ( Math. sqrt ( i+ 100 ) % 1 == 0 ) {
if ( Math. sqrt ( i+ 168 ) % 1 == 0 ) {
System. out. println ( "该数是" + i) ;
}
}
}
}
}
55、循环嵌套
public class Test55 {
public static void main ( String[ ] args) {
for ( int i= 1 ; i<= 4 ; i++ ) {
System. out. println ( "年复一年=" + i) ;
for ( int j= 1 ; j<= 12 ; j++ ) {
System. out. println ( "月复一月=" + i+ ":" + j) ;
}
}
}
}
56、数列的前n项和问题
public class Test20 {
public static void main ( String[ ] args) {
int [ ] m= new int [ 20 ] ;
m[ 0 ] = 2 ;
m[ 1 ] = 3 ;
for ( int i= 2 ; i< m. length; i++ ) {
m[ i] = m[ i- 1 ] + m[ i- 2 ] ;
}
int [ ] n= new int [ 20 ] ;
n[ 0 ] = 1 ;
n[ 1 ] = 2 ;
for ( int i= 2 ; i< n. length; i++ ) {
n[ i] = n[ i- 1 ] + n[ i- 2 ] ;
}
double sum= 0 ;
for ( int i= 0 ; i< m. length; i++ ) {
sum+= ( double ) m[ i] / n[ i] ;
}
System. out. println ( "前20项之和等于:" + sum) ;
int x= 2 , y= 1 , t;
double sum2= 0 ;
for ( int i= 0 ; i< 20 ; i++ ) {
sum2= sum2+ ( double ) x/ y;
t= y;
y= x;
x= y+ t;
}
System. out. println ( "前20项之和等于:" + sum2) ;
}
}
57、岁数问题
public class Test23 {
public static void main ( String[ ] args) {
int age= 10 ;
for ( int i= 2 ; i<= 5 ; i++ ) {
age+= 2 ;
}
System. out. println ( "第五个人" + age+ "岁" ) ;
}
}
58、验证码
import java. util. Arrays;
import java. util. Random;
public class Test58 {
public static void main ( String[ ] args) {
char [ ] arrx= { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' } ;
char [ ] arry= new char [ 4 ] ;
Random ran= new Random ( ) ;
System. out. println ( Arrays. toString ( arrx) ) ;
for ( int i= 0 ; i< arry. length; i++ ) {
int index= ran. nextInt ( arrx. length- i) ;
arry[ i] = arrx[ index] ;
arrx[ index] = arrx[ arrx. length- 1 - i] ;
arrx[ arrx. length- 1 - i] = arry[ i] ;
}
System. out. println ( Arrays. toString ( arrx) ) ;
System. out. println ( Arrays. toString ( arry) ) ;
}
}
59、微计算器
import java. util. Scanner;
public class Test59 {
public static void main ( String[ ] args) {
@SuppressWarnings ( "resource" )
Scanner sc= new Scanner ( System. in) ;
System. out. println ( "请您输入: 数字 运算符 数字" ) ;
int x= sc. nextInt ( ) ;
char ysf= sc. next ( ) . charAt ( 0 ) ;
int y= sc. nextInt ( ) ;
String s= "" + x+ ysf+ y+ "=" ;
if ( ysf == '+' ) {
s+= x+ y;
} else if ( ysf == '/' ) {
if ( y== 0 )
s= "除数不可以为零" ;
else
s+= x/ y;
}
else {
s= "您的运算符还在开发中,请联系:******" ;
}
System. out. println ( s) ;
}
}
60、简单购物车
import java. util. HashMap;
import java. util. Map;
import java. util. Scanner;
public class Test60 {
private Map< String, Integer> map;
public Test60 ( ) {
map= new HashMap < String, Integer> ( ) ;
}
public int buy ( String s, int num) {
if ( map. get ( s) != null) {
map. put ( s, map. get ( s) + num) ;
} else {
map. put ( s, num) ;
}
return num;
}
public Map< String, Integer> getList ( ) {
return map;
}
public int del ( String name) {
if ( map. remove ( name) == null) {
return 0 ;
}
return 1 ;
}
@SuppressWarnings ( "resource" )
public static void main ( String[ ] args) {
Scanner sc= new Scanner ( System. in) ;
System. out. println ( "请您购物:商品随便写" ) ;
Test60 t= new Test60 ( ) ;
boolean bl= true ;
while ( bl) {
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "1.购买商品: 商品名字 数量" ) ;
System. out. println ( "2.查询商品: 商品名字 or all" ) ;
System. out. println ( "3.删除商品: 商品名字" ) ;
System. out. println ( "4.去结账" ) ;
System. out. println ( "请您输入编号:1 or 2 or 3 or 4" ) ;
int x= sc. nextInt ( ) ;
switch ( x) {
case 1 :
System. out. println ( "请输入购买的 商品名字 数量:" ) ;
String s= sc. next ( ) ;
int num= sc. nextInt ( ) ;
int buynum= t. buy ( s, num) ;
System. out. println ( "购买成功:您购买" + s+ ":" + buynum+ "件" ) ;
break ;
case 2 :
System. out. println ( "请输入商品名字 or all:" ) ;
String name = sc. next ( ) ;
Map< String, Integer> list = t. getList ( ) ;
System. out. println ( "商品名字\t\t\t数量" ) ;
if ( "all" . equals ( name) ) {
for ( Map. Entry< String, Integer> m: list. entrySet ( ) ) {
System. out. println ( m. getKey ( ) + "\t\t\t" + m. getValue ( ) ) ;
}
if ( list. size ( ) == 0 ) {
System. out. println ( "您没有购买任何商品" ) ;
}
} else {
boolean noHas = true ;
for ( Map. Entry< String, Integer> m: list. entrySet ( ) ) {
if ( m. getKey ( ) . contains ( name) ) {
noHas = false ;
System. out. println ( m. getKey ( ) + "\t\t\t" + m. getValue ( ) ) ;
}
}
if ( noHas) {
System. out. println ( "您没有购买这个商品" ) ;
}
}
break ;
case 3 :
System. out. println ( "请输入要删除的商品名字 " ) ;
String delName= sc. next ( ) ;
if ( t. del ( delName) == 1 ) {
System. out. println ( "删除成功" ) ;
} else {
System. out. println ( "没有这个商品" ) ;
}
break ;
case 4 :
System. out. println ( "感谢您的使用:请微信支付" ) ;
bl= false ;
break ;
}
}
}
}