238 - 數(shù)字統(tǒng)計(jì)
Description
輸入一個(gè)長(zhǎng)整型的數(shù)焊傅,統(tǒng)計(jì)其中0、1狈涮、2、3鸭栖、4歌馍、5、6晕鹊、7松却、8、9各個(gè)數(shù)字的個(gè)數(shù)溅话,并將結(jié)果合成一個(gè)整數(shù)晓锻。(前面的0不輸出)
Input
長(zhǎng)整型數(shù)
Output
合成后的整數(shù)
Sample Input
234353632
Sample Output
24111000
MyAnswer
import java.util.*;
//H7EaGRl7
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
long num = scan.nextLong();
System.out.println(num);
int a[] = {0,0,0,0,0,0,0,0,0,0};
String s = String.valueOf(num);
System.out.println(s.charAt(0));
for(int i=0; i<10; i++)
if (s.charAt(i) == '0') {
a[0]++;
} else if (s.charAt(i) == '1') {
a[1]++;
} else if (s.charAt(i) == '2') {
a[2]++;
} else if (s.charAt(i) == '3') {
a[3]++;
} else if (s.charAt(i) == '4') {
a[4]++;
} else if (s.charAt(i) == '5') {
a[5]++;
} else if (s.charAt(i) == '6') {
a[6]++;
} else if (s.charAt(i) == '7') {
a[7]++;
} else if (s.charAt(i) == '8') {
a[8]++;
} else if (s.charAt(i) == '9') {
a[9]++;
}
long res=0;
for(int i=0; i<10; i++){
res = res*10 + i;
}
System.out.println(res);
}
}
239 - 家具類
Description
構(gòu)建家具類Furniture,包括長(zhǎng)飞几、寬砚哆、高,均為整數(shù)(cm)屑墨。提供相應(yīng)的構(gòu)造函數(shù)和get躁锁、set函數(shù)。
Main函數(shù)里構(gòu)造家具對(duì)象卵史,并調(diào)用相應(yīng)的函數(shù)战转。
Input
家具對(duì)象的長(zhǎng)寬高
Output
家具對(duì)象的相關(guān)屬性
Sample Input
50 60 100
Sample Output
100
50
60
Pre Append Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Furniture f = new Furniture(sc.nextInt(),sc.nextInt(),sc.nextInt());
System.out.println(f.getHeight());
System.out.println(f.getLength());
System.out.println(f.getWidth());
}
}
MyAnswer
class Furniture{
int length;
int width;
int height;
Furniture(int length, int width, int height){
this.length = length;
this.width = width;
this.height = height;
}
int getHeight(){
return this.height;
}
int getWidth(){
return this.width;
}
int getLength(){
return this.length;
}
}
241 - 手機(jī)類
Description
構(gòu)造手機(jī)類,包含其配置信息:型號(hào)(字符串)以躯、內(nèi)存大谢毖怼(整數(shù))、存儲(chǔ)空間(整數(shù),GB為單位)刁标、價(jià)格(整數(shù))颠通。提供帶參數(shù)的構(gòu)造函數(shù),重寫其equals方法命雀,使得兩個(gè)相同配置(型號(hào)蒜哀、內(nèi)存、存儲(chǔ)相同即可吏砂,價(jià)格可不同)的手機(jī)為相等的手機(jī)撵儿。重寫其toString函數(shù),打印手機(jī)的配置信息狐血,形式為CellPhone [model:xxx, memory:xxx, storage:xxx, price:xxx]
main函數(shù)中從鍵盤讀入兩個(gè)手機(jī)對(duì)象淀歇,比較他們是否相等,輸出他們的配置信息匈织。
Input
兩個(gè)計(jì)算機(jī)對(duì)象浪默,包含型號(hào)、內(nèi)存缀匕、存儲(chǔ)空間纳决、價(jià)格
Output
兩個(gè)對(duì)象是否相等,兩個(gè)對(duì)象的配置信息
Sample Input
P20 8 64 4999
P20 8 64 4999
Sample Output
true
CellPhone [model:P20, memory:8, storage:64, price:4999]
CellPhone [model:P20, memory:8, storage:64, price:4999]
Pre Append Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CellPhone c1 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
CellPhone c2 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
System.out.println(c1.equals(c2));
System.out.println(c1);
System.out.println(c2);
}
}
MyAnswer
class CellPhone{
String model;
int memory;
int storage;
int price;
CellPhone(String model, int memory, int s, int p){
this.model = model;
this.memory = memory;
this.storage = s;
this.price = p;
}
public String toString(){
String s = "CellPhone [model:"+ model+", memory:"+memory+", storage:"+storage+", price:"+price+"]";
return s;
}
public boolean equals(CellPhone o) {
if (this.model.equals(o.model) && this.memory == o.memory && this.storage==o.storage) {
return true;
}
return false;
}
}
242 - 租車服務(wù)
Description
某租車公司提供租車服務(wù)乡小,針對(duì)不同的車輛類型阔加,日租金的計(jì)算方式不同,具體地满钟,對(duì)于貨車而言胜榔,根據(jù)載重量load(單位是噸)計(jì)算,公式為load1000湃番;對(duì)于大型客車而言夭织,根據(jù)車內(nèi)座位數(shù)seats計(jì)算,公式為seats50吠撮;對(duì)于小型汽車而言尊惰,根據(jù)車輛等級(jí)和折舊年數(shù)計(jì)算,公式為200*level/sqrt(year)泥兰,其中sqrt表示平方根择浊。設(shè)計(jì)合適的類繼承結(jié)構(gòu)實(shí)現(xiàn)上述功能,構(gòu)造租車公司類CarRentCompany逾条,提供靜態(tài)函數(shù)rentVehicles琢岩,能夠給定一組待租車輛,計(jì)算日租金總額师脂。
在main函數(shù)中担孔,讀入多個(gè)車輛數(shù)據(jù)江锨,并計(jì)算總的日租金。
Input
汽車數(shù)量
汽車種類 該類汽車相關(guān)屬性
其中1表示貨車糕篇,2表示大型客車啄育,3表示小型汽車
Output
總的日租金,保留兩位小數(shù)
Sample Input
3
1 3
2 50
3 5 5
Sample Output
5947.21
Pre Append Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
Vehicle[] vs = new Vehicle[c];
for (int i=0;i<c;i++) {
int type = sc.nextInt();
Vehicle v = null;
if (type == 1) {//貨車
vs[i] = new Truck (sc.nextDouble());
} else if (type == 2) {
vs[i] = new Keche(sc.nextInt());
} else if (type == 3) {
vs[i] = new Car(sc.nextInt(), sc.nextInt());
}
}
System.out.printf("%.2f",CarRentCompany.rentVehicles(vs));
}
}
MyAnswer
abstract class Vehicle{
public abstract double getrent();
}
class Truck extends Vehicle{
double load;
Truck(double load){
this.load = load;
}
public double getrent(){
return load*1000;
}
}
class Keche extends Vehicle{
int seats;
Keche(int seats){
this.seats = seats;
}
public double getrent(){
return seats*50;
}
}
class Car extends Vehicle{
int level;
int year;
Car(int level, int year){
this.level = level;
this.year = year;
}
public double getrent(){
return 200*level/Math.sqrt(year);
}
}
class CarRentCompany {
public static double rentVehicles(Vehicle[] vs) {
double rent = 0;
for (Vehicle av : vs)
rent += av.getrent();
return rent;
}
}
243 - 字符串加密
Description
嘗試構(gòu)造一種自定義的字符串加密方式拌消,首先該字符串的長(zhǎng)度對(duì)5求余加1作為種子數(shù)字挑豌,以該種子數(shù)字為間隔,獲取原字符串的子字符序列墩崩,該序列求逆得到最終的序列氓英。
Input
原字符串
Output
加密后的字符串
Sample Input
abcdefghijklmn
Sample Output
kfa
MyAnswer
import java.util.Scanner;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int len = s.length();
int seed = len % 5 + 1;
char[] ch = s.toCharArray();
String newstr = "";
for(int i=0; i<len; i += seed){
newstr += ch[i];
}
System.out.println(new StringBuffer(newstr).reverse());
}
}
244 - 成績(jī)管理系統(tǒng)
Description
構(gòu)造一個(gè)成績(jī)管理系統(tǒng)CourseManagementSystem,該系統(tǒng)包括如下幾個(gè)方法:void add(int no, int grade)添加該學(xué)號(hào)的成績(jī)鹦筹,如果系統(tǒng)已有該學(xué)生成績(jī)铝阐,則輸出"the student already exists";void delete(int no)刪除某學(xué)號(hào)成績(jī)铐拐,如果不存在此學(xué)生則輸出"no such student"徘键;int query(int no)查詢并返回該學(xué)號(hào)的成績(jī);統(tǒng)計(jì)成績(jī)void statistics( )統(tǒng)計(jì)[0-59]遍蟋、[60-69]吹害、[70-79]、[80-89]虚青、[90-100]各成績(jī)段的學(xué)生個(gè)數(shù)并打印它呀。請(qǐng)選擇合適的容器實(shí)現(xiàn)上述功能。(題目假設(shè)不會(huì)重復(fù)添加相同學(xué)號(hào)的學(xué)生成績(jī))
main函數(shù)中讀入操作類型及相關(guān)參數(shù)挟憔,并調(diào)用statictic函數(shù)輸出學(xué)生成績(jī)統(tǒng)計(jì)信息。
Input
操作個(gè)數(shù)
操作名 操作參數(shù)
Output
查詢學(xué)生的成績(jī)
各成績(jī)段的學(xué)生個(gè)數(shù)
Sample Input
8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90
Sample Output
the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1
Pre Append Code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CourseManagementSystem cms = new CourseManagementSystem();
int ops = sc.nextInt();
for (int i=0;i<ops;i++) {
String op = sc.next();
if (op.equals("add"))
cms.add(sc.nextInt(), sc.nextInt());
else if (op.equals("delete"))
cms.delete(sc.nextInt());
else if (op.equals("query")) {
int no = sc.nextInt();
int s = cms.query(no);
System.out.println("the score for "+no+" is : "+s);
}
}
cms.statistic();
}
}
MyAnswer
/**
* 本題更好的做法是使用STL : Map
*/
class CourseManagementSystem{
ArrayList<Student> ALs = new ArrayList<>();
void add(int no, int grade){
for(Student st: ALs){
if(st.no == no){
System.out.println("the student already exists");
return ;
}
}
Student stu = new Student(no,grade);
ALs.add(stu);
}
void delete(int no){
int flag = 0;
for(int j=0;j<ALs.size(); j++)
if (ALs.get(j).getNo() == no) {
ALs.remove(j);
flag = 1;
}
if(flag == 0){
System.out.println("no such student");
}
}
int query(int no){
for(Student stu: ALs)
if(stu.no == no) {
return stu.getGrade();
}
return 0;
}
void statistic(){
int a[] = {0,0,0,0,0};
for(Student stu: ALs){
if(stu.grade >=0&&stu.grade <=59)
a[0]++;
else if(stu.grade >=60&&stu.grade <=69)
a[1]++;
else if(stu.grade >=70&&stu.grade <=79)
a[2]++;
else if(stu.grade >=80&&stu.grade <=89)
a[3]++;
else if(stu.grade >=90&&stu.grade <=100)
a[4]++;
}
System.out.println("[0-59] : "+a[0]);
System.out.println("[60-69] : "+a[1]);
System.out.println("[70-79] : "+a[2]);
System.out.println("[80-89] : "+a[3]);
System.out.println("[90-100] : "+a[4]);
}
}
class Student{
int no;
int grade; //根據(jù)題目描述烟号,應(yīng)該是int绊谭,實(shí)際情況下double更合適
Student(int no,int score){
this.no = no;
this.grade = score;
}
int getNo(){return this.no;}
int getGrade(){return this.grade;}
}