T1160
目前正是甲流盛行时期,为了更好地进行分流治疗,医院在挂号时要求对病人的体温和咳嗽情况进行检查,对于体温超过 37.5 度(含等于 37.5度)并且咳嗽的病人初步判定为甲流病人(初筛)。现需要统计某天前来挂号就诊的病人中有多少人被初筛为甲流病人。
解析:使用 Patient 类来存储对象,有三个属性 姓名,体温,是否咳嗽,然后按照条件输出对应结果
package com.java3.ch4;
import java.util.ArrayList;
import java.util.Scanner;
public class T1160 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Patient> dataList = new ArrayList<>();
int amount = scan.nextInt();//获得数据总数
scan.nextLine();//去掉缓存区的 \n
for(int i = 0;i < amount;i++){
String name = scan.next();
Double temperature = scan.nextDouble();
Integer isCough = scan.nextInt();
Patient temp = new Patient(name,temperature,isCough);
dataList.add(temp);
}
//测试数据导入
// for(int i = 0; i < amount;i++){
// System.out.println(dataList.get(i));
// }
//准备输出结果
int patientAmount = 0;
for(int i = 0;i < amount;i++){
Patient temp = dataList.get(i);
if((temp.getTemperature() >= 37.5) && (temp.getIsCough() == 1)){
System.out.println(temp.getName());
patientAmount++;
}
}
System.out.println(patientAmount);
}
}
class Patient{
private String name;
private Double temperature;
private Integer isCough;
public Patient(String name, Double temperature, Integer isCough) {
this.name = name;
this.temperature = temperature;
this.isCough = isCough;
}
public String getName() {
return name;
}
public Double getTemperature() {
return temperature;
}
public Integer getIsCough() {
return isCough;
}
//测试数据方法
@Override
public String toString() {
return "Patient{" +
"name='" + name + '\'' +
", temperature=" + temperature +
", isCough=" + isCough +
'}';
}
}
5
Zhang 38.3 0
Li 37.5 1
Wang 37.1 1
Zhao 39.0 1
Liu 38.2 1
Li
Zhao
Liu
3
T1119
近日,一些热门网站遭受黑客入侵,这些网站的账号、密码及 email 的数据惨遭泄露。你在这些网站上注册若干账号(使用的用户名不一定相同),但是注册时使用了相同的 email。你此时拿到了那份泄露的数据,希望尽快将自己的密码更改。策略如下:根据 email 找到你的用户名和密码,然后更改密码。更改的规则为:小写和大写交换,非字母字符保持不变。
解析:使用 WebData类来保存数据,有三个属性分别为账号,密码,email,定义静态方法 repalce(),实现小写和大写交换,非字母字符保持不变,
package com.java3.ch6;
import java.util.ArrayList;
import java.util.Scanner;
public class T1119 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<WebData> dateList = new ArrayList();
String searchEmail = scan.next();
int amount = scan.nextInt();
scan.nextLine();
for(int i = 0;i < amount;i++){
String user = scan.next();
String password = scan.next();
String email = scan.next();
WebData wd = new WebData(user,password,email);
dateList.add(wd);
}
//测试数据接收
// for(int i = 0;i < amount;i++){
// System.out.println(dateList.get(i));
// }
boolean isSearch = false;
for(int i = 0;i < amount;i++){
WebData temp = dateList.get(i);
if(searchEmail.equals(temp.getEmail())){
System.out.println(temp.getUser() +
" " + replace(temp.getPassword()));
isSearch = true;
}
}
if(!isSearch){
System.out.println("empty");
}
}
public static String replace(String str){
char[] arr = str.toCharArray();
for(int i = 0;i < arr.length;i++){
int temp = (int)arr[i];
if((temp >= 65) && (temp <= 90)){
arr[i] = (char)(temp + 32);
}else if((temp >= 97) && (temp <= 122)){
arr[i] = (char)(temp - 32);
}
}
return new String(arr);
}
}
class WebData{
private String user;
private String password;
private String email;
public WebData(String user, String password, String email) {
this.user = user;
this.password = password;
this.email = email;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "WebData{" +
"user='" + user + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}
abc@pku.edu.cn
5
helloKitty iLoveCats abc@pku.edu.cn
2012 maya2012 cplusplus@exam.com
KittyCat 5iKitty abc@pku.edu.cn
program password teacher@exam.com
whoAmi Feb.29$ abc@pku.edu.cn
helloKitty IlOVEcATS
KittyCat 5IkITTY
whoAmi fEB.29$
T1139
给定 n 行 m 列的图像各像素点的灰度值,要求用如下方法对其进行模糊化处理:
- 四周最外侧的像素点灰度值不变;
- 中间各像素点新灰度值为该像素点及其上下左右相邻四个像素点原灰度值的平均(舍入到最接近的整数)。
解析:使用二维数组来存储数据,主要强调一下double型数据的四舍五入问题,采用 DecimalFormat() 方法
import java.text.DecimalFormat;
public class TestNumberFormat{
public static void main(String[]args){
double pi = 3.1415927; //圆周率
//取一位整数
System.out.println(new DecimalFormat("0").format(pi)); //3
//取一位整数和两位小数
System.out.println(new DecimalFormat("0.00").format(pi)); //3.14
//取两位整数和三位小数,整数不足部分以0填补。
System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
//取所有整数部分
System.out.println(new DecimalFormat("#").format(pi)); //3
//以百分比方式计数,并取两位小数
System.out.println(new DecimalFormat("#.##%").format(pi)); //314.16%
long c =299792458; //光速
//显示为科学计数法,并取五位小数
System.out.println(new DecimalFormat("#.#####E0").format(c)); //2.99792E8
//显示为两位整数的科学计数法,并取四位小数
System.out.println(new DecimalFormat("00.####E0").format(c)); //29.9792E7
//每三位以逗号进行分隔。
System.out.println(new DecimalFormat(",###").format(c)); //299,792,458
//将格式嵌入文本
System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));
}
}
DecimalFormat 类主要靠 # 和 0 两种占位符号来指定数字长度。0 表示如果位数不足则以 0 填充,# 表示只要有可能就把数字拉上这个位置
package com.java3.ch5;
import java.text.DecimalFormat;
import java.util.Scanner;
public class T1139 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] array = new int[n][m];
int[][] result = new int[n][m];
//采用二维数据接受数据
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
array[i][j] = scan.nextInt();
}
}
// 对二维数组的数据进行处理
for(int i = 0;i < n ;i++){
for(int j = 0;j < m;j++){
if(i == 0 || i == n-1 || j == 0||j == m-1){
result[i][j] = array[i][j];
}else{
result[i][j] = progress(array,i,j);
}
}
}
//输出二维数组
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
public static int progress(int[][] arr,int row,int line){
int temp = arr[row-1][line] + arr[row + 1][line]
+ arr[row][line-1]+ arr[row][line+1]
+ arr[row][line];
DecimalFormat format = new DecimalFormat("0");
return Integer.parseInt(format.format(temp / 5.0));
}
}
4 5
100 0 100 0 50
50 100 200 0 0
50 50 100 100 200
100 100 50 50 100
100 0 100 0 50
50 80 100 60 0
50 80 100 90 200
100 100 50 50 100
T1159
用手机发短信,一条短信资费为 0.1 元,但限定一条短信的内容在 70 个字以内(包括 70 个字)。如果蒜头君一次所发送的短信超过了 70 个字,则会按照每 70 个字一条短信的限制把它分割成多条短信发送。假设已经知道蒜头君当月所发送的短信的字数,试统计一下蒜头君当月短信的总资费。
解析:我也不知道当时为什么做错
package com.java3.ch1;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class T1159 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int amount = scan.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0;i < amount;i++){
list.add(scan.nextInt());
}
int sum = 0;
for(int i = 0;i < amount;i++){
int temp = list.get(i);
if(temp % 70 == 0){
sum += (temp / 70);
}else{
sum += (temp / 70 + 1);
}
}
System.out.println(new DecimalFormat(".0").format(sum*0.1));
}
}
10
39
49
42
61
44
147
42
72
35
46
1.3
参考文献
链接:计蒜客:题库