第三届传智杯程序设计赛B组初赛题解Java版
A - 课程报名
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
int n = in. nextInt ( ) ;
int v = in. nextInt ( ) ;
int m = in. nextInt ( ) ;
int a = in. nextInt ( ) ;
int sum = 0 ;
int temp = 1 ;
for ( int i= 0 ; i< n; i++ ) {
if ( i== m* temp) {
temp++ ;
}
sum+= v+ ( temp- 1 ) * a;
}
System. out. println ( sum) ;
}
}
B - 期末考试成绩
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
double gpa = 0.0 ;
int temp = 0 ;
int course = 0 ;
int x = in. nextInt ( ) ;
if ( x >= 90 ) {
gpa = 4.0 ;
} else if ( x>= 60 && x<= 89 ) {
temp = 90 - x;
gpa = 4.0 - temp* 0.1 ;
} else {
course = ( int ) Math. floor ( Math. sqrt ( x) * 10 ) ;
if ( course >= 90 ) {
gpa = 4.0 ;
} else if ( course>= 60 && course<= 89 ) {
temp = 90 - course;
gpa = 4.0 - temp* 0.1 ;
} else {
gpa = 0.0 ;
}
}
System. out. println ( String. format ( "%.1f" , gpa) ) ;
}
}
C - 志愿者
import java. util. Arrays;
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
int n = in. nextInt ( ) ;
Volunteer[ ] volunteerList = new Volunteer [ n] ;
for ( int i= 0 ; i< n; i++ ) {
int t = in. nextInt ( ) ;
int k = in. nextInt ( ) ;
volunteerList[ i] = new Volunteer ( ) ;
volunteerList[ i] . i = ( i+ 1 ) ;
volunteerList[ i] . t = t;
volunteerList[ i] . k = k;
volunteerList[ i] . tk = ( t* k) ;
}
Arrays. sort ( volunteerList) ;
for ( int i= 0 ; i< n; i++ ) {
if ( i!= ( n- 1 ) )
System. out. print ( volunteerList[ i] . i+ " " ) ;
else
System. out. print ( volunteerList[ i] . i) ;
}
}
static class Volunteer implements Comparable < Volunteer> {
public int i;
public int t;
public int k;
public int tk;
@Override
public int compareTo ( Volunteer b) {
if ( this . tk > b. tk) {
return - 1 ;
} else if ( this . tk == b. tk) {
if ( this . t > b. t) {
return - 1 ;
} else if ( this . t == b. t) {
if ( this . i < b. i)
return - 1 ;
}
}
return 1 ;
}
}
}
D - 终端
import java. util. *;
public class Main {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
int n = in. nextInt ( ) ;
List< String> fileSystem = new ArrayList < > ( ) ;
for ( int i = 0 ; i < n; i++ ) {
String file = null;
String command = in. next ( ) ;
if ( ! "ls" . equals ( command) )
file = in. next ( ) ;
if ( "touch" . equals ( command) ) {
if ( file != null) {
fileSystem. add ( file) ;
}
} else if ( "rm" . equals ( command) ) {
if ( file != null) {
if ( fileSystem. contains ( file) ) {
fileSystem. remove ( file) ;
}
}
} else if ( "ls" . equals ( command) ) {
for ( String s : fileSystem) {
System. out. println ( s) ;
}
} else {
String newfile = in. next ( ) ;
if ( file != null) {
if ( fileSystem. contains ( file) && ! fileSystem. contains ( newfile) ) {
for ( int j = fileSystem. size ( ) - 1 ; j >= 0 ; j-- ) {
if ( fileSystem. get ( j) . equals ( file) ) {
fileSystem. set ( j, newfile) ;
}
}
}
}
}
}
}
}
E - 运气
import java. util. *;
public class Main {
private static int n;
private static int k;
private static long res;
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
n = in. nextInt ( ) ;
k = in. nextInt ( ) ;
if ( n == 0 ) {
return ;
}
dfs ( 0 , "" ) ;
System. out. println ( ( long ) ( res% ( Math. pow ( 10 , 9 ) + 7 ) ) ) ;
}
private static void check ( String number) {
long parseLong = Long. parseLong ( number) ;
if ( parseLong% k == 0 ) {
res++ ;
}
}
private static void dfs ( int level, String number) {
if ( level == n) {
check ( number) ;
return ;
}
for ( int i = 1 ; i <= 6 ; i++ ) {
dfs ( level+ 1 , number+ i) ;
}
}
}