基本问题
2000ASCII码排序
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. next ( ) ;
int a = str. charAt ( 0 ) ;
int b = str. charAt ( 1 ) ;
int c = str. charAt ( 2 ) ;
int temp;
if ( b> a) {
temp = a;
a = b;
b = temp;
}
if ( c> a) {
temp = a;
a = c;
c = temp;
}
if ( c> b) {
temp = b;
b = c;
c = temp;
}
System. out. println ( ( char ) c+ " " + ( char ) b+ " " + ( char ) a) ;
}
}
}
2004成绩转换
import java. util. Scanner;
public class Main {
public static void main ( String args[ ] ) {
Scanner in= new Scanner ( System. in) ;
int score;
while ( in. hasNext ( ) ) {
score= in. nextInt ( ) ;
if ( score<= 100 && score>= 90 )
System. out. println ( "A" ) ;
else
if ( score>= 80 && score<= 89 )
System. out. println ( "B" ) ;
else
if ( score>= 70 && score<= 79 )
System. out. println ( "C" ) ;
else
if ( score>= 60 && score<= 69 )
System. out. println ( "D" ) ;
else
if ( score>= 0 && score<= 59 )
System. out. println ( "E" ) ;
else
System. out. println ( "Score is error!" ) ;
}
}
}
2005第几天
import java. util. Calendar;
import java. util. GregorianCalendar;
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
while ( in. hasNext ( ) ) {
String date = in. next ( ) ;
int year = Integer. valueOf ( date. substring ( 0 , 4 ) ) ;
int month = Integer. valueOf ( date. substring ( 5 , date. lastIndexOf ( '/' ) ) ) ;
int day = Integer. valueOf ( date. substring ( date. lastIndexOf ( '/' ) + 1 ) ) ;
Calendar calendar = new GregorianCalendar ( year, month- 1 , day) ;
System. out. println ( calendar. get ( Calendar. DAY_OF_YEAR) ) ;
}
}
}
2014青年歌手大赛—评委会打分
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int n;
while ( in. hasNext ( ) ) {
n= in. nextInt ( ) ;
int a[ ] ;
a= new int [ n] ;
for ( int i= 0 ; i< n; i++ ) {
a[ i] = in. nextInt ( ) ;
}
int ta, ti;
int min= a[ 0 ] ;
ti= 0 ;
int max= a[ 0 ] ;
ta= 0 ;
for ( int i= 1 ; i< n; i++ ) {
if ( a[ i] > max) {
max= a[ i] ;
ta= i;
}
if ( a[ i] < min) {
min= a[ i] ;
ti= i;
}
}
float sum= 0 ;
for ( int i= 0 ; i< n; i++ ) {
if ( i!= ti&& i!= ta) {
sum+= a[ i] ;
}
}
System. out. printf ( "%.2f" , sum/ ( n- 2 ) ) ;
System. out. println ( ) ;
}
}
}
数学问题
2001计算两点间的距离
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
float x1, y1, x2, y2, a, b, c;
while ( in. hasNext ( ) ) {
x1= in. nextFloat ( ) ;
y1= in. nextFloat ( ) ;
x2= in. nextFloat ( ) ;
y2= in. nextFloat ( ) ;
a= ( x2- x1) * ( x2- x1) ;
b= ( y2- y1) * ( y2- y1) ;
c= ( float ) Math. sqrt ( a+ b) ;
System. out. printf ( "%.2f" , c) ;
System. out. println ( ) ;
}
}
}
2002计算球体积
import java. util. Scanner;
public class Main {
public static final double PI= 3.1415927 ;
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
double r, v;
while ( in. hasNext ( ) ) {
r= in. nextDouble ( ) ;
v= ( 4 * PI* r* r* r) / 3 ;
System. out. printf ( "%.3f" , v) ;
System. out. println ( ) ;
}
}
}
2003求绝对值
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
double a;
while ( in. hasNext ( ) ) {
a= in. nextDouble ( ) ;
System. out. printf ( "%.2f" , Math. abs ( a) ) ;
System. out. println ( ) ;
}
}
}
2006求奇数成绩
import java. util. Scanner;
public class Main {
public static void main ( String args[ ] ) {
Scanner in= new Scanner ( System. in) ;
int N, num;
while ( in. hasNext ( ) ) {
int sum= 1 ;
N= in. nextInt ( ) ;
for ( ; N> 0 ; N-- ) {
num= in. nextInt ( ) ;
if ( num% 2 != 0 )
sum*= num;
}
System. out. println ( sum) ;
}
}
}
2008数值统计
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int n;
n= in. nextInt ( ) ;
while ( n!= 0 ) {
int i;
int a= 0 , b= 0 , c= 0 ;
float temp;
for ( i= 0 ; i< n; i++ ) {
temp= in. nextFloat ( ) ;
if ( temp> 0 )
c++ ;
else if ( temp< 0 )
a++ ;
else
b++ ;
}
System. out. printf ( "%d %d %d" , a, b, c) ;
System. out. println ( ) ;
n= in. nextInt ( ) ;
}
}
}
2009 求数列和
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
float n;
int m;
while ( in. hasNext ( ) ) {
n= in. nextInt ( ) ;
m= in. nextInt ( ) ;
int i;
float temp, sum= n;
for ( i= 0 ; i< m- 1 ; i++ ) {
temp= ( float ) Math. sqrt ( n) ;
n= temp;
sum+= temp;
}
System. out. printf ( "%.2f" , sum) ;
System. out. println ( ) ;
}
}
}
2010水仙花数
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int m, n;
while ( in. hasNext ( ) ) {
m= in. nextInt ( ) ;
n= in. nextInt ( ) ;
int i, j= 0 ;
int a[ ] ;
a= new int [ 900 ] ;
int bai, shi, ge;
for ( i= m; i<= n; i++ ) {
bai= i/ 100 ;
shi= ( i/ 10 ) % 10 ;
ge= i% 10 ;
if ( bai* bai* bai+ shi* shi* shi+ ge* ge* ge== i) {
a[ j++ ] = i;
}
}
if ( j== 0 ) {
System. out. println ( "no" ) ;
}
else {
int first= 1 ;
for ( j= 0 ; j< 900 ; j++ ) {
if ( a[ j] != 0 ) {
if ( first== 1 ) {
System. out. printf ( "%d" , a[ j] ) ;
first= 0 ;
}
else
System. out. printf ( " %d" , a[ j] ) ;
}
}
System. out. println ( ) ;
}
}
}
}
2011多项式求和
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int m, n, i= 0 ;
m= in. nextInt ( ) ;
for ( ; i< m; i++ ) {
float sum= 1 ;
n= in. nextInt ( ) ;
if ( n!= 1 ) {
for ( int j= 2 ; j<= n; j++ ) {
float temp;
temp= ( float ) ( 1.00 / j) ;
if ( j% 2 == 0 )
sum-= temp;
else
sum+= temp;
}
System. out. printf ( "%.2f" , sum) ;
System. out. println ( ) ;
}
else
System. out. println ( "1.00" ) ;
}
}
}
2012素数判定
import java. util. Arrays;
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
int [ ] nums = new int [ 3000 ] ;
nums[ 2 ] = 0 ;
nums[ 3 ] = 0 ;
nums[ 5 ] = 0 ;
for ( int i = 2 ; i < nums. length; i++ ) {
int j= 2 ;
if ( nums[ i] == 0 ) {
for ( ; j* i < nums. length; j++ ) {
nums[ j* i] = 1 ;
}
}
}
while ( in. hasNext ( ) ) {
int x = in. nextInt ( ) ;
int y = in. nextInt ( ) ;
if ( x== 0 && y== 0 ) {
break ;
}
int i;
for ( i = x; i <= y; i++ ) {
if ( nums[ ( i* i+ i+ 41 ) ] != 0 ) {
break ;
}
}
if ( i== y+ 1 ) {
System. out. println ( "OK" ) ;
} else {
System. out. println ( "Sorry" ) ;
}
}
}
}
2013 蟠桃记
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int n;
while ( in. hasNext ( ) ) {
int sum= 1 ;
n= in. nextInt ( ) ;
for ( int i= 1 ; i< n; i++ ) {
sum*= 2 ;
}
sum*= 3 ;
System. out. println ( sum- 2 ) ;
}
}
}
2015 偶数求和
import java. util. ArrayList;
import java. util. List;
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
int [ ] nums = new int [ 100 ] ;
nums[ 0 ] = 2 ;
for ( int i = 1 ; i < nums. length; i++ ) {
nums[ i] = nums[ i- 1 ] + 2 ;
}
while ( in. hasNext ( ) ) {
int n = in. nextInt ( ) ;
int m = in. nextInt ( ) ;
List< Integer> list = new ArrayList < > ( ) ;
if ( n% m== 0 ) {
for ( int i = 0 ; i < n; i+= m) {
int sum = 0 ;
for ( int j = i; j < i+ m; j++ ) {
sum+= nums[ j] ;
}
list. add ( sum/ m) ;
}
} else {
int i = 0 ;
for ( ; i < n- ( n% m) ; i+= m) {
int sum = 0 ;
for ( int j = i; j < i+ m; j++ ) {
sum+= nums[ j] ;
}
list. add ( sum/ m) ;
}
int sum = 0 ;
m = n% m;
for ( int j = i; j < n; j++ ) {
sum+= nums[ j] ;
}
list. add ( sum/ m) ;
}
for ( int i = 0 ; i < list. size ( ) - 1 ; i++ ) {
System. out. print ( list. get ( i) + " " ) ;
}
System. out. println ( list. get ( list. size ( ) - 1 ) ) ;
}
}
}